:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #eef0f3;
  --text: #1a1a1a;
  --text-muted: #5a5f68;
  --border: #dde1e6;
  --accent: #1565c0;
  --accent-contrast: #ffffff;
  --danger: #c62828;
  --success: #2e7d32;
  --warning: #ef6c00;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #121316;
  --surface: #1c1e22;
  --surface-alt: #24262b;
  --text: #e8e9eb;
  --text-muted: #a3a8b0;
  --border: #33363c;
  --accent: #64b5f6;
  --accent-contrast: #0b1220;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121316;
    --surface: #1c1e22;
    --surface-alt: #24262b;
    --text: #e8e9eb;
    --text-muted: #a3a8b0;
    --border: #33363c;
    --accent: #64b5f6;
    --accent-contrast: #0b1220;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

body { max-width: 100%; overflow-x: hidden; }

a { color: var(--accent); }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

button, select, input {
  font-family: inherit;
  font-size: inherit;
}

.fatal-error {
  background: var(--danger);
  color: #fff;
  padding: 1rem;
  text-align: center;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  position: sticky;
  top: 0;
  z-index: 20;
}

.app-header-top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.app-title h1 { font-size: 1.15rem; margin: 0; }
.app-title-sub { margin: 0.15rem 0 0; color: var(--text-muted); font-size: 0.82rem; }

.app-status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.85rem;
}

.status-item { display: inline-flex; align-items: center; gap: 0.35rem; color: var(--text-muted); }

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
}
.status-dot.ok { background: var(--success); }
.status-dot.warn { background: var(--warning); }
.status-dot.error { background: var(--danger); }

.disclaimer {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-style: italic;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  min-height: 36px;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); font-weight: 600; }
.btn-icon { padding: 0.4rem 0.55rem; }

.countdown { color: var(--text-muted); font-size: 0.8rem; min-width: 2.5em; display: inline-block; }

.filter-toggle-mobile { display: none; margin: 0.5rem 1rem; }

.filter-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1.25rem;
  position: sticky;
  top: 62px;
  z-index: 15;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 0.6rem;
}

.filter-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 150px;
  border: none;
  margin: 0;
  padding: 0;
}

.panel-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  font-size: 0.85rem;
}

.panel-toolbar select {
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.filter-field[hidden] { display: none; }

.filter-field legend { font-weight: 600; font-size: 0.8rem; padding: 0; margin-bottom: 0.2rem; }
.filter-field label { font-size: 0.78rem; color: var(--text-muted); }

.filter-field select, .filter-field input {
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-height: 34px;
}

.filter-custom-range { flex-direction: row; align-items: center; flex-wrap: wrap; }

.checkbox-group { display: flex; flex-wrap: wrap; gap: 0.35rem; max-width: 320px; }

.checkbox-pill {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  font-size: 0.78rem;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
  background: var(--surface-alt);
}

.filter-more { margin-top: 0.5rem; }
.filter-more summary { cursor: pointer; font-weight: 600; font-size: 0.85rem; color: var(--accent); }

.filter-actions { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; margin-top: 0.6rem; }
.filter-summary { color: var(--text-muted); font-size: 0.82rem; }

.badge {
  background: var(--accent);
  color: var(--accent-contrast);
  border-radius: 999px;
  padding: 0.05rem 0.4rem;
  font-size: 0.75rem;
  margin-left: 0.3rem;
}

.filter-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.5rem; }
.chip {
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  cursor: pointer;
}

.tabs {
  display: flex;
  overflow-x: auto;
  gap: 0.25rem;
  padding: 0.5rem 1.25rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.tab-button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 0.6rem 0.9rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
  font-size: 0.88rem;
}
.tab-button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.tab-status { min-height: 1.2em; padding: 0.2rem 1.25rem; font-size: 0.8rem; color: var(--text-muted); }

.filter-notice {
  margin: 0 1.25rem 0.5rem;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  background: rgba(249, 168, 37, 0.12);
  border: 1px solid var(--warning);
  color: var(--text);
  font-size: 0.8rem;
}

.tab-content { padding: 0.5rem 1.25rem 3rem; }

.export-toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 0.75rem; }

.warning-banner {
  background: rgba(239, 108, 0, 0.15);
  border: 1px solid var(--warning);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  margin-bottom: 0.75rem;
}

.simulation-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.simulation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.simulation-grid label { display: grid; gap: 0.25rem; color: var(--text-muted); font-size: 0.8rem; }
.simulation-grid input, .simulation-grid select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.45rem;
  color: var(--text);
  background: var(--surface);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 0.9rem;
  box-shadow: var(--shadow);
}
.stat-card h3 { margin: 0 0 0.3rem; font-size: 0.75rem; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em; }
.stat-value { margin: 0; font-size: 1.4rem; font-weight: 700; }
.stat-denominator { margin: 0.15rem 0 0; font-size: 0.75rem; color: var(--text-muted); }
.stat-flag { margin: 0.25rem 0 0; font-size: 0.72rem; color: var(--warning); font-weight: 600; }
.stat-trend { display: inline-block; margin-top: 0.3rem; font-size: 0.75rem; color: var(--text-muted); }
.stat-trend.up { color: var(--success); }
.stat-trend.down { color: var(--danger); }

.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.4rem;
  position: relative;
}
.chart-card-wide { grid-column: 1 / -1; }

.chart-card-toolbar { display: flex; justify-content: flex-end; gap: 0.4rem; align-items: center; }
.chart-card-toolbar button {
  border: none; background: transparent; cursor: pointer; color: var(--text-muted);
  font-size: 1rem; padding: 0.2rem 0.4rem;
}

.chart-table-toggle {
  font-size: 0.72rem !important;
  border: 1px solid var(--border) !important;
  border-radius: 4px;
  padding: 0.15rem 0.4rem !important;
}
.chart-table-toggle[aria-pressed="true"] { color: var(--accent); border-color: var(--accent) !important; }

.chart-table-alt { width: 100%; font-size: 0.8rem; margin-top: 0.4rem; }

.chart { width: 100%; height: 320px; min-height: 280px; }

.chart-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--surface);
  padding: 1rem;
}
.chart-fullscreen .chart { height: calc(100vh - 3rem); }

.insights-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}
.insights-panel h2 { margin-top: 0; font-size: 1rem; }
.insights-panel ul { margin: 0; padding-left: 1.2rem; }
.insights-panel li { margin-bottom: 0.4rem; }

.table-wrapper { overflow-x: auto; margin-bottom: 1rem; border: 1px solid var(--border); border-radius: var(--radius); }

.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; min-width: 640px; }
.data-table thead { background: var(--surface-alt); position: sticky; top: 0; }
.data-table th, .data-table td { padding: 0.5rem 0.6rem; text-align: left; border-bottom: 1px solid var(--border); }
.data-table.sortable th[data-sort] { padding: 0; }
.th-sort-button {
  border: none; background: transparent; color: inherit; font: inherit; cursor: pointer;
  user-select: none; width: 100%; text-align: left; padding: 0.5rem 0.6rem;
}
.th-sort-button:hover { color: var(--accent); }
.th-sort-button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
th[aria-sort="ascending"] .th-sort-button::after { content: ' \25B2'; }
th[aria-sort="descending"] .th-sort-button::after { content: ' \25BC'; }

.row-open { background: rgba(117, 117, 117, 0.08); }
.row-loss { background: rgba(198, 40, 40, 0.08); }
.row-backfilled { background: rgba(106, 27, 154, 0.08); }

.empty-state { text-align: center; color: var(--text-muted); padding: 1.5rem !important; }

.link-button { border: none; background: transparent; color: var(--accent); cursor: pointer; text-decoration: underline; padding: 0; }

.explorer-toolbar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin-bottom: 0.75rem; }
.explorer-toolbar input[type="search"] {
  flex: 1; min-width: 220px; padding: 0.45rem 0.6rem; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}

.pagination-controls { display: flex; align-items: center; gap: 0.75rem; }

.note { color: var(--text-muted); font-size: 0.82rem; }

.modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 1rem;
}
.modal[hidden] { display: none; }
.modal-content {
  background: var(--surface); border-radius: var(--radius); padding: 1.25rem;
  max-width: 720px; width: 100%; max-height: 85vh; overflow-y: auto;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.modal-header h2 { margin: 0; font-size: 1.05rem; }

.detail-grid { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1rem; margin-bottom: 1rem; }
.detail-grid dt { color: var(--text-muted); font-size: 0.8rem; }
.detail-grid dd { margin: 0; font-size: 0.9rem; }

.app-footer { padding: 1rem 1.25rem 2rem; color: var(--text-muted); font-size: 0.78rem; text-align: center; }

@media (max-width: 860px) {
  .filter-toggle-mobile { display: inline-flex; }
  .filter-bar { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; overflow-y: auto; z-index: 50; }
  .filter-bar.open { display: block; }
  .app-status { font-size: 0.78rem; }
  .stat-value { font-size: 1.2rem; }
  .chart { height: 260px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

@media print {
  .app-header, .filter-bar, .filter-toggle-mobile, .tabs, .tab-status, .export-toolbar,
  .chart-card-toolbar, #theme-toggle, .modal { display: none !important; }
  .tab-panel { display: block !important; }
  body { background: #fff; color: #000; }
}
