/* ==========================================================================
   Ledger — design tokens
   Ink-and-brass ledger book: dark bottle-green ground, brass rule lines,
   green ink for credits, rust-red ink for debits (the old bookkeeper's
   convention of "red ink" for a loss). Tabular monospace for every figure.
   ========================================================================== */
:root {
  --ink:        #0E1712;
  --surface:    #16241C;
  --surface-2:  #1D3226;
  --hairline:   #2E4536;
  --brass:      #C9A64A;
  --brass-dim:  #8A733A;
  --paper:      #F1EDE1;
  --text:       #ECE7D8;
  --text-dim:   #A7B5A9;
  --credit:     #4FAE7A;
  --credit-bg:  rgba(79, 174, 122, 0.12);
  --debit:      #C1533B;
  --debit-bg:   rgba(193, 83, 59, 0.12);

  --font-display: "Source Serif 4", Georgia, serif;
  --font-body:    "Inter", system-ui, sans-serif;
  --font-mono:    "IBM Plex Mono", ui-monospace, monospace;

  --radius: 14px;
  --radius-sm: 8px;
  --shadow-lift: 0 8px 24px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

.is-hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------------- ledger mark (signature element) ---------------- */
.ledger-mark {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 46px;
  margin: 0 auto 18px;
}
.ledger-mark.small { width: 26px; gap: 4px; margin: 0; }
.ledger-mark .line {
  height: 3px;
  border-radius: 2px;
  background: var(--credit);
}
.ledger-mark .line.debit { background: var(--debit); width: 70%; }
.ledger-mark.small .line { height: 2px; }

/* ---------------- auth screen ---------------- */
.auth-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px 48px;
  text-align: center;
}

.brand {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 2.1rem;
  letter-spacing: 0.01em;
  margin: 0 0 6px;
  color: var(--paper);
}
.brand.small { font-size: 1.15rem; margin: 0; }

.tagline {
  color: var(--text-dim);
  font-size: 0.95rem;
  margin: 0 0 28px;
  max-width: 320px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lift);
  overflow: hidden;
  text-align: left;
}

.auth-tabs {
  display: flex;
  border-bottom: 1px solid var(--hairline);
}
.auth-tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 14px 8px;
  border-bottom: 2px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.auth-tab.is-active {
  color: var(--brass);
  border-bottom-color: var(--brass);
}

.auth-form {
  padding: 22px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-dim);
  font-weight: 500;
}
.field input {
  background: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  color: var(--paper);
  font-size: 0.95rem;
  font-family: var(--font-mono);
  outline: none;
  transition: border-color 0.15s ease;
}
.field input:focus-visible {
  border-color: var(--brass);
  outline: 2px solid var(--brass-dim);
  outline-offset: 1px;
}
.field input::placeholder { color: #55665A; }

.hint {
  font-size: 0.78rem;
  color: var(--text-dim);
  line-height: 1.4;
  margin: -4px 0 0;
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-weight: 600;
  font-size: 0.9rem;
  transition: transform 0.1s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--brass);
  color: #1B1607;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-block { width: 100%; }
.btn-ghost {
  background: transparent;
  border: 1px solid var(--hairline);
  color: var(--text);
}
.btn-small { padding: 8px 14px; font-size: 0.8rem; }

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

.plate {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--hairline);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 5;
}
.plate-left { display: flex; align-items: center; gap: 10px; }
.plate-right { display: flex; align-items: center; gap: 10px; }

.sync-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.72rem;
  color: var(--text-dim);
  font-weight: 500;
}
.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--debit);
}
.dot.is-online { background: var(--credit); }
.dot.is-syncing {
  background: var(--brass);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.user-chip {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--hairline);
  color: var(--paper);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-menu {
  position: absolute;
  right: 18px;
  top: 64px;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 14px;
  min-width: 200px;
  box-shadow: var(--shadow-lift);
  z-index: 6;
}
.user-menu-name { margin: 0; font-weight: 600; color: var(--paper); }
.user-menu-email { margin: 2px 0 12px; font-size: 0.78rem; color: var(--text-dim); }

.ledger-body {
  flex: 1;
  padding: 18px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.balance-card {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  padding: 22px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.balance-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
}
.balance-amount {
  font-family: var(--font-mono);
  font-size: 2.6rem;
  font-weight: 600;
  color: var(--paper);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.balance-split {
  display: flex;
  gap: 18px;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 4px;
}
.balance-split b {
  font-family: var(--font-mono);
  font-weight: 600;
}
.split.credit b { color: var(--credit); }
.split.debit b { color: var(--debit); }

.section-title {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0 0 12px;
  color: var(--paper);
}

.add-card, .tape-card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  padding: 18px 16px 20px;
}

.type-toggle {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}
.type-btn {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--hairline);
  background: var(--ink);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.85rem;
  transition: all 0.15s ease;
}
.type-btn[data-type="credit"].is-active {
  background: var(--credit-bg);
  border-color: var(--credit);
  color: var(--credit);
}
.type-btn[data-type="debit"].is-active {
  background: var(--debit-bg);
  border-color: var(--debit);
  color: var(--debit);
}

.txn-form { display: flex; flex-direction: column; gap: 14px; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.form-actions { display: flex; flex-direction: column; gap: 10px; }

.category-filter {
  margin: 0;
  flex: 1;
  min-width: 0;
}
.category-filter select {
  background: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  color: var(--paper);
  font-size: 0.85rem;
  font-family: var(--font-body);
  outline: none;
  width: 100%;
}
.category-filter select:focus-visible {
  border-color: var(--brass);
  outline: 2px solid var(--brass-dim);
  outline-offset: 1px;
}

.tape-controls {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  margin: 2px 0 14px;
  flex-wrap: wrap;
}
.print-btn {
  flex-shrink: 0;
  white-space: nowrap;
}

.category-summary {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  background: var(--ink);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 0.82rem;
  color: var(--text-dim);
}
.category-summary b {
  font-family: var(--font-mono);
  font-weight: 600;
}
.category-summary .net b { color: var(--paper); }
.category-summary .cs-credit b { color: var(--credit); }
.category-summary .cs-debit b { color: var(--debit); }

.print-header { display: none; }

.tape-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.entry-count {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-family: var(--font-mono);
}

.tape { display: flex; flex-direction: column; }

.tape-date-group {
  padding-top: 10px;
}
.tape-date {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--brass-dim);
  font-weight: 600;
  margin: 12px 0 4px;
}

.tape-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px dashed var(--hairline);
  animation: rowIn 0.25s ease;
}
.tape-row:last-child { border-bottom: none; }
@keyframes rowIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.tape-row-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tape-desc {
  font-size: 0.9rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tape-meta {
  font-size: 0.72rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}
.tape-meta .unsynced {
  color: var(--brass);
}

.tape-row-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.tape-actions {
  display: flex;
  gap: 4px;
}
.tape-action-btn {
  background: none;
  border: 1px solid var(--hairline);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  line-height: 1;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.tape-action-btn:hover { color: var(--paper); border-color: var(--brass-dim); }
.tape-action-btn.danger:hover { color: var(--debit); border-color: var(--debit); }

.add-card.is-editing {
  border-color: var(--brass);
}

.tape-amount {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.98rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.tape-amount.credit { color: var(--credit); }
.tape-amount.debit { color: var(--debit); }

.empty-state {
  color: var(--text-dim);
  font-size: 0.88rem;
  text-align: center;
  padding: 20px 10px 4px;
  line-height: 1.5;
}

/* ---------------- banner (persistent, unlike the toast) ---------------- */
.banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 60;
  background: var(--debit-bg);
  border-bottom: 1px solid var(--debit);
  color: var(--paper);
  padding: 10px 16px;
  font-size: 0.82rem;
  line-height: 1.4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.banner span { flex: 1; }
.banner button {
  background: none;
  border: none;
  color: var(--paper);
  font-size: 1.2rem;
  line-height: 1;
  padding: 0 4px;
  flex-shrink: 0;
}

/* ---------------- toast ---------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2);
  border: 1px solid var(--hairline);
  color: var(--paper);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 0.85rem;
  box-shadow: var(--shadow-lift);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
  max-width: 88vw;
  text-align: center;
}
.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------------- desktop / wider viewports ---------------- */
@media (min-width: 720px) {
  .app-screen {
    max-width: 640px;
    margin: 24px auto 48px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lift);
    min-height: auto;
  }
  .plate { border-radius: var(--radius) var(--radius) 0 0; }
  .ledger-body { padding: 24px 28px 36px; }
  .field-row { gap: 16px; }
}

@media (max-width: 380px) {
  .balance-amount { font-size: 2.1rem; }
  .field-row { flex-direction: column; gap: 14px; }
}

/* ==========================================================================
   Print — "Save as PDF" via the browser's print dialog. Optimised for ink:
   white background, black text, only the balance + tape are shown (the
   entry form, nav, and per-row action buttons are irrelevant on paper).
   Respects whatever category filter is active on screen.
   ========================================================================== */
@media print {
  body { background: #fff; color: #111; }
  .auth-screen, .plate, .add-card, .toast, .banner, .user-menu,
  .tape-controls, .tape-actions, .empty-state { display: none !important; }

  .app-screen {
    max-width: 100%;
    margin: 0;
    border: none;
    box-shadow: none;
    display: block;
  }

  .ledger-body { padding: 0; gap: 14px; }

  .print-header {
    display: block;
    border-bottom: 2px solid #111;
    padding-bottom: 10px;
    margin-bottom: 4px;
  }
  .print-header h1 {
    font-family: var(--font-display);
    font-size: 1.6rem;
    margin: 0 0 4px;
    color: #111;
  }
  .print-header p {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: #333;
    margin: 0;
  }

  .balance-card {
    background: #fff;
    border: 1px solid #111;
    box-shadow: none;
  }
  .balance-label { color: #444; }
  .balance-amount { color: #111; }
  .split.credit b { color: #1a6b3f; }
  .split.debit b { color: #9c3a26; }

  .tape-card {
    background: #fff;
    border: none;
    padding: 0;
  }
  .section-title { color: #111; }
  .entry-count { color: #444; }

  .category-summary {
    background: #fff;
    border: 1px solid #111;
    color: #333;
  }
  .category-summary .net b { color: #111; }
  .category-summary .cs-credit b { color: #1a6b3f; }
  .category-summary .cs-debit b { color: #9c3a26; }

  .tape-date { color: #555; }
  .tape-row { border-bottom: 1px dashed #999; break-inside: avoid; }
  .tape-desc { color: #111; }
  .tape-meta { color: #555; }
  .tape-amount.credit { color: #1a6b3f; }
  .tape-amount.debit { color: #9c3a26; }
}
