:root {
  --bg: #f5f6fa; --surface: #ffffff; --surface-2: #f0f1f6; --line: #e3e5ee;
  --text: #1c1e29; --dim: #6b7085; --accent: #7c3aed; --accent-2: #a855f7; --on-accent: #fff;
  --good: #16a34a; --warn: #d97706; --bad: #dc2626; --trial: #0891b2;
  --shadow: 0 1px 2px rgba(20, 22, 40, .06), 0 4px 16px rgba(20, 22, 40, .05);
  --radius: 16px; --radius-sm: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1117; --surface: #181b24; --surface-2: #20242f; --line: #2a2f3c;
    --text: #e8e9f0; --dim: #9196a8; --accent: #a78bfa; --accent-2: #c084fc; --on-accent: #14101f;
    --good: #4ade80; --warn: #fbbf24; --bad: #f87171; --trial: #22d3ee;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1117; --surface: #181b24; --surface-2: #20242f; --line: #2a2f3c;
  --text: #e8e9f0; --dim: #9196a8; --accent: #a78bfa; --accent-2: #c084fc; --on-accent: #14101f;
  --good: #4ade80; --warn: #fbbf24; --bad: #f87171; --trial: #22d3ee;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
/* open dropdown lists: always theme colours (Windows Chrome draws them light otherwise) */
select option, select optgroup { background-color: var(--surface); color: var(--text); }
a { color: var(--accent); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg circle[r="1.6"], svg circle[r="1.4"] { fill: currentColor; stroke: none; }
h1, h2 { margin: 0; }
h2 { font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); margin-bottom: 12px; }
.muted { color: var(--dim); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.grow { flex: 1; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

.app { max-width: 1120px; margin: 0 auto; padding: 16px 16px 120px; padding-top: max(16px, env(safe-area-inset-top)); }

/* top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.brand > div { min-width: 0; }
.brand img { border-radius: 9px; }
.brand h1 { font-size: 1.3rem; font-weight: 750; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p { margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); padding: 9px 16px; border-radius: 999px; font-weight: 600; text-decoration: none;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.98); }
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: var(--on-accent); }
.btn-ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.danger:hover { border-color: var(--bad); }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.icon-btn:hover { border-color: var(--accent); }
.select-pill {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: 0 6px 0 12px; height: 40px;
}
.select-pill select { border: 0; background: transparent; padding: 8px 4px; font-weight: 600; cursor: pointer; outline: none; max-width: 120px; }
.select-pill:focus-within, .search:focus-within { border-color: var(--accent); }

/* layout */
.layout { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 20px; align-items: start; }
.main { min-width: 0; }
.side { min-width: 0; display: flex; flex-direction: column; gap: 14px; position: sticky; top: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }

/* summary */
.summary { background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: var(--on-accent); border: 0; }
.summary .muted { color: inherit; opacity: .8; }
.summary-main { display: flex; flex-direction: column; margin-bottom: 14px; }
.summary-main strong { font-size: 2.1rem; font-weight: 800; letter-spacing: -.02em; line-height: 1.15; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.summary-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.summary-grid div { display: flex; flex-direction: column; min-width: 0; }
.summary-grid b { font-size: 1.05rem; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

/* categories */
.catbar { display: flex; gap: 3px; height: 12px; border-radius: 99px; overflow: hidden; margin-bottom: 12px; }
.catbar span { min-width: 4px; }
.catlist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 7px; }
.catlist li { display: grid; grid-template-columns: 10px 1fr auto 40px; align-items: center; gap: 8px; font-size: 13.5px; }
.catlist i { width: 10px; height: 10px; border-radius: 3px; }
.catlist b { font-weight: 600; font-variant-numeric: tabular-nums; }
.catlist em { font-style: normal; color: var(--dim); text-align: right; font-size: 12.5px; }

/* upcoming */
.upcoming { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.upcoming li { display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "av name price" "av when price"; column-gap: 10px; align-items: center; padding: 6px; margin: 0 -6px; border-radius: var(--radius-sm); cursor: pointer; }
.upcoming li:hover { background: var(--surface-2); }
.upcoming .avatar { grid-area: av; }
.up-name { grid-area: name; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.up-when { grid-area: when; font-size: 12.5px; color: var(--dim); }
.upcoming li > b, .upcoming li > .tag { grid-area: price; font-variant-numeric: tabular-nums; font-size: 14px; }
.upcoming li.soon .up-when { color: var(--warn); font-weight: 600; }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.search { display: flex; align-items: center; gap: 8px; flex: 1 1 180px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 0 14px; height: 40px; }
.search svg { color: var(--dim); width: 18px; height: 18px; }
.search input { border: 0; background: transparent; outline: none; width: 100%; height: 100%; }
.chips { display: flex; gap: 6px; min-width: 0; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 13px; cursor: pointer; font-size: 13.5px; font-weight: 600; white-space: nowrap; color: var(--dim); }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }

/* subscription list */
.subs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.sub { position: relative; display: flex; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); transition: border-color .15s, opacity .15s; }
.sub:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); }
.sub.dim .sub-open { opacity: .62; }
.sub.dragging { z-index: 5; box-shadow: 0 12px 32px rgba(0, 0, 0, .25); border-color: var(--accent); transition: none; }
.handle { display: grid; place-items: center; align-self: stretch; width: 30px; margin-left: 4px; color: var(--dim); cursor: grab; touch-action: none; }
.dragging .handle { cursor: grabbing; }
.sub-open { flex: 1; min-width: 0; display: flex; align-items: center; gap: 14px; padding: 14px 16px; border: 0; background: none; text-align: left; cursor: pointer; border-radius: var(--radius); }
.draggable .sub-open { padding-left: 4px; }
.avatar { --c: var(--accent); flex: none; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; font-weight: 750; font-size: 15px; color: var(--c); background: color-mix(in srgb, var(--c) 16%, transparent); }
.avatar.sm { width: 34px; height: 34px; border-radius: 9px; font-size: 12.5px; }
.sub-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.sub-name { font-weight: 650; font-size: 15.5px; display: flex; align-items: center; gap: 8px; min-width: 0; }
.sub-name .nm { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub-name .tag { flex: none; }
.sub-meta { font-size: 13px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.cat-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.next { font-size: 12.5px; color: var(--dim); }
.next.soon { color: var(--warn); font-weight: 600; }
.sub-price { display: flex; flex-direction: column; align-items: flex-end; text-align: right; flex: none; }
.sub-price b { font-size: 16px; font-variant-numeric: tabular-nums; }
.manage { margin-right: 12px; width: 36px; height: 36px; }
.manage svg { width: 17px; height: 17px; }

.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; white-space: nowrap; letter-spacing: .01em; }
.tag-trial { color: var(--trial); background: color-mix(in srgb, var(--trial) 14%, transparent); }
.tag-paused { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.tag-cancelled { color: var(--dim); background: var(--surface-2); }

.empty { text-align: center; padding: 36px 24px; }
.empty h2 { text-transform: none; letter-spacing: 0; font-size: 1.15rem; color: var(--text); }
.empty p { max-width: 420px; margin: 0 auto 18px; }

/* dialogs */
dialog.sheet { border: 0; padding: 0; border-radius: 20px; width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); background: var(--surface); color: var(--text); box-shadow: 0 24px 64px rgba(0, 0, 0, .35); }
dialog.sheet::backdrop { background: rgba(10, 10, 20, .5); backdrop-filter: blur(2px); }
dialog.sheet[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
.sheet > form, .sheet > div { display: flex; flex-direction: column; max-height: calc(100dvh - 32px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px; }
.sheet-head h2 { text-transform: none; letter-spacing: 0; font-size: 1.15rem; color: var(--text); margin: 0; }
.sheet-head .icon-btn { border: 0; background: var(--surface-2); width: 36px; height: 36px; }
.sheet-body { padding: 8px 18px 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.sheet-body p { margin: 0; }
.sheet-foot { display: flex; align-items: center; gap: 8px; padding: 12px 18px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row > .field { min-width: 120px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--dim); }
.field i { font-weight: 400; font-style: normal; }
.field input, .field select {
  height: 44px; padding: 0 12px; border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--surface-2);
  color: var(--text); font-size: 16px; font-weight: 500; outline: none; width: 100%;
}
.field input:focus, .field select:focus { border-color: var(--accent); background: var(--surface); }
.inline { display: flex; gap: 6px; }
.inline input { width: 70px; flex: none; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 8px 4px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--dim); }
.seg input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.form-error { color: var(--bad); font-weight: 600; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.stack .btn { justify-content: flex-start; border-radius: var(--radius-sm); }
hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 14px; max-width: calc(100vw - 32px);
  background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); font-weight: 500;
  animation: pop .18s ease-out;
}
.toast-btn { border: 0; background: none; color: var(--accent-2); font-weight: 700; cursor: pointer; padding: 4px; }

/* floating add button (phone) */
.fab {
  position: fixed; right: 18px; bottom: max(20px, env(safe-area-inset-bottom)); z-index: 20;
  width: 58px; height: 58px; border-radius: 18px; border: 0; cursor: pointer; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--on-accent); box-shadow: 0 10px 28px color-mix(in srgb, var(--accent) 45%, transparent);
}
.fab svg { width: 26px; height: 26px; stroke-width: 2.4; }
.show-phone { display: none; }

@media (max-width: 860px) {
  /* one column: totals, the list, coming up, categories */
  .layout { display: flex; flex-direction: column; align-items: stretch; gap: 14px; }
  .side { display: contents; }
  .summary { order: 0; }
  .main { order: 1; margin-top: 6px; }
  #cat-card { order: 3; }
  #up-card { order: 2; }
}
@media (max-width: 600px) {
  .app { padding-left: 12px; padding-right: 12px; }
  .hide-phone { display: none !important; }
  .show-phone { display: grid; }
  .brand h1 { font-size: 1.15rem; }
  .brand img { width: 28px; height: 28px; }
  .topbar { gap: 8px; }
  .topbar-actions { gap: 6px; }
  .icon-btn { width: 38px; height: 38px; }
  .select-pill { height: 38px; }
  .select-pill { padding-left: 8px; }
  .select-pill select { max-width: none; }
  .brand p { display: none; }
  .toolbar .select-pill { order: 3; }
  .chips { order: 2; width: 100%; }
  .sub-open { gap: 12px; padding: 12px; }
  .avatar { width: 40px; height: 40px; }
  .summary-main strong { font-size: 1.85rem; }
  /* dialogs become bottom sheets */
  dialog.sheet { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 20px 20px 0 0; max-height: 92dvh; }
  .sheet > form, .sheet > div { max-height: 92dvh; }
  dialog.sheet[open] { animation: up .22s ease-out; }
  .seg { grid-template-columns: repeat(2, 1fr); }
  .toast { bottom: max(90px, calc(env(safe-area-inset-bottom) + 80px)); }
}
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
