:root {
  --bg: #12100e;
  --surface: #1b1815;
  --surface-2: #241f1b;
  --line: #322b25;
  --text: #f5efe8;
  --muted: #a79a8d;
  --accent: #f47721;
  --good: #57b894;
  --warn: #e8b23a;
  --bad: #e0603f;
  --radius: 14px;
  --tabbar: 62px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; font-weight: 700; }
h2 { font-size: 1rem; font-weight: 600; letter-spacing: 0.01em; }
h3 { font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
p { margin: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

#app {
  padding: 16px 16px calc(var(--tabbar) + env(safe-area-inset-bottom) + 24px);
  max-width: 720px;
  margin: 0 auto;
}

.view { display: grid; gap: 20px; }
[hidden] { display: none !important; }

/* Dashboard cards */
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 4px;
}
.card .label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.card .value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.card .sub { font-size: 0.78rem; color: var(--muted); }
.card.wide { grid-column: 1 / -1; }

.block { display: grid; gap: 10px; }
.hint { font-size: 0.8rem; color: var(--muted); line-height: 1.45; }

/* Rows */
.item {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: inherit;
  font: inherit;
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  align-items: baseline;
  cursor: pointer;
}
.item + .item { margin-top: 8px; }
.item:active { background: var(--surface-2); }
.item .name { font-weight: 600; }
.item .meta { grid-column: 1 / -1; font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.item .figure { font-weight: 700; font-variant-numeric: tabular-nums; }

.group-head {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 14px 0 6px;
}

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  border: 1px solid transparent;
}
.pill.good { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, transparent); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.pill.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.pill.flat { color: var(--muted); border-color: var(--line); }

.empty {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 18px 14px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

/* Buttons */
button, .file-btn {
  font: inherit;
  border-radius: 11px;
  border: 1px solid transparent;
  padding: 11px 14px;
  cursor: pointer;
  min-height: 44px;
}
.primary { background: var(--accent); color: #1a0e04; font-weight: 700; }
.secondary { background: var(--surface-2); color: var(--text); border-color: var(--line); font-weight: 600; }
.ghost { background: none; color: var(--muted); }
.ghost.danger { color: var(--bad); }
.icon-btn { background: none; color: var(--muted); padding: 6px 8px; min-height: 0; font-size: 0.85rem; }
.block-btn { width: 100%; }
.stack { display: grid; gap: 8px; }
.file-btn { display: block; text-align: center; font-weight: 600; }

/* Tab bar */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 6;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 10px 2px;
  border-radius: 0;
}
.tab.is-active { color: var(--accent); }

/* Forms and dialogs */
dialog {
  border: 0;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(560px, 100%);
  max-height: 92dvh;
  border-radius: 16px 16px 0 0;
  margin: auto auto 0;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.62); }
dialog > * {
  display: grid;
  gap: 12px;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  overflow-y: auto;
  max-height: 92dvh;
}
@media (min-width: 640px) {
  dialog { border-radius: 16px; margin: auto; }
}

label { display: grid; gap: 6px; font-size: 0.82rem; color: var(--muted); position: relative; }
input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 12px;
  min-height: 44px;
  width: 100%;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.suffix {
  position: absolute;
  right: 12px;
  bottom: 12px;
  font-size: 0.8rem;
  color: var(--muted);
  pointer-events: none;
}

.line-row { display: grid; grid-template-columns: 1fr 84px 34px; gap: 8px; align-items: end; }
.line-row.wide { grid-template-columns: 1fr 74px 84px 34px; }
.line-row .remove { background: none; color: var(--bad); border: 0; padding: 0; min-height: 44px; }
.line-row select { min-width: 0; }

.count-row { display: grid; grid-template-columns: 1fr 92px; gap: 8px; align-items: center; }
.count-row .count-name { font-size: 0.85rem; }
.count-row input { text-align: right; }

.live {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: grid;
  gap: 6px;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
.live .big { font-size: 1.35rem; font-weight: 700; }
.live .split { display: flex; justify-content: space-between; gap: 12px; }
.live .split span:first-child { color: var(--muted); }

.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 4px 0 0; }
.dialog-actions .ghost.danger { margin-right: auto; }

table.history { width: 100%; border-collapse: collapse; font-size: 0.8rem; font-variant-numeric: tabular-nums; }
table.history th { text-align: left; color: var(--muted); font-weight: 600; padding: 4px 0; }
table.history td { padding: 4px 0; border-top: 1px solid var(--line); }
table.history td:last-child, table.history th:last-child { text-align: right; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  z-index: 20;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 0.85rem;
  max-width: 92vw;
  text-align: center;
}
