:root {
  color-scheme: dark;
  --bg: #16141b;
  --panel: #1f1c25;
  --raised: #29252f;
  --border: #332f3a;
  --line: #2a2630;
  --ink: #f1ece2;
  --muted: #a49fad;
  --faint: #7d7887;
  --accent: #e8a33d;
  --accent-ink: #1d1b22;
  --good: #6cc08a;
  --out: #ef8a5a;
  --bad: #f07a8a;
  --foil: linear-gradient(115deg, rgba(255,0,128,.28), rgba(255,200,0,.28), rgba(0,220,255,.28), rgba(140,0,255,.28));
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.app {
  max-width: 560px; margin: 0 auto; min-height: 100vh; min-height: 100dvh;
  display: flex; flex-direction: column;
}

/* ---------- top bar ---------- */
.appbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 20px 6px;
}
.logo { border-radius: 8px; flex: none; }
.title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.title strong { font-size: 17px; letter-spacing: -0.01em; }
.status { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); min-width: 0; }
.status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status .dot { width: 7px; height: 7px; border-radius: 4px; background: var(--faint); flex: none; }
.status[data-status="live"] .dot { background: var(--good); }
.status[data-status="offline"] .dot, .status[data-status="missing"] .dot { background: var(--bad); }

.seg { display: flex; padding: 2px; border-radius: 9px; background: var(--panel); border: 1px solid var(--border); flex: none; }
.seg button {
  border: 0; background: transparent; color: var(--muted); font-size: 12px; font-weight: 600;
  padding: 5px 10px; border-radius: 7px;
}
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

.menu-wrap { position: relative; flex: none; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 17px; border: 1px solid var(--border); background: transparent;
  color: var(--muted); font-size: 11px; letter-spacing: 1px; display: flex; align-items: center; justify-content: center;
}
.menu {
  position: absolute; right: 0; top: 40px; z-index: 20; min-width: 210px; padding: 6px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 16px 40px rgba(0,0,0,.45); display: flex; flex-direction: column;
}
.menu button {
  text-align: left; border: 0; background: transparent; padding: 10px 12px; border-radius: 9px; font-size: 15px;
}
.menu button:hover, .menu button:focus-visible { background: var(--raised); outline: none; }
.menu button.danger { color: var(--bad); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

/* ---------- summary ---------- */
.summary {
  margin: 10px 16px 0; padding: 16px; border-radius: 20px; background: var(--panel); border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 12px;
}
.totals { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.total { display: flex; flex-direction: column; min-width: 0; }
.total.theirs { align-items: flex-end; text-align: right; }
.total .label { font-size: 12px; color: var(--muted); }
.total .amount { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.total.theirs .amount { color: var(--accent); }
.ratio { display: flex; height: 8px; gap: 3px; }
.ratio div { flex-basis: 0; border-radius: 4px; min-width: 0; transition: flex-grow .3s; }
.ratio .mine { background: var(--ink); }
.ratio .theirs { background: var(--accent); }
.ratio.empty div { background: var(--raised); }
.verdict { font-size: 17px; font-weight: 700; padding-top: 2px; }
.verdict .amt { color: var(--accent); font-variant-numeric: tabular-nums; }
.verdict .diff-note { color: var(--muted); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- tabs ---------- */
.tabs { display: flex; margin: 16px 16px 0; border-bottom: 1px solid var(--border); }
.tab {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; padding: 8px 4px 10px;
  background: transparent; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab .tab-name { font-size: 15px; font-weight: 600; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab .tab-sub { font-size: 12px; color: var(--faint); }
.tab[aria-selected="true"] { border-bottom-color: var(--ink); }
.tab[aria-selected="true"] .tab-name { font-weight: 700; color: var(--ink); }
.tab[aria-selected="true"] .tab-sub { color: var(--muted); }

/* ---------- cards ---------- */
.card-list { list-style: none; margin: 0; padding: 0 16px; flex: 1; }
.card-row {
  display: grid; grid-template-columns: 1fr auto; gap: 0 12px; padding: 12px 0; border-bottom: 1px solid var(--line);
}
.row-main { display: flex; gap: 12px; align-items: flex-start; min-width: 0; cursor: pointer; border-radius: 6px; }
.row-main:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.thumb {
  width: 46px; height: 64px; flex: none; border-radius: 4px; position: relative; overflow: hidden;
  background: repeating-linear-gradient(135deg, #2a2630 0 6px, #25212b 6px 12px);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-row.is-foil .thumb::after { content: ""; position: absolute; inset: 0; background: var(--foil); mix-blend-mode: screen; pointer-events: none; }
.info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.info .name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.info .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badges { display: flex; gap: 6px; align-items: center; margin-top: 4px; min-width: 0; }
.pill { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap; }
.pill:empty { display: none; }
.pill.in { color: var(--good); }
.pill.out { color: var(--out); }
.pill.none { color: var(--bad); }
.pill.muted { color: var(--muted); }
.alt { font-size: 11px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.side-col { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.price { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.price.none { color: var(--bad); }
.price.overridden { text-decoration: underline dotted var(--muted); text-underline-offset: 3px; }
.price.loading { color: var(--faint); font-weight: 500; font-size: 13px; }
.stepper { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; color: var(--muted); }
.stepper button { width: 30px; height: 28px; border: 0; background: transparent; color: var(--muted); font-size: 15px; }
.stepper button:disabled { opacity: .35; cursor: default; }
.stepper .qty { min-width: 20px; text-align: center; color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.card-row.readonly .stepper { border-color: transparent; }
.card-row.readonly .stepper button { display: none; }
.card-row.readonly .stepper .qty::before { content: "×"; color: var(--faint); margin-right: 1px; }

.details { grid-column: 1 / -1; padding: 10px 0 2px 58px; display: flex; flex-direction: column; gap: 10px; }
.note { margin: 0; font-size: 12px; color: var(--muted); }
.note:empty { display: none; }
.links { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; }
.edit { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.edit label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); flex: 1; min-width: 150px; }
.edit .field { height: 40px; }
.remove {
  height: 40px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--border); background: transparent; color: var(--bad); font-weight: 600;
}
.card-row.readonly .edit { display: none; }

.empty { color: var(--faint); text-align: center; padding: 28px 12px; font-size: 14px; }

/* ---------- dock ---------- */
.dock {
  position: sticky; bottom: 0; z-index: 10; margin-top: 12px;
  padding: 12px 16px calc(14px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 10px;
}
.accept-area { display: flex; flex-direction: column; gap: 8px; }
.accept-area:empty { display: none; }
.download-btn {
  width: 100%; height: 44px; border-radius: 12px; border: 1px solid var(--border); background: var(--panel);
  color: var(--ink); font-weight: 600;
}
.accept-btn {
  width: 100%; height: 50px; border-radius: 14px; border: 0; background: var(--accent); color: var(--accent-ink);
  display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 16px; font-weight: 700;
}
.accept-btn .sub { font-size: 13px; font-weight: 600; opacity: .75; }
.accept-btn:disabled { background: var(--raised); color: var(--faint); cursor: default; }
.accept-btn.waiting { background: transparent; border: 1px solid var(--good); color: var(--good); font-size: 15px; font-weight: 600; }
.accept-btn.done { background: var(--good); color: #0f1a13; }

.add-form { display: flex; flex-direction: column; gap: 8px; }
.add-form .row { display: flex; gap: 8px; align-items: center; }
.field {
  height: 44px; border-radius: 12px; background: var(--panel); border: 1px solid var(--border);
  padding: 0 12px; font-size: 16px; color: var(--ink); min-width: 0; width: 100%;
}
.field::placeholder { color: var(--faint); }
.field:focus { outline: none; border-color: var(--accent); }
.field.set { width: 78px; flex: none; }
.field.num { width: 70px; flex: none; }
.field.finish { flex: 1; appearance: none; -webkit-appearance: none; padding-right: 28px;
  background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0h10L5 6z' fill='%237d7887'/%3E%3C/svg%3E") no-repeat right 12px center; }
.field.finish:disabled { color: var(--muted); opacity: 1; }
.add-btn {
  width: 44px; height: 44px; flex: none; border-radius: 12px; background: var(--raised); border: 1px solid var(--border);
  color: var(--ink); font-size: 24px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.add-btn:not(:disabled):hover { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.add-btn:disabled { opacity: .5; cursor: progress; }
.preview { display: flex; gap: 10px; align-items: center; font-size: 13px; color: var(--muted); }
.preview img { width: 36px; border-radius: 3px; }
.preview strong { display: block; font-size: 14px; color: var(--ink); }
.form-msg { margin: 0; font-size: 13px; color: var(--muted); }
.form-msg:empty { display: none; }
.form-msg.error { color: var(--bad); }
.readonly-hint { margin: 0; font-size: 13px; color: var(--faint); text-align: center; }

/* ---------- sheet & toast ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(5,4,8,.6);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; background: var(--panel); border: 1px solid var(--border); border-bottom: 0;
  border-radius: 22px 22px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
}
.sheet h2 { margin: 0; font-size: 19px; }
.sheet p { margin: 0; color: var(--muted); font-size: 14px; }
.sheet label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.sheet .choices { display: flex; gap: 8px; }
.sheet .choice {
  flex: 1; min-width: 0; height: 52px; border-radius: 14px; border: 1px solid var(--border); background: var(--raised);
  font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 10px;
}
.sheet .choice[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.sheet .actions { display: flex; gap: 8px; }
.sheet .primary { flex: 1; height: 50px; border-radius: 14px; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 16px; font-weight: 700; }
.sheet .secondary { height: 50px; padding: 0 18px; border-radius: 14px; border: 1px solid var(--border); background: transparent; font-weight: 600; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 40;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s;
  background: var(--ink); color: var(--accent-ink); font-weight: 600; font-size: 14px; padding: 10px 16px; border-radius: 999px;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
