:root {
  --paper: #F4F5F2;
  --paper-2: #FBFBF9;
  --ink: #1F2B3A;
  --ink-2: #566170;
  --rule: #C8CCC6;
  --rule-2: #E3E6E1;
  --stamp: #B4322D;
  --ledger: #2E6B4E;
  --amber: #8A6D1F;
  --serif: "PT Serif", Georgia, serif;
  --sans: "PT Sans", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: none; }
body { max-height: 999999px; }
html, body { overflow-x: hidden; width: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  font-variant-numeric: tabular-nums;
  padding-bottom: env(safe-area-inset-bottom);
}
main { padding: 0 16px 40px; max-width: 560px; margin: 0 auto; }
.sr { position: absolute; left: -9999px; }
[hidden] { display: none !important; }

/* ---------- top ---------- */
.top {
  display: grid; grid-template-columns: 1fr auto; grid-template-areas: "mark bal" "tabs tabs";
  align-items: center; row-gap: 6px; column-gap: 12px;
  padding: 10px 16px 0;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
  position: sticky; top: 0; z-index: 5;
}
.mark {
  grid-area: mark; justify-self: start;
  font-family: var(--serif); font-weight: 700; font-size: 15px; letter-spacing: .01em;
  border: 1.5px solid var(--ink); padding: 2px 8px; line-height: 1.3;
}
.balance { grid-area: bal; font-family: var(--serif); font-size: 15px; color: var(--ink-2); white-space: nowrap; }
.balance b { color: var(--ink); }
.tabs { grid-area: tabs; display: flex; }
.tab {
  flex: 1; background: none; border: 0; padding: 8px 4px 9px; font: inherit; font-size: 15px; color: var(--ink-2);
  border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab.is-active { color: var(--ink); border-bottom-color: var(--ink); }

/* ---------- form ---------- */
form { padding-top: 16px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; position: relative; }
.field label, fieldset legend { font-size: 13px; color: var(--ink-2); }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.row .field { flex: 0 1 auto; min-width: 0; }
.row .field input, .row .field select { width: 100%; }
.row .grow { flex: 1 1 120px; min-width: 0; }
input, select {
  font: inherit; color: var(--ink); background: var(--paper-2);
  border: 1px solid var(--rule); border-radius: 3px; padding: 9px 10px;
  width: 100%; min-width: 0; max-width: 100%; min-height: 42px;
}
input[type=date] { -webkit-appearance: none; appearance: none; }
.field { min-width: 0; }
input:focus, select:focus { outline: 2px solid var(--ink); outline-offset: -1px; border-color: var(--ink); }
input::placeholder { color: #9AA1A9; }
select { appearance: none; -webkit-appearance: none; padding-right: 28px;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-2) 50%), linear-gradient(135deg, var(--ink-2) 50%, transparent 50%);
  background-position: calc(100% - 15px) 50%, calc(100% - 10px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
.hint { margin: 2px 0 0; font-size: 13px; color: var(--ink-2); min-height: 1.2em; }
.hint.ok { color: var(--ledger); }
.hint.bad { color: var(--stamp); }

.who { border: 0; padding: 0; margin: 0 0 16px; display: flex; gap: 0; }
.seg { flex: 1; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; left: 0; top: 0; margin: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 9px 6px; white-space: nowrap; font-size: 15px; border: 1px solid var(--rule);
  background: var(--paper-2); color: var(--ink-2); cursor: pointer;
}
.seg:first-child span { border-radius: 3px 0 0 3px; }
.seg:last-child span { border-radius: 0 3px 3px 0; border-left: 0; }
.seg input:checked + span { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.seg input:focus-visible + span { outline: 2px solid var(--ink); outline-offset: 2px; }

.check { display: flex; gap: 8px; align-items: center; padding: 9px 0; font-size: 15px; cursor: pointer; }
.check input { width: 18px; height: 18px; min-height: 0; margin: 0; accent-color: var(--ink); }

.suggest {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 10; margin: 2px 0 0; padding: 0; list-style: none;
  background: var(--paper-2); border: 1px solid var(--ink); border-radius: 3px; max-height: 260px; overflow: auto;
}
.suggest li { padding: 8px 10px; border-bottom: 1px solid var(--rule-2); cursor: pointer; font-size: 14px; }
.suggest li:last-child { border-bottom: 0; }
.suggest li:hover, .suggest li.is-hi { background: var(--rule-2); }
.suggest code { font-family: var(--serif); font-weight: 700; margin-right: 6px; }
.suggest small { display: block; color: var(--ink-2); }

button.primary {
  width: 100%; margin-top: 4px; padding: 13px; font: 700 17px var(--sans); color: var(--paper);
  background: var(--ink); border: 0; border-radius: 3px; cursor: pointer;
}
button.primary:disabled { opacity: .55; cursor: wait; }
button.ghost {
  padding: 9px 14px; font: inherit; color: var(--ink); background: none;
  border: 1px solid var(--ink); border-radius: 3px; cursor: pointer;
}
.fine { font-size: 13px; color: var(--ink-2); margin: 8px 0 0; }
.error { color: var(--stamp); font-size: 14px; margin: 0 0 10px; }
.empty { color: var(--ink-2); text-align: center; padding: 40px 0; }

/* ---------- result sheet ---------- */
.sheet {
  margin-top: 24px; background: var(--paper-2); border: 1px solid var(--rule); padding: 18px 16px 14px;
}
.sheet-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 4px; }
.sheet-head h2 { font: 700 15px var(--serif); margin: 0; }
.sheet-head time { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.sheet .name { font-size: 14px; color: var(--ink-2); margin: 0 0 2px; }
.sheet .rate { font-family: var(--serif); font-size: 15px; margin: 0 0 14px; }

.lines { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--ink); }
.lines li { padding: 10px 0; border-bottom: 1px solid var(--rule-2); }
.lines .l { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.lines .t { font-size: 15px; min-width: 0; }
.lines .a { font: 700 17px var(--serif); white-space: nowrap; }
.lines .f { font-size: 13px; color: var(--ink-2); margin-top: 3px; word-break: break-word; }
.lines .n { font-size: 12px; color: var(--ink-2); margin-top: 2px; }
.lines .n.pref { color: var(--ledger); }

.total { border-top: 2px solid var(--stamp); margin-top: 2px; padding: 12px 0 6px; }
.total .l { display: flex; justify-content: space-between; align-items: baseline; }
.total .t { font: 700 15px var(--serif); }
.total .a { font: 700 24px/1 var(--serif); }
.total .pu { font-size: 13px; color: var(--ink-2); margin-top: 4px; text-align: right; }

.warns { margin: 12px 0 0; padding: 0; list-style: none; }
.warns li { font-size: 13px; color: var(--amber); padding: 2px 0; }

.broker { margin-top: 18px; }
.broker h3 { font: 700 15px var(--serif); margin: 0 0 8px; }
.broker ul { list-style: none; margin: 0; padding: 0; }
.broker li { padding: 8px 0 8px 18px; border-top: 1px solid var(--rule-2); position: relative; }
.broker li::before {
  content: ""; position: absolute; left: 0; top: 14px; width: 8px; height: 8px; border-radius: 50%;
}
.broker li.sev-blocker::before { background: var(--stamp); }
.broker li.sev-check::before { background: var(--amber); }
.broker li.sev-info::before { background: var(--rule); }
.broker b { font-weight: 700; font-size: 14px; display: block; }
.broker p { margin: 2px 0 0; font-size: 13px; color: var(--ink-2); }

.sheet-foot { display: flex; gap: 10px; margin-top: 16px; }
.sheet-foot .fine { flex: 1; margin: 0; align-self: center; }

/* ---------- history ---------- */
.history { list-style: none; margin: 0; padding: 12px 0 0; }
.history li { padding: 12px 0; border-bottom: 1px solid var(--rule-2); cursor: pointer; }
.history .h1 { display: flex; justify-content: space-between; gap: 10px; }
.history code { font-family: var(--serif); font-weight: 700; }
.history .sum { font: 700 16px var(--serif); white-space: nowrap; }
.history .h2 { font-size: 13px; color: var(--ink-2); margin-top: 2px; display: flex; justify-content: space-between; }

/* ---------- pay modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(31,43,58,.55); display: flex; align-items: flex-end; z-index: 20; }
.modal-body { background: var(--paper); width: 100%; max-width: 100vw; box-sizing: border-box; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); border-radius: 8px 8px 0 0; }
.modal h2 { font: 700 18px var(--serif); margin: 0 0 6px; }
.modal p { margin: 0 0 14px; font-size: 14px; color: var(--ink-2); }
.packs { display: grid; gap: 8px; margin-bottom: 14px; }
.pack {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 12px; text-align: left; padding: 12px 14px;
  border: 1px solid var(--ink); border-radius: 3px; background: var(--paper-2); cursor: pointer; font: inherit; color: var(--ink);
}
.pack b { font: 700 18px var(--serif); }
.pack small { color: var(--ink-2); }

/* ---------- admin ---------- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; margin: 16px 0; }
.stats div { border-top: 1px solid var(--ink); padding-top: 6px; }
.stats dt { font-size: 12px; color: var(--ink-2); }
.stats dd { margin: 0; font: 700 22px var(--serif); }
.toggles { margin-bottom: 16px; }
#adm-grant input { flex: 1; }

@media (prefers-reduced-motion: no-preference) {
  .sheet { animation: rise .25s ease-out; }
  @keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
}

@media (max-width: 360px) {
  body { font-size: 15px; }
  main { padding: 0 12px 40px; }
  .top { padding: 8px 12px 0; }
  .sheet { padding: 14px 12px 12px; }
  .total .a { font-size: 22px; }
}
