* { box-sizing: border-box; }

:root {
  --ink: #1c1b19;
  --mute: #837c70;
  --line: #e2ddd0;
  --bg: #f7f4ec;
  --card: #ffffff;
  --accent: #1c1b19;
  --accent-contrast: #f7f4ec;
  --danger: #a3382f;
  --expense: #8a6a4f;
  --radius: 4px;
  --radius-lg: 6px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  --grid-bg:
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='128' height='128'><path d='M0 0 L128 0 M0 0 L0 128' stroke='%231C1B19' stroke-width='0.7' opacity='0.16'/></svg>"),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='32' height='32'><path d='M0 0 L32 0 M0 0 L0 32' stroke='%231C1B19' stroke-width='0.4' opacity='0.08'/></svg>");
}

/* Explicit dark override (from the in-app theme picker) always wins,
   regardless of OS preference. */
:root[data-theme="dark"] {
  --ink: #f0eee8;
  --mute: #93897a;
  --line: #2c2a26;
  --bg: #121110;
  --card: #1b1a17;
  --accent: #f0eee8;
  --accent-contrast: #121110;
  --danger: #e0847f;
  --expense: #c7a883;
  --grid-bg:
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='128' height='128'><path d='M0 0 L128 0 M0 0 L0 128' stroke='%23F0EEE8' stroke-width='0.7' opacity='0.09'/></svg>"),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='32' height='32'><path d='M0 0 L32 0 M0 0 L0 32' stroke='%23F0EEE8' stroke-width='0.4' opacity='0.045'/></svg>");
}

/* OS-level dark preference, only when the user hasn't explicitly picked
   a theme (no "light" or "dark" override sitting on <html>). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --ink: #f0eee8;
    --mute: #93897a;
    --line: #2c2a26;
    --bg: #121110;
    --card: #1b1a17;
    --accent: #f0eee8;
    --accent-contrast: #121110;
    --danger: #e0847f;
    --expense: #c7a883;
    --grid-bg:
      url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='128' height='128'><path d='M0 0 L128 0 M0 0 L0 128' stroke='%23F0EEE8' stroke-width='0.7' opacity='0.09'/></svg>"),
      url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='32' height='32'><path d='M0 0 L32 0 M0 0 L0 32' stroke='%23F0EEE8' stroke-width='0.4' opacity='0.045'/></svg>");
  }
}

html, body { margin: 0; min-height: 100%; }

body {
  font-family: var(--sans);
  color: var(--ink);
  background-color: var(--bg);
  background-image: var(--grid-bg);
  background-size: 128px 128px, 32px 32px;
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select { font-family: inherit; cursor: pointer; }
input, textarea, select { cursor: auto; }

h1, h2 { margin: 0; }

.wordmark {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.view { width: 100%; padding: 24px 20px 72px; }

/* ============ auth ============ */
#auth-view { align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
#auth-view[hidden] { display: none; }
#auth-view:not([hidden]) { display: flex; }
.auth-card {
  width: 100%;
  max-width: 340px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
  text-align: center;
}
.auth-card .sub { color: var(--mute); font-size: 13px; margin: 10px 0 20px; line-height: 1.4; }
.auth-mode-toggle { justify-content: center; margin-bottom: 24px; }

/* ============ pin pad ============ */
.pindots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  min-height: 14px;
  margin-bottom: 24px;
}
.pindot {
  width: 11px; height: 11px; border-radius: 50%;
  border: 1.5px solid var(--line);
  transition: background 0.14s ease, border-color 0.14s ease, transform 0.14s ease;
}
.pindot--on { background: var(--ink); border-color: var(--ink); transform: scale(1.1); }

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 64px);
  justify-content: center;
  gap: 12px;
  margin-bottom: 20px;
}
.key {
  width: 64px; aspect-ratio: 1; border-radius: 50%;
  font-family: var(--serif); font-size: 22px; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line);
  transition: transform 0.08s ease, background 0.14s ease;
}
.key:active { transform: scale(0.93); background: var(--card); }
.key:disabled { opacity: 0.4; }
.key-text {
  font-family: var(--sans) !important; font-size: 12px !important; font-weight: 600;
  color: var(--mute); background: none; border-color: transparent;
}

#pin-continue-btn:disabled { opacity: 0.4; }
#pin-status { min-height: 18px; margin-top: 14px; }
.auth-card input {
  width: 100%;
  text-align: center;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  margin-bottom: 12px;
  font-size: 16px;
}
.auth-card input::placeholder { color: var(--mute); }

.auth-card button[type="submit"], .entry-card button[type="submit"], #pin-continue-btn {
  width: 100%;
  padding: 15px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 15px;
  font-weight: 600;
  min-height: 48px;
  transition: opacity 0.15s ease;
}
.auth-card button[type="submit"]:hover, .entry-card button[type="submit"]:hover, #pin-continue-btn:hover { opacity: 0.85; }

/* ============ app shell ============ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 0 28px;
  max-width: 1120px; margin: 0 auto;
}
.icon-btn {
  width: 40px; height: 40px;
  border: none; background: transparent; color: var(--mute);
  font-size: 20px; border-radius: var(--radius);
}
.icon-btn:hover { background: var(--card); color: var(--ink); }

.panel {
  max-width: 1120px; margin: 0 auto 24px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 22px;
}
.panel h2 { font-size: 13px; font-weight: 600; color: var(--mute); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 16px; }
.panel h3 { font-size: 12px; font-weight: 600; color: var(--mute); text-transform: uppercase; letter-spacing: 0.06em; margin: 20px 0 10px; }
.panel label { display: block; font-size: 13px; color: var(--mute); margin-bottom: 14px; }
.panel input {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px; border: 1px solid var(--line); border-radius: var(--radius);
  background: transparent; color: var(--ink); font-size: 15px;
}
.panel button[type="submit"] {
  padding: 11px 18px; border: none; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-contrast); font-weight: 600; min-height: 44px;
}
.text-btn {
  background: none; border: none; color: var(--danger);
  font-size: 14px; padding: 10px 0; margin-top: 10px; min-height: 44px;
}

.theme-picker { display: flex; gap: 8px; margin-bottom: 4px; }
.theme-option {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: transparent;
  color: var(--mute);
  font-size: 13px;
  min-height: 40px;
}
.theme-option.active { border-color: var(--ink); color: var(--ink); font-weight: 600; }

/* ============ stats row ============ */
.stats-row {
  max-width: 1120px; margin: 0 auto 24px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
.stat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px 22px;
}
.stat-card-net { background: var(--accent); border-color: var(--accent); }
.stat-card-net .stat-label, .stat-card-net .stat-value { color: var(--accent-contrast); }
.stat-label { display: block; font-size: 12px; color: var(--mute); text-transform: uppercase; letter-spacing: 0.06em; }
.stat-value { display: block; font-family: var(--serif); font-size: 30px; font-weight: 500; margin-top: 8px; }

/* ============ period selector ============ */
.period-row {
  max-width: 1120px; margin: 0 auto 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.period-toggle { display: flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.period-toggle button {
  padding: 8px 16px; border: none; background: transparent; color: var(--mute); font-size: 13px; min-height: 36px;
}
.period-toggle button.active { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.period-nav { display: flex; align-items: center; gap: 4px; }
.period-nav button {
  width: 32px; height: 32px; border: 1px solid var(--line); border-radius: var(--radius);
  background: transparent; color: var(--ink); font-size: 14px;
}
.period-nav button:hover { background: var(--card); }
.period-label { font-size: 14px; font-weight: 500; min-width: 110px; text-align: center; }

/* ============ dashboard layout ============ */
.dashboard {
  max-width: 1120px; margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.dashboard h2 { font-size: 13px; font-weight: 600; color: var(--mute); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 14px; }

/* ============ entry form ============ */
.entry-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 24px;
}

.kind-toggle {
  display: flex;
  gap: 24px;
  margin-bottom: 22px;
  border-bottom: 1px solid var(--line);
}
.kind-option {
  background: none;
  border: none;
  padding: 0 0 10px;
  font-size: 15px;
  font-weight: 500;
  color: var(--mute);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  min-height: 44px;
}
.kind-option.active { color: var(--ink); border-bottom-color: var(--ink); font-weight: 600; }

.amount-row {
  display: flex;
  align-items: baseline;
  gap: 4px;
  margin-bottom: 16px;
}
.amount-prefix { font-family: var(--serif); font-size: 32px; color: var(--mute); font-weight: 400; }
#amount-input {
  flex: 1;
  font-family: var(--serif);
  font-size: 40px;
  font-weight: 500;
  border: none;
  background: transparent;
  color: var(--ink);
  width: 100%;
  padding: 0;
}
#amount-input::-webkit-outer-spin-button, #amount-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.entry-card input[type="text"], .entry-card input[type="date"] {
  width: 100%;
  font-size: 15px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  margin-bottom: 12px;
}
.entry-card input::placeholder { color: var(--mute); }

.row { display: flex; gap: 10px; }
.row input { flex: 1; min-width: 0; }

.source-toggle {
  display: flex; gap: 8px; margin-bottom: 16px;
}
.source-option {
  flex: 1; padding: 9px; border: 1px solid var(--line); border-radius: var(--radius);
  background: transparent; color: var(--mute); font-size: 13px; min-height: 40px;
}
.source-option.active { border-color: var(--ink); color: var(--ink); background: var(--bg); font-weight: 600; }

.error { color: var(--danger); font-size: 13px; margin: 10px 0 0; }
.hint { color: var(--mute); font-size: 13px; }

/* ============ actions ============ */
.actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.ghost-btn {
  flex: 1;
  text-align: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  text-decoration: none;
  font-size: 13px;
  background: transparent;
  min-height: 44px;
  min-width: 100px;
  transition: background 0.15s ease;
}
.ghost-btn:hover { background: var(--card); border-color: var(--ink); }
#sync-status { margin-top: 10px; text-align: center; }

/* ============ entry table (desktop) / list (mobile) ============ */
.table-col { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 22px; }
.entry-table-head {
  display: grid;
  grid-template-columns: 84px 1fr 130px 90px 100px 32px;
  gap: 12px;
  padding: 0 8px 10px;
  border-bottom: 1px solid var(--line);
  font-size: 11px;
  color: var(--mute);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.align-right { text-align: right; }

#entry-list { list-style: none; margin: 0; padding: 0; }
.entry-row {
  display: grid;
  grid-template-columns: 84px 1fr 130px 90px 100px 32px;
  align-items: center;
  gap: 12px;
  padding: 14px 8px;
  border-bottom: 1px solid var(--line);
}
.entry-row:last-child { border-bottom: none; }

.cell-date, .cell-cat, .cell-source { font-size: 13px; color: var(--mute); }
.cell-desc { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-amount { font-family: var(--serif); font-size: 15px; text-align: right; white-space: nowrap; }
.cell-amount.expense { color: var(--expense); }
.cell-meta { display: none; }
.cell-delete {
  background: none; border: none; color: var(--mute); font-size: 15px;
  min-height: 32px; min-width: 32px; border-radius: var(--radius);
  justify-self: end;
}
.cell-delete:hover { background: var(--bg); color: var(--danger); }

/* ============ confirm overlay ============ */
.overlay {
  position: fixed; inset: 0; background: rgba(28, 27, 25, 0.45);
  align-items: center; justify-content: center; padding: 20px;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.overlay:not([hidden]) { display: flex; }
.confirm-card {
  background: var(--card); border-radius: var(--radius-lg); padding: 22px;
  max-width: 340px; width: 100%; border: 1px solid var(--line);
}
.confirm-card p { margin: 0 0 18px; font-size: 15px; }
.confirm-actions { display: flex; gap: 10px; }
.danger-btn {
  flex: 1; padding: 12px; border: none; border-radius: var(--radius);
  background: var(--danger); color: #fff; font-weight: 600; min-height: 44px;
}
.confirm-actions .ghost-btn { flex: 1; }

/* ============================================================
   Desktop dashboard: two columns, wordmark scales up, form pins
   ============================================================ */
@media (min-width: 900px) {
  .view { padding: 40px 40px 80px; }
  .wordmark { font-size: 26px; }
  .stat-value { font-size: 34px; }
  .dashboard { flex-direction: row; align-items: flex-start; }
  .form-col { width: 360px; flex: none; position: sticky; top: 40px; }
  .table-col { flex: 1; min-width: 0; }
}

/* ============================================================
   Mobile: stats stack, form/table stack, table collapses to
   a card list with a combined meta line instead of columns
   ============================================================ */
@media (max-width: 899px) {
  .stats-row { grid-template-columns: 1fr; }
  .entry-table-head { display: none; }
  .table-col { padding: 18px; }
  .entry-row {
    grid-template-columns: 1fr auto;
    grid-template-areas: "desc amount" "meta delete";
    row-gap: 4px;
    padding: 14px 4px;
  }
  .cell-date, .cell-cat, .cell-source { display: none; }
  .cell-desc { grid-area: desc; white-space: normal; }
  .cell-amount { grid-area: amount; }
  .cell-meta { grid-area: meta; display: block; font-size: 12px; color: var(--mute); }
  .cell-delete { grid-area: delete; justify-self: end; }
  .period-row { flex-wrap: wrap; }
}
