:root {
  color-scheme: light;
  --bg: #f6f7f2;
  --surface: #ffffff;
  --surface-muted: #eef1e8;
  --ink: #1c2520;
  --muted: #5f6b62;
  --line: #d8ded2;
  --teal: #167c80;
  --blue: #315f9f;
  --gold: #b77716;
  --red: #b4463a;
  --green: #3b7f45;
  --shadow: 0 10px 28px rgba(28, 37, 32, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

.app-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 24px;
  align-items: end;
  padding: 24px clamp(16px, 4vw, 44px) 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.55rem, 2vw, 2.1rem);
  line-height: 1.1;
  font-weight: 760;
}

.app-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
}

.header-meta {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}

main {
  width: min(1500px, calc(100vw - 32px));
  margin: 20px auto 40px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(140px, 0.8fr) minmax(180px, 1.2fr) minmax(160px, 1fr) repeat(2, minmax(140px, 0.8fr)) minmax(220px, 1.4fr) auto;
  gap: 12px;
  align-items: end;
  margin-bottom: 16px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

label {
  display: grid;
  gap: 6px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
  text-transform: uppercase;
}

select,
input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
}

select:focus,
input:focus,
button:focus {
  outline: 2px solid rgba(22, 124, 128, 0.28);
  outline-offset: 2px;
}

button {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-muted);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
}

button:hover {
  border-color: #b8c3b8;
  background: #e4ebdf;
}

.status {
  display: none;
  margin-bottom: 16px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
}

.status.is-visible {
  display: block;
}

.status.is-error {
  color: var(--red);
  border-color: rgba(180, 70, 58, 0.35);
  background: #fff6f4;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.kpi {
  min-height: 98px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.kpi span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

.kpi strong {
  color: var(--ink);
  font-size: clamp(1.35rem, 2.2vw, 2rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.chart-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.85fr);
  gap: 16px;
}

.panel {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-wide {
  grid-column: span 2;
}

.panel-heading {
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-heading h2 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.2;
  font-weight: 760;
}

.panel-heading > span {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px 12px;
  color: var(--muted);
  font-size: 0.82rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

canvas {
  display: block;
  width: 100%;
  height: 300px;
}

.panel-wide canvas {
  height: 320px;
}

.chart-tooltip {
  position: fixed;
  z-index: 20;
  max-width: min(320px, calc(100vw - 24px));
  padding: 10px 12px;
  border: 1px solid rgba(28, 37, 32, 0.22);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 12px 32px rgba(28, 37, 32, 0.18);
  color: var(--ink);
  pointer-events: none;
  font-size: 0.84rem;
}

.chart-tooltip[hidden] {
  display: none;
}

.tooltip-title {
  margin-bottom: 7px;
  font-weight: 780;
  line-height: 1.25;
}

.tooltip-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: baseline;
  padding-top: 4px;
  color: var(--muted);
}

.tooltip-row strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.tooltip-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.tooltip-swatch {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 2px;
}

.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  gap: 16px;
  margin-top: 16px;
}

.table-panel {
  padding-bottom: 0;
}

.table-scroll {
  max-height: 560px;
  overflow: auto;
  margin: 0 -16px;
  border-top: 1px solid var(--line);
}

table {
  width: 100%;
  min-width: 980px;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th,
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #eef1e8;
  color: #3c473f;
  font-size: 0.76rem;
  font-weight: 780;
  text-transform: uppercase;
}

td {
  color: var(--ink);
}

tr[data-event-id] {
  cursor: pointer;
}

tr[data-event-id]:hover,
tr[data-event-id].is-selected {
  background: #f4f7ef;
}

.number {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.source-cell {
  color: var(--blue);
  font-weight: 700;
  white-space: nowrap;
}

.note-flag {
  display: inline-flex;
  min-width: 28px;
  justify-content: center;
  padding: 2px 7px;
  border-radius: 999px;
  background: #f0e2c8;
  color: #6b4310;
  font-size: 0.78rem;
  font-weight: 760;
}

.note-detail {
  display: grid;
  gap: 12px;
  color: var(--muted);
  line-height: 1.45;
}

.note-detail dl {
  display: grid;
  grid-template-columns: minmax(92px, auto) 1fr;
  gap: 8px 12px;
  margin: 0;
}

.note-detail dt {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.note-detail dd {
  margin: 0;
  color: var(--ink);
  overflow-wrap: anywhere;
}

.note-text {
  max-height: 360px;
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcf8;
  color: var(--ink);
  white-space: pre-wrap;
}

.quality-row {
  margin-top: 16px;
}

.quality-list {
  display: grid;
  gap: 8px;
  color: var(--muted);
}

.quality-item {
  padding: 10px 12px;
  border-radius: 6px;
  background: #fff7e8;
  color: #6d4611;
}

@media (max-width: 1180px) {
  .toolbar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .search-control {
    grid-column: span 2;
  }

  .kpi-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .app-header {
    grid-template-columns: 1fr;
    align-items: start;
  }

  .header-meta {
    align-items: flex-start;
    white-space: normal;
  }

  main {
    width: min(100% - 20px, 1500px);
    margin-top: 10px;
  }

  .toolbar,
  .chart-grid,
  .detail-layout {
    grid-template-columns: 1fr;
  }

  .search-control,
  .panel-wide {
    grid-column: span 1;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .detail-layout {
    gap: 12px;
  }
}

@media (max-width: 560px) {
  .app-header {
    padding: 20px 14px 16px;
  }

  .toolbar {
    padding: 12px;
  }

  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .panel,
  .kpi {
    padding: 14px;
  }

  .table-scroll {
    margin: 0 -14px;
  }

  canvas,
  .panel-wide canvas {
    height: 280px;
  }
}
