:root {
  --bg: #efece4;
  --paper: #fffdf7;
  --ink: #161513;
  --muted: #5c5954;
  --line: #e3dfd4;
  --line-strong: #cfc9ba;
  --forest: #1c4A36;
  --forest-press: #153828;
  --cream: #f6f1e4;
  --over: #0f7a4a;
  --over-bg: #e4f5ec;
  --short: #c4312a;
  --short-bg: #fdecea;
  --dock: #161513;
  --dock-text: #f6f1e4;
  --shadow: 0 1px 2px rgba(22, 21, 19, 0.06);
  --radius: 16px;
  --tap: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --dock-h: calc(148px + var(--safe-bottom));
  --font: "SF Pro Text", "SF Pro Display", -apple-system, BlinkMacSystemFont,
    "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.35;
}

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

button, input, textarea { font: inherit; color: inherit; }

.app {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
  padding-bottom: var(--dock-h);
}

/* Header */
.header {
  position: sticky;
  top: 0;
  z-index: 8;
  background: color-mix(in srgb, var(--bg) 92%, white);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: calc(10px + var(--safe-top)) 16px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand .tag {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  border: 1px solid var(--line-strong);
  background: var(--paper);
  border-radius: 14px;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--ink);
  flex-shrink: 0;
  touch-action: manipulation;
}

.icon-btn svg { width: 22px; height: 22px; }

/* Tabs */
.tabs {
  margin: 0 16px 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #e4e0d6;
  border-radius: 14px;
  padding: 4px;
  gap: 4px;
}

.tabs button {
  appearance: none;
  border: 0;
  background: transparent;
  min-height: 44px;
  border-radius: 11px;
  font-weight: 650;
  font-size: 15px;
  cursor: pointer;
  color: var(--muted);
  touch-action: manipulation;
}

.tabs button[aria-selected="true"] {
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow), 0 0 0 1px rgba(22, 21, 19, 0.04);
}

/* Meta fields */
.meta {
  margin: 0 16px 16px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.field {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  min-height: 52px;
  border-top: 1px solid var(--line);
}

.field:first-child { border-top: 0; }

.field label {
  flex: 0 0 118px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.field input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  outline: none;
  font-size: 16px;
  padding: 10px 0;
  font-variant-numeric: tabular-nums;
}

.field input.bank {
  font-weight: 700;
  font-size: 18px;
}

/* Sections */
.section {
  margin: 0 16px 16px;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 4px 8px;
}

.section-head h2 {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--muted);
}

.section-head .sec-total {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 14px;
}

.rows {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.row {
  display: grid;
  grid-template-columns: 74px var(--tap) minmax(64px, 1fr) var(--tap) 78px;
  align-items: center;
  gap: 4px;
  padding: 8px 8px;
  min-height: 64px;
  border-top: 1px solid var(--line);
}

.row:first-child { border-top: 0; }

.row.rare-hidden { display: none; }

.denom {
  padding-left: 6px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
}

.denom .name {
  font-weight: 750;
  font-size: 18px;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.denom .detail {
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.step {
  width: var(--tap);
  height: var(--tap);
  border-radius: 14px;
  border: 1px solid var(--line-strong);
  background: #f4f1e9;
  font-size: 28px;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  user-select: none;
  touch-action: manipulation;
  color: var(--ink);
}

.step.plus {
  background: var(--forest);
  border-color: var(--forest);
  color: #f6f1e4;
}

.step:active { transform: scale(0.96); }
.step.plus:active { background: var(--forest-press); }

.count {
  width: 100%;
  height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  outline: none;
  padding: 0;
  appearance: textfield;
  -moz-appearance: textfield;
}

.count:focus {
  border-color: var(--forest);
  box-shadow: 0 0 0 3px rgba(28, 74, 54, 0.18);
}

.count::-webkit-outer-spin-button,
.count::-webkit-inner-spin-button { appearance: none; margin: 0; }

.subtotal {
  text-align: right;
  padding-right: 6px;
  font-variant-numeric: tabular-nums;
  font-weight: 650;
  font-size: 14px;
  color: var(--muted);
}

.subtotal.has-value { color: var(--ink); }

.more-btn, .linkish {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  min-height: 48px;
  margin-top: 10px;
  border: 0;
  background: transparent;
  color: var(--forest);
  font-weight: 650;
  font-size: 15px;
  cursor: pointer;
  touch-action: manipulation;
}

.clear-btn {
  display: block;
  width: calc(100% - 32px);
  margin: 4px 16px 24px;
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line-strong);
  background: transparent;
  color: var(--muted);
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
}

/* Sticky dock */
.dock {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 430px;
  background: var(--dock);
  color: var(--dock-text);
  padding: 12px 16px calc(14px + var(--safe-bottom));
  z-index: 12;
  box-shadow: 0 -8px 24px rgba(22, 21, 19, 0.25);
}

.dock-total {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.dock-total .lbl {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: #b7b1a4;
}

.dock-total .amt {
  font-size: 32px;
  font-weight: 750;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.dock-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin: 8px 0 12px;
  font-size: 13px;
  font-weight: 600;
  color: #d4cfc3;
  font-variant-numeric: tabular-nums;
}

.dock-meta .status.over { color: #8be0b2; }
.dock-meta .status.short { color: #ffb4ae; }
.dock-meta .status.even { color: #d4cfc3; }

.done-btn {
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--cream);
  color: var(--ink);
  font-size: 17px;
  font-weight: 750;
  cursor: pointer;
  touch-action: manipulation;
}

.done-btn:active { transform: scale(0.99); }

/* Panels: summary + history + confirm */
.panel {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  overflow: hidden;
}

.panel.open { display: flex; flex-direction: column; }

.panel-inner {
  max-width: 430px;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: calc(12px + var(--safe-top)) 16px 0;
  min-height: 0;
}

.panel-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding-bottom: 8px;
}

.panel-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 10px;
}

.panel-bar h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -0.02em;
}

.summary-card, .history-item {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.kicker {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--muted);
  margin: 0 0 4px;
}

.when {
  font-weight: 650;
  margin: 0 0 2px;
}

.meta-line {
  color: var(--muted);
  font-size: 14px;
  margin: 0;
}

.breakdown {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  margin-top: 14px;
}

.breakdown caption {
  caption-side: top;
  text-align: left;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--muted);
  padding: 10px 0 6px;
}

.breakdown td {
  padding: 5px 0;
  border-top: 1px solid var(--line);
  font-size: 15px;
}

.breakdown td:last-child { text-align: right; font-weight: 650; }

.breakdown tr.section-total td {
  font-weight: 750;
}

.breakdown tr.grand td {
  border-top: 2px solid var(--ink);
  font-size: 18px;
  font-weight: 750;
  padding-top: 10px;
}

.pill-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 0 0;
  font-variant-numeric: tabular-nums;
  font-weight: 650;
}

.pill-row.over { color: var(--over); }
.pill-row.short { color: var(--short); }

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0 -16px 0;
  padding: 10px 16px calc(14px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

.actions button, .ghost-btn {
  min-height: 52px;
  border-radius: 14px;
  border: 1px solid var(--line-strong);
  background: var(--paper);
  font-weight: 750;
  cursor: pointer;
  touch-action: manipulation;
}

.actions button.primary {
  background: var(--forest);
  border-color: var(--forest);
  color: var(--cream);
  grid-column: 1 / -1;
}

.history-item {
  width: 100%;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}

.history-item .top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.history-item .sub {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
}

/* Confirm modal */
.modal-back {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(22, 21, 19, 0.45);
  z-index: 30;
  align-items: flex-end;
  justify-content: center;
  padding: 16px 16px calc(16px + var(--safe-bottom));
}

.modal-back.open { display: flex; }

.modal {
  width: 100%;
  max-width: 398px;
  background: var(--paper);
  border-radius: 20px;
  padding: 20px 18px 16px;
}

.modal h3 { margin: 0 0 8px; font-size: 18px; }
.modal p { margin: 0 0 16px; color: var(--muted); }

.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.modal-actions button {
  min-height: 52px;
  border-radius: 14px;
  font-weight: 750;
  cursor: pointer;
  touch-action: manipulation;
  border: 1px solid var(--line-strong);
  background: var(--paper);
}

.modal-actions .danger {
  background: var(--short);
  border-color: var(--short);
  color: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--dock-h) + 8px);
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: var(--cream);
  padding: 10px 16px;
  border-radius: 999px;
  font-weight: 650;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 40;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (min-width: 431px) {
  .dock, .header { max-width: 430px; }
}
