:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #5d6877;
  --border: #dde1e7;
  --primary: #1f6f5c;
  --primary-hover: #185a4a;
  --primary-text: #ffffff;
  --bill: #2e6bb3;
  --coin: #a8741a;
  --pos: #1d7a3e;
  --neg: #b42318;
  --danger: #b42318;
  --event: #6c4dc4;
  --notice-bg: #fff4e0;
  --info-bg: #e7f0fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1b2129;
    --text: #e6e9ee;
    --muted: #9aa4b2;
    --border: #2d3540;
    --primary: #3aa58a;
    --primary-hover: #4bb89c;
    --primary-text: #0b1512;
    --bill: #6ea4e6;
    --coin: #e0a948;
    --pos: #4cc277;
    --neg: #f07167;
    --danger: #f07167;
    --event: #a991f0;
    --notice-bg: #3a2c12;
    --info-bg: #182a40;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--primary); }

h1, h2, h3 { line-height: 1.2; margin: 0 0 0.25rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.1rem; }
p { margin: 0 0 0.75rem; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; color: var(--text); text-decoration: none; }
.logo {
  display: inline-grid; place-items: center;
  width: 2rem; height: 2rem; border-radius: 50%;
  background: var(--primary); color: var(--primary-text); font-weight: 800;
}
.topbar nav { display: flex; gap: 1rem; align-items: center; }
.link-button { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; cursor: pointer; }
.link-button:hover { color: var(--text); }

main { max-width: 860px; margin: 0 auto; padding: 1.25rem 1rem 7rem; }
main:focus { outline: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}
.narrow { max-width: 420px; margin: 2rem auto; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

label, .label { display: block; font-weight: 600; margin-bottom: 0.35rem; }

input, textarea, select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
}
input:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
input:disabled { opacity: 0.65; }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
  font: inherit; font-weight: 600; font-size: 0.95rem;
  padding: 0.55rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover:not(:disabled), .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
button.primary:hover { background: var(--primary-hover); }
button.primary.outline { background: var(--surface); color: var(--primary); }
button.primary.outline:hover { background: var(--surface); border-color: var(--primary-hover); }
button.danger { color: var(--danger); }
button.export { color: var(--primary); }
button.small { padding: 0.25rem 0.6rem; font-size: 0.85rem; }
.wide { width: 100%; margin-top: 1rem; padding: 0.75rem; }

.code-input { font-size: 1.6rem; letter-spacing: 0.3em; text-align: center; text-transform: uppercase; font-weight: 700; }
.error-text { color: var(--danger); margin: 0.5rem 0 0; }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.back-link { display: inline-block; margin-bottom: 0.75rem; }

.badge { display: inline-block; padding: 0.1rem 0.55rem; border-radius: 999px; font-size: 0.8rem; font-weight: 700; }
.badge.open { background: rgba(29, 122, 62, 0.14); color: var(--pos); }
.badge.added { background: rgba(29, 122, 62, 0.14); color: var(--pos); }
.badge.removed { background: rgba(180, 35, 24, 0.12); color: var(--neg); }
.small-badge { margin-left: 0.5rem; font-size: 0.7rem; padding: 0 0.45rem; }
.badge.closed { background: rgba(93, 104, 119, 0.16); color: var(--muted); }

.notice { background: var(--notice-bg); padding: 0.75rem 1rem; border-radius: 8px; }
.notice.info { background: var(--info-bg); }
.notes { white-space: pre-wrap; }

.save-status { font-size: 0.9rem; color: var(--muted); min-height: 1.5em; }
.save-status.error { color: var(--danger); font-weight: 600; }

/* Counting */
.field-card label { margin-bottom: 0.35rem; }
.section-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 0.5rem; }
.section-head strong { font-size: 1.1rem; }

.denom-row {
  display: grid;
  grid-template-columns: 5.5rem 1fr 7rem;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
}
.denom-label { font-weight: 700; font-variant-numeric: tabular-nums; }
.denom-label.bill { color: var(--bill); }
.denom-label.coin { color: var(--coin); }
.stepper { display: flex; gap: 0.4rem; justify-content: center; }
.stepper input { width: 5.5rem; text-align: center; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.step { width: 2.75rem; height: 2.75rem; padding: 0; font-size: 1.4rem; line-height: 1; }
.subtotal { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

.total-bar {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 8px rgba(16, 24, 40, 0.08);
  padding: 0.75rem max(1rem, calc((100vw - 828px) / 2));
}
.sub-totals { display: flex; gap: 1.25rem; color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.total-line { display: flex; justify-content: space-between; align-items: baseline; font-size: 1.1rem; }
.grand-total { font-size: 1.5rem; font-variant-numeric: tabular-nums; }

/* Admin */
.cash-card { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.cash-card p { margin: 0; }
.cash-total { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.cash-hint { flex-basis: 100%; }
.stock-details { flex-basis: 100%; }
.stock-details summary { cursor: pointer; font-weight: 600; color: var(--primary); }
.stock-details .piece-lists { margin-top: 0.5rem; }
.cash-split { display: flex; gap: 1.25rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.session-card.adjustment { border-left: 4px solid var(--primary); }
.denom-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1.5rem; margin-bottom: 1rem; }
.denom-column .section-head h3 { margin: 0; }
.denom-row.compact { grid-template-columns: 4.5rem 1fr 5.5rem; gap: 0.5rem; padding: 0.35rem 0; }
.denom-row.compact .step { width: 2.25rem; height: 2.25rem; font-size: 1.2rem; }
.denom-row.compact .stepper input { width: 4rem; padding: 0.4rem; font-size: 1rem; }
.piece-lists { display: flex; gap: 2rem; flex-wrap: wrap; margin-top: 0.75rem; }
.piece-lists .label { margin-bottom: 0.15rem; color: var(--muted); }
.pieces { list-style: none; margin: 0; padding: 0; font-variant-numeric: tabular-nums; }
.pieces li { display: flex; justify-content: space-between; gap: 1.5rem; min-width: 11rem; }
.kind-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.kind-option { align-items: flex-start; border: 1px solid var(--border); border-radius: 8px; padding: 0.6rem 0.75rem; }
.kind-option input { margin-top: 0.2rem; }
.kind-option:has(input:checked) { border-color: var(--primary); }
.float-section { border-left: 4px solid var(--event); padding-left: 0.9rem; margin-bottom: 1rem; }
.stock-hint { display: block; font-weight: 400; font-size: 0.75rem; color: var(--muted); }
.stock-hint.warn { color: var(--danger); font-weight: 600; }
.badges { display: flex; gap: 0.35rem; flex-wrap: wrap; justify-content: flex-end; }
.badge.event { background: rgba(108, 77, 196, 0.14); color: var(--event); }
.session-card.event { border-left: 4px solid var(--event); }
.float-details { margin-top: 0.5rem; }
.float-details summary { cursor: pointer; font-weight: 600; }
.float-details .pieces { margin-top: 0.35rem; max-width: 16rem; }
.radio-row { display: flex; gap: 1.5rem; flex-wrap: wrap; }
.balance-preview { background: var(--bg); border-radius: 8px; padding: 0.6rem 0.8rem; font-variant-numeric: tabular-nums; white-space: pre-wrap; }
.actions { display: flex; gap: 0.5rem; align-items: center; }
.actions.wrap { flex-wrap: wrap; }
.page-head .actions { flex-wrap: wrap; }
.session-form h2 { margin-bottom: 1rem; }
.form-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.form-row .field { flex: 1 1 180px; }
.form-row .grow { flex: 3 1 240px; }
.field { margin-bottom: 1rem; }
.denom-groups { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }
.denom-group { border: 1px solid var(--border); border-radius: 8px; padding: 0.5rem 0.9rem 0.9rem; margin: 0; }
.denom-group legend { padding: 0 0.35rem; }
.check { display: flex; align-items: center; gap: 0.45rem; font-weight: 400; margin: 0; cursor: pointer; }
.check.strong { font-weight: 700; }
.check input { width: 1.1rem; height: 1.1rem; margin: 0; }
.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 0.4rem 0.75rem; }
.start-amounts { margin-bottom: 1rem; }
.start-amounts summary { cursor: pointer; font-weight: 600; margin-bottom: 0.5rem; }

.session-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.session-head p { margin: 0; }
.code-line { display: flex; align-items: center; gap: 0.75rem; margin: 0.75rem 0; flex-wrap: wrap; }
.code { font-size: 1.15rem; letter-spacing: 0.15em; }

table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 0.4rem 0.5rem; text-align: right; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
tbody th { font-weight: 600; }
.totals-table { margin: 0.75rem 0 1rem; }
.totals-table tr.sum th, .totals-table tr.sum td { font-weight: 700; border-bottom: 0; }
.ledger td:nth-child(2) { text-align: left; }
.ledger th:nth-child(2) { text-align: left; }
.table-scroll { overflow-x: auto; margin-bottom: 1rem; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.empty { text-align: center; padding: 2rem 0; }

#toast {
  position: fixed; left: 50%; top: 1rem; transform: translate(-50%, -150%);
  background: var(--text); color: var(--bg);
  padding: 0.6rem 1rem; border-radius: 8px; font-weight: 600;
  transition: transform 0.2s ease, opacity 0.2s ease; z-index: 10; pointer-events: none;
  opacity: 0;
}
#toast.show { transform: translate(-50%, 0); opacity: 1; }
#toast.error { background: var(--danger); color: #fff; }

@media (max-width: 560px) {
  h1 { font-size: 1.35rem; }
  .card { padding: 1rem; }
  .denom-row { grid-template-columns: 4.25rem 1fr; }
  .subtotal { grid-column: 2; text-align: center; margin-top: -0.35rem; font-size: 0.85rem; }
  .stepper input { width: 4.75rem; }
  .denom-groups { grid-template-columns: 1fr; }
  .denom-columns { grid-template-columns: 1fr; }
  .kind-row { grid-template-columns: 1fr; }
  .denom-row.compact { grid-template-columns: 4.25rem 1fr; }
  .totals-table th, .totals-table td { padding: 0.35rem 0.25rem; font-size: 0.9rem; }
}
