/* SubLens: private subscription statement */

:root {
  --paper:      #F4F5F2;
  --sheet:      #FFFFFF;
  --tint:       #F2F4F0;
  --ink:        #14213D;
  --ink-2:      #44506A;
  --ink-3:      #5F6A80;
  --rule:       #D6DCD4;
  --rule-soft:  #E7EBE5;
  --green:      #2F6F4E;
  --green-hi:   #255A3F;
  --red:        #B33A3A;
  --red-wash:   #F8E9E7;
  --amber:      #8A5A00;
  --amber-wash: #F6EEDC;
  --shadow:     0 1px 2px rgba(20, 33, 61, 0.08);
  --font: -apple-system, BlinkMacSystemFont, "PingFang HK", "PingFang TC", "Noto Sans TC",
          "Microsoft JhengHei", "Segoe UI", sans-serif;
  color-scheme: light;
}

.dark {
  --paper:      #080E1B;
  --sheet:      #0F1729;
  --tint:       #131D33;
  --ink:        #E7EBF3;
  --ink-2:      #B3BCCD;
  --ink-3:      #8E98AD;
  --rule:       #27324A;
  --rule-soft:  #1B2540;
  --green:      #79C59B;
  --green-hi:   #95D4B0;
  --red:        #EC8B83;
  --red-wash:   #2E1719;
  --amber:      #E3B566;
  --amber-wash: #2A2213;
  --shadow:     0 1px 2px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.55 var(--font);
  font-feature-settings: "tnum" 1, "lnum" 1;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--green); color: var(--sheet); }

:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; border-radius: 3px; }

h1, h2, h3, p { margin: 0; }

button { font: inherit; color: inherit; cursor: pointer; }

.hidden { display: none !important; }
.flex   { display: flex; }

.min-w { min-width: 0; }

.num { font-variant-numeric: tabular-nums lining-nums; letter-spacing: -0.01em; }

.icon {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ── Buttons ───────────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 16px;
  border: 1px solid transparent; border-radius: 4px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
  transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.btn-primary { background: var(--green); color: var(--sheet); }
.btn-primary:hover { background: var(--green-hi); }
.btn-ghost { border-color: var(--rule); background: var(--sheet); color: var(--ink); }
.btn-ghost:hover { border-color: var(--ink-3); }
.btn-danger { background: var(--red); color: var(--sheet); }
.btn-danger:hover { filter: brightness(.92); }
.btn-text { background: none; padding: 0 6px; min-height: 28px; color: var(--green); font-weight: 600; }
.btn-text:hover { text-decoration: underline; text-underline-offset: 3px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; border: 0; border-radius: 4px;
  background: transparent; color: var(--ink-2);
  transition: background-color .15s ease, color .15s ease;
}
.icon-btn:hover { background: var(--tint); color: var(--ink); }
.icon-btn.sm { width: 32px; height: 32px; }
.icon-btn.danger:hover { color: var(--red); background: var(--red-wash); }

/* ── Letterhead ────────────────────────────────────────── */

.letterhead {
  position: sticky; top: 0; z-index: 40;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--rule);
}
.letterhead-inner {
  max-width: 1440px; margin: 0 auto; padding: 10px 32px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.wordmark { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.wordmark-name { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; color: var(--ink); }
.wordmark-name span { color: var(--green); }
.letterhead-actions { display: flex; align-items: center; gap: 4px; }
.letterhead-actions .btn { margin-left: 8px; }

.theme-sun { display: none; }
.dark .theme-sun  { display: block; }
.dark .theme-moon { display: none; }

/* ── Demo strip ────────────────────────────────────────── */

.strip {
  max-width: 1440px; margin: 16px auto 0; padding: 0 32px;
}
.strip-inner {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px;
  padding: 8px 14px; border: 1px dashed var(--ink-3); border-radius: 4px;
  font-size: 13px; color: var(--ink-2);
}
.strip-inner strong { color: var(--ink); }
.strip-actions { display: flex; gap: 4px; }

/* ── Page and panels ───────────────────────────────────── */

.page {
  max-width: 1440px; margin: 0 auto; padding: 16px 32px 48px;
  display: grid; gap: 16px;
}

.panel {
  background: var(--sheet);
  border: 1px solid var(--rule-soft);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.section { padding: 20px 24px 16px; }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  margin-bottom: 16px;
}
.section-head:has(+ .tabs) { margin-bottom: 4px; }
.section-title { font-size: 18px; font-weight: 700; color: var(--ink); }
.section-note  { font-size: 12px; color: var(--ink-3); }

/* Statement head: account details + monthly spend */
.head-grid {
  display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); gap: 16px;
}

.account { margin: 0; padding: 12px 24px; display: grid; align-content: center; }
.account-row {
  display: grid; grid-template-columns: 132px minmax(0, 1fr); gap: 12px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--rule-soft);
  font-size: 14px;
}
.account-row:last-child { border-bottom: 0; }
.account-row dt { color: var(--ink-2); }
.account-row dd { margin: 0; color: var(--ink); display: flex; flex-wrap: wrap; align-items: center; gap: 4px 14px; font-weight: 500; }
.rate { white-space: nowrap; }
.rate b { font-weight: 700; }
#ratesBar .icon-btn.sm { margin: -6px -8px -6px -4px; }
.rate-meta { color: var(--ink-3); font-size: 13px; font-weight: 400; }

.due-panel { padding: 6px; }
.due {
  height: 100%;
  padding: 16px 18px 14px;
  background: var(--tint);
  border-radius: 6px;
}
.due-label { font-size: 14px; font-weight: 600; color: var(--ink); }
.due-amount {
  margin-top: 4px;
  display: flex; align-items: baseline; gap: 10px;
  font-size: clamp(40px, 4.2vw, 56px); font-weight: 800; line-height: 1.05;
  letter-spacing: -0.03em; color: var(--ink);
}
.due-amount .ccy { font-size: 26px; font-weight: 800; letter-spacing: 0; }
.due-split {
  display: grid; grid-template-columns: 1fr 1fr;
  margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--rule);
}
.due-split > div { padding-right: 16px; }
.due-split > div + div { padding: 0 0 0 24px; border-left: 1px solid var(--rule); }
.due-sub { font-size: 13px; color: var(--ink-2); }
.due-fig { margin-top: 4px; font-size: 18px; font-weight: 700; color: var(--ink); white-space: nowrap; }
.due-fig small { font-size: 12px; font-weight: 400; color: var(--ink-3); }
.due-warn { margin-top: 10px; padding: 6px 10px; border-radius: 4px; font-size: 13px; color: var(--amber); background: var(--amber-wash); }

/* Next debit line */
.next-debit {
  display: flex; flex-wrap: wrap; align-items: center;
  padding: 12px 24px; font-size: 14px; color: var(--ink-2);
}
.next-debit > span + span::before { content: "·"; margin: 0 10px; color: var(--ink-3); }
.next-debit-tag { display: inline-flex; align-items: center; gap: 10px; color: var(--ink-2); }
.next-debit-tag::before {
  content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--green);
}
.next-debit.is-soon .next-debit-tag::before { background: var(--red); }
.next-debit.is-soon .next-debit-tag { color: var(--red); font-weight: 600; }

/* Category subtotals */
.split-bar {
  display: flex; height: 12px; gap: 2px; margin-bottom: 12px;
}
.split-bar span { display: block; height: 100%; min-width: 4px; border-radius: 2px; }

.ledger-table, .subtotal-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.subtotal-table th, .subtotal-table td { padding: 8px 0; border-bottom: 1px solid var(--rule-soft); text-align: left; }
.subtotal-table th { font-size: 13px; font-weight: 500; color: var(--ink-2); }
.subtotal-table .r { text-align: right; }
.subtotal-table td.num, .subtotal-table th.r { padding-left: 16px; }
.subtotal-table td:first-child { color: var(--ink); }
.subtotal-table tbody td.num { color: var(--ink-2); }
.subtotal-table tfoot td { border-bottom: 0; font-weight: 700; color: var(--ink); }
.subtotal-table .swatch { width: 14px; height: 14px; border-radius: 50%; margin-right: 12px; vertical-align: -2px; }
.swatch { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 10px; }

.empty-note { padding: 20px 0; color: var(--ink-3); font-size: 14px; }

/* Ledger */
.tabs {
  display: flex; gap: 12px; margin-bottom: 0;
  border-bottom: 1px solid var(--rule);
  overflow-x: auto; scrollbar-width: none;
}
.tab-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 14px; margin-bottom: -1px;
  background: none; border: 0; border-bottom: 2px solid transparent;
  font-size: 14px; font-weight: 600; color: var(--ink-3); white-space: nowrap;
}
.tab-btn:hover { color: var(--ink); }
.tab-count { margin-left: 2px; }
.tab-btn.is-active { color: var(--green); border-bottom: 3px solid var(--green); }
.tab-count { font-size: 12px; font-weight: 600; color: var(--ink-3); }
.tab-btn.is-active .tab-count { color: var(--green); }

.ledger-table th {
  font-size: 13px; font-weight: 500; color: var(--ink-2);
  text-align: left; padding: 12px 10px 8px; border-bottom: 1px solid var(--rule);
  white-space: nowrap;
}
.ledger-table td { padding: 6px 10px; border-bottom: 1px solid var(--rule-soft); vertical-align: middle; }
.ledger-table th:first-child, .ledger-table td:first-child { padding-left: 0; }
.ledger-table th:last-child,  .ledger-table td:last-child  { padding-right: 0; }
.ledger-table .r { text-align: right; }
.ledger-table tbody tr { transition: background-color .15s ease; }
.ledger-table tbody tr:hover { background: var(--tint); }

.item { display: flex; align-items: center; gap: 12px; min-width: 0; }
.item-emoji {
  flex: none; width: 34px; height: 34px; display: grid; place-items: center;
  font-size: 18px; border: 1px solid var(--rule); border-radius: 6px; background: var(--sheet);
}
.item-name { font-weight: 600; color: var(--ink); overflow-wrap: anywhere; }
.item-note { font-size: 12px; color: var(--ink-3); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }

.cat { display: inline-flex; align-items: center; font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.cat .swatch { margin-right: 6px; }

.cycle { font-size: 13px; color: var(--ink-2); white-space: nowrap; }

.orig { white-space: nowrap; color: var(--ink-2); }
.orig .code { font-size: 12px; color: var(--ink-3); margin-right: 4px; }

.monthly { white-space: nowrap; }
.monthly-figure { font-weight: 700; color: var(--ink); }
.col-ops { width: 140px; }
.actions { justify-content: flex-start; gap: 14px; }

.next { white-space: nowrap; font-size: 13px; color: var(--ink-2); }
.next.is-soon { color: var(--red); font-weight: 600; }

.status {
  display: inline-block; font-size: 12px; font-weight: 600;
  padding: 1px 6px; border-radius: 3px; border: 1px solid currentColor;
}
.status.paused   { color: var(--amber); }
.status.archived { color: var(--ink-3); }

.row-paused .item-emoji, .row-archived .item-emoji, .row-paused .swatch, .row-archived .swatch { opacity: .5; }
.row-paused .item-name, .row-archived .item-name,
.row-paused .monthly-figure, .row-archived .monthly-figure { color: var(--ink-2); font-weight: 600; }
.row-archived .monthly-figure { text-decoration: line-through; text-decoration-thickness: 1px; }

.actions { display: flex; }

.empty-state { padding: 48px 0 24px; text-align: center; }
.empty-state p:first-child { font-weight: 700; font-size: 16px; color: var(--ink); }
.empty-state p + p { margin-top: 4px; font-size: 14px; color: var(--ink-3); }
.empty-state .btn { margin-top: 16px; }

.fine-print { padding-top: 12px; font-size: 12px; color: var(--ink-3); }

/* ── Overlays, dialogs, toasts ─────────────────────────── */

.loading {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: var(--paper); color: var(--ink-3); font-size: 14px;
}
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--rule); border-top-color: var(--green);
  animation: spin .8s linear infinite;
}
.spinner.sm { width: 16px; height: 16px; }
@keyframes spin { to { transform: rotate(360deg); } }

.api-spinner {
  position: fixed; right: 20px; bottom: 20px; z-index: 55;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: 4px;
  background: var(--sheet); border: 1px solid var(--rule); box-shadow: var(--shadow);
  font-size: 13px; color: var(--ink-2);
}

.toasts {
  position: fixed; top: 72px; right: 20px; z-index: 70;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  pointer-events: auto;
  min-width: 220px; max-width: 360px;
  padding: 10px 14px; border-radius: 4px;
  background: var(--ink); color: var(--sheet);
  font-size: 14px; font-weight: 500; box-shadow: var(--shadow);
  display: flex; gap: 10px; align-items: flex-start;
  animation: toastIn .35s cubic-bezier(.16, 1, .3, 1);
}
.toast.error { background: var(--red); }
.dark .toast { background: var(--tint); color: var(--ink); border: 1px solid var(--rule); }
.dark .toast.error { background: var(--red-wash); color: var(--red); border-color: var(--red); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-6px); } }

.overlay {
  position: fixed; inset: 0; z-index: 50;
  align-items: center; justify-content: center; padding: 16px;
  background: rgba(8, 14, 27, 0.48);
}
.modal-enter { animation: fadeIn .2s ease forwards; }
.modal-enter .dialog { animation: rise .35s cubic-bezier(.16, 1, .3, 1); }
@keyframes fadeIn { from { opacity: 0; } }
@keyframes rise   { from { opacity: 0; transform: translateY(12px); } }

.dialog {
  width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto;
  background: var(--sheet); color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 0;
}
.dialog.sm { max-width: 400px; }
.dialog-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 20px; border-bottom: 1px solid var(--rule);
}
.dialog-title { font-size: 17px; font-weight: 700; }
.dialog-body { padding: 20px; display: grid; gap: 16px; }
.dialog-body > p { font-size: 14px; color: var(--ink-2); }
.dialog-foot { display: flex; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--rule); background: var(--tint); }
.dialog-foot .btn { flex: 1; }
.dialog-aside { padding: 14px 20px 18px; border-top: 1px solid var(--rule); font-size: 13px; color: var(--ink-3); display: grid; gap: 8px; }

.field { display: grid; gap: 6px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-row.emoji { grid-template-columns: 1fr 88px; }
.label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.label .req { color: var(--red); margin-left: 2px; }
.input {
  width: 100%; min-height: 40px; padding: 8px 10px;
  border: 1px solid var(--rule); border-radius: 4px;
  background: var(--sheet); color: var(--ink);
  font: inherit; font-size: 15px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.input::placeholder { color: var(--ink-3); }
.input:hover { border-color: var(--ink-3); }
.input:focus { outline: none; border-color: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 22%, transparent); }
textarea.input { resize: vertical; min-height: 64px; }
select.input { appearance: none; padding-right: 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235F6A80' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; }
.input.center { text-align: center; font-size: 20px; }

.choice {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px; border: 1px solid var(--rule); border-radius: 4px;
  cursor: pointer; transition: border-color .15s ease, background-color .15s ease;
}
.choice:hover { border-color: var(--ink-3); }
.choice.is-selected { border-color: var(--ink); background: var(--tint); }
.choice input { margin-top: 3px; accent-color: var(--ink); }
.choice-title { font-size: 14px; font-weight: 600; }
.choice-desc { font-size: 13px; color: var(--ink-3); }
.choice.danger.is-selected { border-color: var(--red); background: var(--red-wash); }

/* ── Responsive ────────────────────────────────────────── */

@media (max-width: 960px) {
  .head-grid { grid-template-columns: 1fr; }
  .due-panel { order: -1; }
}

@media (max-width: 720px) {
  body { font-size: 14px; }
  .letterhead-inner { padding: 8px 16px; }
  .letterhead-actions .btn { padding: 0 12px; }
  .strip { padding: 0 16px; margin-top: 12px; }
  .page { padding: 12px 16px 32px; gap: 12px; }
  .section { padding: 16px; }
  .account { padding: 6px 16px; }
  .account-row { grid-template-columns: 76px minmax(0, 1fr); }
  .next-debit { padding: 12px 16px; gap: 2px 12px; }
  .next-debit > span + span::before { content: none; }
  .due-split > div + div { padding-left: 16px; }
  .due-fig { font-size: 16px; }
  .due-fig small { display: block; }

  .col-cycle, .col-cat, .col-orig, .col-next { display: none; }
  .ledger-table thead { display: none; }
  .ledger-table, .ledger-table tbody { display: block; }
  .ledger-table tbody tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "item monthly" "meta actions";
    gap: 2px 12px; padding: 10px 0; border-bottom: 1px solid var(--rule-soft);
  }
  .ledger-table tbody td { padding: 0; border: 0; }
  .ledger-table td.cell-item { grid-area: item; }
  .ledger-table td.cell-monthly { grid-area: monthly; }
  .ledger-table td.cell-meta { grid-area: meta; display: block !important; padding-left: 46px; font-size: 12px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; align-self: center; }
  .ledger-table .actions { gap: 0; }
  .ledger-table .actions .icon-btn.sm { width: 30px; height: 30px; }
  .ledger-table td.cell-actions { grid-area: actions; }
  .item-note { max-width: 180px; }
  .toasts { left: 16px; right: 16px; top: 64px; }
  .toast { max-width: none; }
}

@media (min-width: 721px) {
  .cell-meta { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
