:root {
  --bg: #f3f4f8;
  --card: #ffffff;
  --text: #151821;
  --muted: #6b7385;
  --line: #e7e9f0;
  --soft: #eef0f6;
  --accent: #4c78ff;
  --accent-text: #ffffff;
  --good: #1f9d6b;
  --bad: #e5484d;
  --shadow: 0 1px 2px rgba(20, 24, 40, 0.04), 0 8px 24px rgba(20, 24, 40, 0.05);
  --radius: 18px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1015;
    --card: #171a22;
    --text: #eceef4;
    --muted: #8a92a6;
    --line: #262a35;
    --soft: #1f232d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e1015;
  --card: #171a22;
  --text: #eceef4;
  --muted: #8a92a6;
  --line: #262a35;
  --soft: #1f232d;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.gate { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.gate-card {
  max-width: 360px; text-align: center; background: var(--card); border-radius: 24px;
  padding: 32px 28px; box-shadow: var(--shadow);
}
.gate-icon { font-size: 44px; margin-bottom: 8px; }
.gate-card p { color: var(--muted); margin: 12px 0 0; white-space: pre-line; }

.top {
  position: relative; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  padding: 14px 16px 10px; border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; max-width: 1180px; margin: 0 auto 10px; }
.presets, .custom-range, .filters, .chips { max-width: 1180px; margin-left: auto; margin-right: auto; }
.presets { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.presets::-webkit-scrollbar { display: none; }
.presets button {
  flex: 0 0 auto; border: 0; background: var(--soft); color: var(--muted);
  padding: 7px 13px; border-radius: 999px; font-weight: 550; transition: background .15s, color .15s;
}
.presets button.on { background: var(--accent); color: var(--accent-text); }
.custom-range { display: flex; gap: 10px; margin-top: 8px; flex-wrap: wrap; color: var(--muted); }
.custom-range input {
  background: var(--soft); border: 0; border-radius: 10px; padding: 7px 10px; margin-left: 4px;
}
.filters { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line);
  background: var(--card); padding: 7px 13px; border-radius: 999px; font-weight: 550;
}
.pill b {
  background: var(--accent); color: var(--accent-text); border-radius: 999px;
  min-width: 20px; height: 20px; display: grid; place-items: center; font-size: 12px; padding: 0 6px;
}
.switch { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); user-select: none; cursor: pointer; margin-left: 4px; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch i {
  width: 36px; height: 22px; border-radius: 999px; background: var(--soft); position: relative;
  border: 1px solid var(--line); transition: background .2s;
}
.switch i::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--muted); transition: transform .2s, background .2s;
}
.switch input:checked + i { background: var(--accent); border-color: var(--accent); }
.switch input:checked + i::after { transform: translateX(14px); background: #fff; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chips:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; border: 0; background: var(--soft);
  padding: 4px 6px 4px 10px; border-radius: 999px; font-size: 13px;
}
.chip i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.chip span { width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center; background: var(--line); font-size: 12px; line-height: 1; }

.ghost { border: 0; background: transparent; color: var(--accent); font-weight: 600; padding: 6px 8px; border-radius: 10px; }
.primary { border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600; padding: 11px 18px; border-radius: 12px; }

main {
  max-width: 1180px; margin: 0 auto; padding: 14px 16px 40px;
  display: grid; grid-template-columns: 1fr; gap: 14px; transition: opacity .2s;
}
main.loading { opacity: 0.55; pointer-events: none; }
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; background: var(--soft); border-radius: 11px; padding: 3px; }
.seg button {
  border: 0; background: transparent; color: var(--muted); padding: 5px 11px; border-radius: 9px;
  font-size: 13px; font-weight: 550; transition: background .15s, color .15s;
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.search, .select {
  background: var(--soft); border: 0; border-radius: 11px; padding: 8px 12px; min-width: 0;
}
.search { width: 150px; }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; min-width: 0; }
.kpi.hero { grid-column: 1 / -1; }
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi.hero .value { font-size: 34px; }
.kpi .sub { color: var(--muted); font-size: 13px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.delta { font-weight: 650; }
.delta.up { color: var(--bad); }
.delta.down { color: var(--good); }

.chart { position: relative; height: 240px; }
.chart.tall { height: 300px; }
.chart.short { height: 190px; }
.donut-wrap { display: flex; justify-content: center; }
.chart.donut { height: 220px; width: 100%; max-width: 280px; }
.chart.donut.big { height: 300px; max-width: 360px; margin: 0 auto 8px; }
.empty { text-align: center; color: var(--muted); padding: 28px 0; }

.rank { display: flex; flex-direction: column; gap: 2px; margin-top: 12px; }
.row {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 8px 8px; margin: 0 -8px;
  border-radius: 12px; border: 0; background: transparent; text-align: left; width: calc(100% + 16px);
}
button.row:hover { background: var(--soft); }
.row.on { background: var(--soft); }
.row .name { display: flex; align-items: center; gap: 8px; min-width: 0; font-weight: 550; }
.row .name i { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.row .name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .amount { text-align: right; font-weight: 650; white-space: nowrap; }
.row .meta { color: var(--muted); font-size: 12.5px; }
.row .meta.r { text-align: right; }
.bar { grid-column: 1 / -1; height: 6px; border-radius: 999px; background: var(--soft); overflow: hidden; display: flex; margin-top: 4px; }
.bar b { display: block; height: 100%; }

.group { border-top: 1px solid var(--line); padding: 4px 0; }
.group:first-child { border-top: 0; }
.group summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; gap: 10px; padding: 10px 0;
}
.group summary::-webkit-details-marker { display: none; }
.group summary i { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.group summary .gname { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group summary .gsum { font-weight: 650; }
.group summary::after { content: "›"; color: var(--muted); transition: transform .2s; font-size: 18px; }
.group[open] summary::after { transform: rotate(90deg); }
.group .inner { padding: 0 0 8px 20px; }

.ops { display: flex; flex-direction: column; }
.day { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; padding: 14px 0 6px; font-weight: 600; }
.day:first-child { padding-top: 4px; }
.op { display: grid; grid-template-columns: 38px 1fr auto; gap: 12px; align-items: center; padding: 9px 0; border-top: 1px solid var(--line); }
.day + .op { border-top: 0; }
.op .dot { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; font-weight: 700; font-size: 14px; color: #fff; }
.op .title { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.op .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.op .sum { font-weight: 650; white-space: nowrap; text-align: right; }
.op .sum small { display: block; color: var(--muted); font-weight: 500; font-size: 12px; }
.op > div { min-width: 0; }
.more { margin: 12px auto 0; display: block; border: 0; background: var(--soft); border-radius: 12px; padding: 10px 20px; font-weight: 600; }

.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-bg { position: absolute; inset: 0; background: rgba(8, 10, 16, 0.5); animation: fade .2s; }
.sheet-body {
  position: relative; background: var(--card); width: 100%; max-width: 480px; max-height: 82vh;
  border-radius: 22px 22px 0 0; padding: 16px; display: flex; flex-direction: column; gap: 10px;
  animation: rise .22s ease-out; padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet .search { width: 100%; }
.sheet-list { overflow-y: auto; flex: 1; min-height: 120px; margin: 0 -6px; padding: 0 6px; }
.pick { display: flex; align-items: center; gap: 12px; padding: 10px 6px; border-radius: 12px; cursor: pointer; }
.pick:hover { background: var(--soft); }
.pick input { width: 20px; height: 20px; accent-color: var(--accent); flex: 0 0 auto; }
.pick i { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.pick .pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pick .psub { color: var(--muted); font-size: 13px; }
.sheet-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.sheet-foot .primary { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 60; max-width: 90vw;
}

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (min-width: 720px) {
  .top { padding: 16px 24px 12px; }
  main { padding: 18px 24px 48px; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
  .card.wide, .kpis { grid-column: 1 / -1; }
  .kpis { grid-template-columns: repeat(4, 1fr); }
  .kpi.hero { grid-column: 1 / -1; }
  .kpi.hero .value { font-size: 36px; }
  .sheet { align-items: center; }
  .sheet-body { border-radius: 22px; padding-bottom: 16px; }
  .search { width: 190px; }
}

@media (min-width: 1000px) and (min-height: 760px) {
  .top { position: sticky; top: 0; }
}

@media (min-width: 1000px) {
  .chart.tall { height: 340px; }
  .kpis { grid-template-columns: repeat(6, 1fr); }
  .kpi.hero { grid-column: span 2; }
  .kpi .value { font-size: 22px; }
  .kpi.hero .value { font-size: 32px; }
}
