/* SplitFair styles — see DESIGN_SYSTEM.md for token meanings */
:root {
  --primary: #3730A3;
  --primary-hover: #312E81;
  --primary-tint: #EEF2FF;
  --bg: #F7F6F2;
  --surface: #FFFFFF;
  --text: #1C1917;
  --text-muted: #57534E;
  --border: #D6D3D1;
  --border-soft: #E7E5E4;
  --danger: #B42318;
  --danger-tint: #FEF3F2;
  --success: #166534;
  --success-tint: #F0FDF4;
  --warn: #92400E;
  --warn-tint: #FEF3C7;
  --radius: 8px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px;
  --font: "IBM Plex Sans Thai", "Noto Sans Thai", "Leelawadee UI", Tahoma, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  display: flex; flex-direction: column; min-height: 100vh;
  font-family: var(--font); font-size: 16px; line-height: 1.6;
  overflow-wrap: anywhere;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.75rem; line-height: 1.3; font-weight: 600; }
h2 { font-size: 1.125rem; line-height: 1.4; font-weight: 600; }
a { color: var(--primary); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.small { font-size: .875rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
h1:focus { outline: none; }

.skip { position: absolute; left: var(--s2); top: -100px; background: var(--surface); padding: var(--s2) var(--s3); z-index: 20; border: 1px solid var(--primary); border-radius: var(--radius); }
.skip:focus { top: var(--s2); }

/* Layout */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border-soft); }
.topbar-in { max-width: 960px; margin: 0 auto; padding: 0 var(--s4); min-height: 56px; display: flex; align-items: center; justify-content: space-between; }
.brand { display: inline-flex; align-items: center; min-height: 44px; font-weight: 700; font-size: 1.125rem; color: var(--primary); text-decoration: none; letter-spacing: .01em; }
main, .site-footer { width: min(100%, 960px); margin-inline: auto; padding-inline: var(--s4); }
main { flex: 1; padding-block: var(--s5) var(--s7); }
.site-footer p { border-top: 1px solid var(--border); padding-block: var(--s4); color: var(--text-muted); font-size: .875rem; }
.site-footer nav { display: flex; flex-wrap: wrap; gap: 0 var(--s5); }
.site-footer nav a { display: inline-flex; align-items: center; min-height: 44px; }
.info-page { max-width: 640px; }
.info-page h2 { margin-top: var(--s6); }
.info-page p, .info-page ul, .info-page ol { margin-top: var(--s3); }
.info-page li + li { margin-top: var(--s2); }
.page-head { display: flex; flex-direction: column; gap: var(--s2); margin-bottom: var(--s5); }
.back { display: inline-flex; align-items: center; gap: var(--s1); min-height: 44px; text-decoration: none; font-weight: 500; margin-left: calc(var(--s2) * -1); padding: 0 var(--s2); }
.back:hover { text-decoration: underline; }
.title-row { display: flex; align-items: flex-start; gap: var(--s2); }
.title-row h1 { flex: 1; min-width: 0; }

.stack > * + * { margin-top: var(--s5); }
.two-col { display: grid; gap: var(--s6); }
@media (min-width: 960px) { .two-col { grid-template-columns: 1fr 320px; align-items: start; } }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding-bottom: var(--s2); border-bottom: 2px solid var(--text); margin-bottom: 0; }

/* Icons */
.ic { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 44px; padding: 0 var(--s4); border-radius: var(--radius);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  border: 1px solid transparent; background: none; color: var(--text);
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { background: var(--primary-tint); border-color: var(--primary); }
.btn-danger { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger-tint); }
.btn-text { color: var(--primary); padding: 0 var(--s2); }
.btn-text:hover { text-decoration: underline; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border-radius: var(--radius); border: 0; background: none;
  color: var(--text-muted); cursor: pointer; text-decoration: none; flex: none;
}
.icon-btn:hover { background: var(--primary-tint); color: var(--primary); }
.icon-btn.danger:hover { background: var(--danger-tint); color: var(--danger); }
.actions { display: flex; flex-wrap: wrap; gap: var(--s3); }

/* Notices */
.notice { display: flex; gap: var(--s3); padding: var(--s3) var(--s4); border-radius: var(--radius); border: 1px solid; font-size: .9375rem; }
.notice .ic { margin-top: 2px; }
.notice-warn { background: var(--warn-tint); border-color: #E7C873; color: #5B2D06; }
.notice-error { background: var(--danger-tint); border-color: #F3B6B0; color: #7A1810; }
.notice-ok { background: var(--success-tint); border-color: #A7D7B5; color: #14532D; }
.notice-info { background: var(--primary-tint); border-color: #C7D2FE; color: #1E1B6B; }
.notice strong { display: block; }
.notice .actions { margin-top: var(--s2); }

/* Lists */
.rows { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: var(--s3); padding: var(--s3) 0; border-bottom: 1px solid var(--border-soft); min-height: 64px; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; }
.row-meta { font-size: .875rem; color: var(--text-muted); }
.row-amount { font-weight: 600; text-align: right; white-space: nowrap; }
a.row-link { color: inherit; text-decoration: none; }
a.row-link:hover .row-title { color: var(--primary); text-decoration: underline; }
.row-end { display: flex; align-items: center; }
.groups-list { border-top: 2px solid var(--text); }
.group-row { padding-block: 0; }
.group-link { display: flex; align-items: center; gap: var(--s3); min-height: 64px; padding: var(--s3) var(--s2); }
.group-link:hover { background: var(--primary-tint); }
.group-link > span:first-child { flex: 1; min-width: 0; }
.group-link .row-title, .group-link .row-meta { display: block; }

.total-line { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: var(--s2) var(--s5); }
.total-figure { font-size: 2rem; font-weight: 700; line-height: 1.2; }

/* Empty */
.empty { padding: var(--s5) 0; }
.empty h2 { margin-bottom: var(--s3); }
.steps { margin: 0 0 var(--s4); padding-left: var(--s5); }
.steps li { margin-bottom: var(--s1); }
.home-empty { border-block: 1px solid var(--border); }
.home-empty .steps { max-width: 54ch; margin-bottom: var(--s5); }
.home-empty .steps li + li { margin-top: var(--s2); }

/* Forms */
.field { display: flex; flex-direction: column; gap: var(--s1); }
label, legend { font-weight: 600; padding: 0; }
.hint { font-size: .875rem; color: var(--text-muted); }
input[type=text], select {
  width: 100%; min-height: 44px; padding: var(--s2) var(--s3); font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid #78716C; border-radius: var(--radius);
}
input[aria-invalid=true], select[aria-invalid=true] { border-color: var(--danger); border-width: 2px; }
.field-error { color: var(--danger); font-size: .9375rem; font-weight: 500; }
.amount-wrap { position: relative; }
.amount-wrap input { padding-right: 56px; text-align: right; font-variant-numeric: tabular-nums; font-size: 1.25rem; }
.amount-wrap span { position: absolute; right: var(--s3); top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
.check-grid { display: grid; gap: var(--s2); margin-top: var(--s2); }
@media (min-width: 600px) { .check-grid { grid-template-columns: 1fr 1fr; } }
.check { display: flex; align-items: center; gap: var(--s3); min-height: 44px; padding: 0 var(--s3); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); font-weight: 500; cursor: pointer; }
.check:has(input:checked) { border-color: var(--primary); background: var(--primary-tint); }
.check input { width: 20px; height: 20px; accent-color: var(--primary); flex: none; }
.form-grid { display: grid; gap: var(--s5); }
.form-narrow { max-width: 640px; }
.inline-form { display: flex; gap: var(--s2); align-items: flex-start; }
.inline-form .field { flex: 1; }

.preview { border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius); padding: var(--s3) var(--s4); }
.preview .row { min-height: 44px; padding: var(--s2) 0; }
.preview .row:last-child { border-bottom: 0; }
.link-btn { background: none; border: 0; padding: 0 var(--s2); min-height: 44px; font: inherit; color: var(--primary); cursor: pointer; font-weight: 500; }
.link-btn:hover { text-decoration: underline; }

.form-actions { display: flex; gap: var(--s3); flex-wrap: wrap; }
@media (max-width: 599px) {
  .form-actions.sticky {
    position: sticky; bottom: 0; background: var(--bg); padding: var(--s3) var(--s4);
    margin: 0 calc(var(--s4) * -1); border-top: 1px solid var(--border); z-index: 5;
  }
  .form-actions.sticky .btn-primary { flex: 1; }
}

/* Net table */
.net { width: 100%; border-collapse: collapse; }
.net th { text-align: left; font-size: .875rem; font-weight: 600; color: var(--text-muted); padding: var(--s2) var(--s3); border-bottom: 2px solid var(--text); }
.net td { padding: var(--s3); border-bottom: 1px solid var(--border-soft); vertical-align: top; }
.net .r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.net th.r { text-align: right; }
.net .who { font-weight: 600; }
.net-line { display: block; }
.net-line + .net-line { margin-top: var(--s1); }
.net-pay b { color: var(--danger); font-weight: 600; }
.net-recv b { color: var(--success); font-weight: 600; }
.net-none { color: var(--text-muted); }
.net .tot td { font-weight: 600; border-top: 2px solid var(--text); border-bottom: 0; }
.stat-label { display: none; }

@media (max-width: 719px) {
  .net thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .net, .net tbody, .net tr, .net td { display: block; }
  .net tr { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--s3); padding: var(--s3) 0; border-bottom: 1px solid var(--border-soft); }
  .net td { padding: 0; border: 0; }
  .net td.who { grid-column: 1 / -1; grid-row: 1; font-size: 1.0625rem; }
  .net td.res { grid-column: 1 / -1; grid-row: 2; margin: var(--s1) 0 var(--s2); font-size: 1.0625rem; }
  .net td.paid { grid-column: 1; grid-row: 3; text-align: left; }
  .net td.owed { grid-column: 2; grid-row: 3; text-align: left; }
  .stat-label { display: block; font-size: .8125rem; color: var(--text-muted); }
  .net .tot { display: flex; justify-content: space-between; border-top: 2px solid var(--text); padding: var(--s3) 0; }
  .net .tot td { display: block; }
  .net .tot td.paid, .net .tot td.owed { display: none; }
}

/* Share */
.share-box {
  white-space: pre-wrap; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--s4); font-size: .9375rem; line-height: 1.7; user-select: text;
}

/* Dialog + toast */
dialog { border: 0; border-radius: 12px; padding: var(--s5); width: min(440px, calc(100vw - 32px)); color: var(--text); font: inherit; box-shadow: 0 8px 32px rgba(28,25,23,.25); }
dialog::backdrop { background: rgba(28,25,23,.5); }
dialog h2 { margin-bottom: var(--s3); }
dialog .actions { margin-top: var(--s5); justify-content: flex-end; }
dialog ul { padding-left: var(--s5); margin: var(--s2) 0 0; }
#toast { position: fixed; left: 50%; bottom: var(--s5); transform: translateX(-50%); z-index: 30; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
#toast .t { background: var(--text); color: #fff; padding: var(--s3) var(--s4); border-radius: var(--radius); font-weight: 500; margin-top: var(--s2); }

/* Compact storage warning */
.notice-compact { display: flex; align-items: flex-start; gap: var(--s2); margin: 0; padding: 6px 10px; font-size: .8125rem; line-height: 1.5; color: #5B2D06; background: var(--warn-tint); border-left: 3px solid var(--warn); border-radius: 4px; }
.notice-compact .ic { width: 16px; height: 16px; margin-top: 2px; color: var(--warn); }
.notice-compact strong { font-weight: 600; }

@media (prefers-reduced-motion: no-preference) { .btn, .icon-btn { transition: background-color .12s; } }
@media (max-width: 599px) {
  h1 { font-size: 1.5rem; }
  main { padding-top: var(--s4); }
}
