:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #eef1f6;
  --surface-hover: #f8f9fc;
  --border: #e2e6ec;
  --text: #1c2434;
  --muted: #6b7686;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --danger-bg: #fde8e8;
  --input-bg: #ffffff;
  --toast-bg: #1c2434;
  --toast-text: #ffffff;
  --btn-primary-text: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 30, 50, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161f;
    --surface: #1a2030;
    --surface-alt: #232a3d;
    --surface-hover: #202637;
    --border: #2c3448;
    --text: #e7ebf3;
    --muted: #98a2b8;
    --primary: #5b8def;
    --primary-dark: #4570d6;
    --green: #34d399;
    --red: #f87171;
    --danger-bg: #3a2028;
    --input-bg: #1a2030;
    --toast-bg: #2c3448;
    --toast-text: #e7ebf3;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  --bg: #12161f;
  --surface: #1a2030;
  --surface-alt: #232a3d;
  --surface-hover: #202637;
  --border: #2c3448;
  --text: #e7ebf3;
  --muted: #98a2b8;
  --primary: #5b8def;
  --primary-dark: #4570d6;
  --green: #34d399;
  --red: #f87171;
  --danger-bg: #3a2028;
  --input-bg: #1a2030;
  --toast-bg: #2c3448;
  --toast-text: #e7ebf3;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 1.15rem; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.muted { color: var(--muted); font-size: 0.8rem; }

.btn {
  display: inline-block;
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
}
.btn-primary { background: var(--primary); color: var(--btn-primary-text); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--surface-alt); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.btn-icon { background: var(--surface-alt); padding: 8px 12px; font-size: 1rem; line-height: 1; color: var(--text); }
.btn-danger { background: var(--danger-bg); color: var(--red); padding: 6px 10px; font-size: 0.8rem; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  padding-left: calc(12px + env(safe-area-inset-left));
  padding-right: calc(12px + env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  position: sticky;
  top: calc(53px + env(safe-area-inset-top));
  z-index: 9;
}
.tab {
  border: none;
  background: transparent;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

main {
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
  max-width: 1100px;
  margin: 0 auto;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.panel h2 { font-size: 1rem; margin: 0 0 12px; }
.panel-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.panel-header h2 { margin: 0; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 760px) {
  .grid-2 { grid-template-columns: 1fr; }
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}
.card .label { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; }
.card .value { font-size: 1.3rem; font-weight: 700; }
.card .sub { font-size: 0.78rem; margin-top: 4px; }

.positive { color: var(--green); }
.negative { color: var(--red); }

.chart-wrap { position: relative; height: 280px; }
.chart-wrap.small { height: 220px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.87rem; min-width: 560px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.02em; }
tbody tr:hover { background: var(--surface-hover); }

.form-inline { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.form-inline.compact { gap: 6px; }
.form-inline input, .form-inline select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.88rem;
  background: var(--input-bg);
  color: var(--text);
  min-width: 0;
  flex: 1 1 140px;
}
.select-sm { padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; font-size: 0.85rem; background: var(--input-bg); color: var(--text); }

.hint { color: var(--muted); font-size: 0.8rem; margin: 8px 0 0; }
.hint code { background: var(--surface-alt); padding: 1px 5px; border-radius: 4px; }
.hint a { color: var(--primary); }

.search-results { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.search-result {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface-hover);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.87rem;
  cursor: pointer;
  color: var(--text);
}
.search-result:hover { background: var(--surface-alt); border-color: var(--primary); }
.search-result strong { margin-right: 6px; }

.breakdown-block { margin-bottom: 14px; }
.breakdown-block h3 { font-size: 0.82rem; color: var(--muted); text-transform: uppercase; margin: 0 0 6px; }
.breakdown-row { display: flex; justify-content: space-between; font-size: 0.88rem; padding: 4px 0; border-bottom: 1px dashed var(--border); }

#simTable input[type="number"] { width: 90px; padding: 5px 7px; }

.corr-table td, .corr-table th { text-align: center; padding: 6px 8px; }
.corr-table th:first-child, .corr-table td:first-child { text-align: left; }

.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--toast-bg);
  color: var(--toast-text);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s ease;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--red); }

@media (max-width: 480px) {
  .topbar h1 { font-size: 1rem; }
  table { min-width: 480px; }
}

.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top));
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-card h1 { font-size: 1.2rem; margin: 0 0 4px; }
.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--input-bg);
  color: var(--text);
  text-align: center;
}
.login-card .btn { width: 100%; padding: 11px; font-size: 0.95rem; }
