/*
 * Cocoahead design system: tokens, base styles and shared components for every page.
 * Pages set <body class="field"> (big targets, outdoor contrast) or <body class="admin"> (dense, laptop-first).
 * Field-only rules live in app.css; dense admin layout in admin.css. No build step: plain CSS.
 * Contrast (light): text 16.3:1, text-2 9.3:1, text-3 6.4:1, strong borders >= 3.4:1, semantic on soft >= 5.5:1.
 */

@font-face {
  font-family: "Inter";
  src: url("fonts/inter-latin-wght.woff2") format("woff2");
  font-weight: 100 900; font-style: normal; font-display: swap;
}

:root {
  color-scheme: light;
  /* neutrals (warm stone) */
  --bg: #F7F5F2; --surface: #FFFFFF; --surface-2: #F1EDE8; --surface-3: #E8E2DB;
  --text: #1C1714; --text-2: #4F453E; --text-3: #685D54; --text-inverse: #FFFFFF;
  --border: #E2DBD3; --border-strong: #8A7D72; --divider: #ECE6E0;
  /* brand */
  --brand: #5A3220; --brand-hover: #4A2818; --brand-press: #3E2416; --brand-ink: #FFFFFF;
  --brand-soft: #F3EAE3; --brand-soft-ink: #5A3220; --espresso: #2A1B14; --copper: #A4572E;
  /* semantic: fg / soft bg / border */
  --ok: #1E6B3A;   --ok-bg: #E3F1E7;   --ok-line: #9CCBA9;
  --warn: #8A4B00; --warn-bg: #FCEFD8; --warn-line: #E6B866;
  --bad: #B3261E;  --bad-bg: #FCE8E6;  --bad-line: #EBA39D;
  --info: #1D5A8A; --info-bg: #E5EEF6; --info-line: #9DBEDB;
  --focus: #2563EB;
  /* charts */
  --chart-1: #A4572E; --chart-2: #3F6F8F; --chart-muted: #CBC2BA; --chart-grid: #ECE6E0;
  /* type */
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Roboto Mono", monospace;
  --fs-xs: 12px; --fs-sm: 13px; --fs-md: 14px; --fs-base: 15px; --fs-lg: 17px; --fs-xl: 20px; --fs-2xl: 24px; --fs-3xl: 30px;
  --lh-tight: 1.2; --lh: 1.45;
  /* space (4-pt) */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 20px; --s6: 24px; --s8: 32px; --s10: 40px; --s12: 48px;
  /* radii */
  --r-sm: 6px; --r-md: 8px; --r-lg: 12px; --r-pill: 999px;
  /* elevation */
  --shadow-1: 0 1px 2px rgba(28,23,20,.06);
  --shadow-2: 0 4px 12px rgba(28,23,20,.10), 0 1px 3px rgba(28,23,20,.08);
  --shadow-pop: 0 12px 32px rgba(28,23,20,.22);
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--focus);
  /* motion */
  --t-fast: 120ms; --t-med: 200ms; --ease: cubic-bezier(.2,.7,.2,1);
  /* control sizes (overridden per scale) */
  --ctl-h: 40px; --ctl-fs: 14px; --ctl-px: 12px; --tap: 40px; --ctl-bw: 1px; --ctl-r: var(--r-md);
}
/* Dark: follows the device unless a theme was chosen (data-theme on <html>, set by config.js). */
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) {
  color-scheme: dark;
  --bg: #14110F; --surface: #1E1916; --surface-2: #28211D; --surface-3: #332A25;
  --text: #F4EFEA; --text-2: #CFC4BA; --text-3: #A99C91; --text-inverse: #14110F;
  --border: #3A312B; --border-strong: #7A6C62; --divider: #2E2723;
  --brand: #DDA67F; --brand-hover: #E8B893; --brand-press: #CF946B; --brand-ink: #1A110C;
  --brand-soft: #3A2A21; --brand-soft-ink: #EBC3A6; --espresso: #0F0B09; --copper: #E09A6B;
  --ok: #7BD394;   --ok-bg: #16261B;   --ok-line: #2F5E3D;
  --warn: #F2B661; --warn-bg: #2E2412; --warn-line: #6B5020;
  --bad: #F49387;  --bad-bg: #331B18;  --bad-line: #6E302A;
  --info: #8EC1EA; --info-bg: #142331; --info-line: #2C4F6E;
  --focus: #7CB3FF;
  --chart-1: #E09A6B; --chart-2: #7FB0D0; --chart-muted: #5A4F48; --chart-grid: #2E2723;
  --shadow-1: none; --shadow-2: 0 4px 12px rgba(0,0,0,.4); --shadow-pop: 0 12px 32px rgba(0,0,0,.6);
}}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14110F; --surface: #1E1916; --surface-2: #28211D; --surface-3: #332A25;
  --text: #F4EFEA; --text-2: #CFC4BA; --text-3: #A99C91; --text-inverse: #14110F;
  --border: #3A312B; --border-strong: #7A6C62; --divider: #2E2723;
  --brand: #DDA67F; --brand-hover: #E8B893; --brand-press: #CF946B; --brand-ink: #1A110C;
  --brand-soft: #3A2A21; --brand-soft-ink: #EBC3A6; --espresso: #0F0B09; --copper: #E09A6B;
  --ok: #7BD394;   --ok-bg: #16261B;   --ok-line: #2F5E3D;
  --warn: #F2B661; --warn-bg: #2E2412; --warn-line: #6B5020;
  --bad: #F49387;  --bad-bg: #331B18;  --bad-line: #6E302A;
  --info: #8EC1EA; --info-bg: #142331; --info-line: #2C4F6E;
  --focus: #7CB3FF;
  --chart-1: #E09A6B; --chart-2: #7FB0D0; --chart-muted: #5A4F48; --chart-grid: #2E2723;
  --shadow-1: none; --shadow-2: 0 4px 12px rgba(0,0,0,.4); --shadow-pop: 0 12px 32px rgba(0,0,0,.6);
}
body.field { --ctl-h: 52px; --ctl-fs: 18px; --ctl-px: 14px; --tap: 48px; --ctl-bw: 1.5px; --ctl-r: 10px; font-size: 17px; }
body.admin { --ctl-h: 36px; --ctl-fs: 14px; --ctl-px: 10px; --tap: 36px; font-size: 14px; }
@media (pointer: coarse) { body.admin { --ctl-h: 44px; --tap: 44px; } }
@media (prefers-reduced-motion: reduce) { :root { --t-fast: 0ms; --t-med: 0ms; } }

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); line-height: var(--lh);
  font-feature-settings: "cv05" 1; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}
h1, h2, h3 { margin: 0; line-height: var(--lh-tight); letter-spacing: -0.01em; }
p { margin: 0; }
a { color: var(--copper); text-underline-offset: 3px; }
a:hover { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus { outline: none; }
:focus-visible { outline: none; box-shadow: var(--ring); }
.num, td.n, th.n, .tile-v, input[type=number], .id { font-variant-numeric: tabular-nums; }
.id { font-weight: 600; }
.mono { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--text-3); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.icon { flex: none; display: inline-block; vertical-align: -0.2em; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  height: var(--ctl-h); min-height: var(--ctl-h); padding: 0 var(--s4); border-radius: var(--ctl-r);
  border: 1px solid transparent; font-weight: 600; font-size: var(--ctl-fs); line-height: 1; cursor: pointer;
  text-decoration: none; white-space: nowrap; transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}
.btn .icon { width: 18px; height: 18px; }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn-primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); color: var(--brand-ink); }
.btn-primary:active { background: var(--brand-press); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn-danger:hover { filter: brightness(.94); color: #fff; }
.btn-danger-quiet { background: var(--surface); color: var(--bad); border-color: var(--bad-line); }
.btn-danger-quiet:hover { background: var(--bad-bg); color: var(--bad); }
.btn-ghost { background: transparent; color: var(--text-2); border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn:disabled, .btn[aria-disabled="true"] {
  opacity: 1; background: var(--surface-2); color: var(--text-3); border-color: var(--border); cursor: not-allowed; filter: none;
}
.btn[aria-busy="true"] { cursor: progress; }
.btn-block { width: 100%; }
.btn-sm { --ctl-h: 32px; padding: 0 var(--s3); font-size: var(--fs-sm); }
.btn-icon { width: var(--ctl-h); padding: 0; }

/* ---------- inputs ---------- */
.input, .select, .textarea,
input[type=text], input[type=number], input[type=date], input[type=time], input[type=search], input[type=email], input[type=tel], select, textarea {
  width: 100%; height: var(--ctl-h); min-height: var(--ctl-h); padding: 0 var(--ctl-px); font-size: var(--ctl-fs);
  border: var(--ctl-bw) solid var(--border-strong); border-radius: var(--ctl-r); background: var(--surface); color: var(--text);
  transition: border-color var(--t-fast) var(--ease);
}
textarea, .textarea { height: auto; min-height: 96px; padding: var(--s3) var(--ctl-px); resize: vertical; line-height: var(--lh); }
body.admin textarea { min-height: 72px; }
input:hover, select:hover, textarea:hover { border-color: var(--text-3); }
input:focus-visible, select:focus-visible, textarea:focus-visible { border-color: var(--focus); box-shadow: var(--ring); }
select {
  appearance: none; -webkit-appearance: none; padding-right: calc(var(--ctl-px) + 24px);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23685D54' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--ctl-px) center; background-size: 16px;
}
input:disabled, select:disabled, textarea:disabled, input[readonly] { background: var(--surface-2); color: var(--text-2); }
.is-error input, .is-error select, .is-error textarea, input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--bad); }
.is-warn input, .is-warn select, .is-warn textarea { border-color: var(--warn-line); background: var(--warn-bg); }
.field-msg { display: flex; gap: var(--s1); align-items: flex-start; margin-top: var(--s1); font-weight: 600; font-size: var(--fs-sm); }
body.field .field-msg { font-size: 15px; }
.field-msg.bad { color: var(--bad); }
.field-msg.warn { color: var(--warn); }
.field-msg .icon { width: 16px; height: 16px; margin-top: 2px; }

.input-unit { display: flex; align-items: stretch; }
.input-unit input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-unit .unit {
  display: grid; place-items: center; min-width: 56px; padding: 0 var(--s3); border: var(--ctl-bw) solid var(--border-strong); border-left: 0;
  border-radius: 0 var(--ctl-r) var(--ctl-r) 0; background: var(--surface-2); color: var(--text-2); font-weight: 600; font-size: var(--ctl-fs);
}
body.admin .input-unit input { text-align: right; }

/* ---------- option chips (radio / checkbox) ---------- */
.chips { display: flex; flex-wrap: wrap; gap: var(--s2); }
.chip-opt { position: relative; display: inline-flex; }
.chip-opt input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.chip-opt span {
  display: inline-flex; align-items: center; gap: var(--s2); min-height: var(--tap); padding: 0 var(--s4);
  border: var(--ctl-bw) solid var(--border-strong); border-radius: var(--r-pill); background: var(--surface); color: var(--text); font-weight: 600;
  transition: background var(--t-fast) var(--ease);
}
.chip-opt span::before { content: ""; width: 0; height: 16px; transition: width var(--t-fast) var(--ease); }
.chip-opt input:checked + span { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.chip-opt input:checked + span::before {
  width: 16px; background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E") center / 16px no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E") center / 16px no-repeat;
}
.chip-opt input:focus-visible + span { box-shadow: var(--ring); }
.chip-opt.other span { border-style: dashed; }

/* ---------- cards, tiles ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s4); box-shadow: var(--shadow-1); min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); flex-wrap: wrap; margin-bottom: var(--s3); }
.card-head h2, .card-head h3 { font-size: 16px; font-weight: 600; }
.card-sub { color: var(--text-3); font-size: var(--fs-sm); margin-top: 2px; }
.card.later { background: transparent; border-style: dashed; box-shadow: none; }
.group { background: var(--surface-2); border-radius: var(--r-lg); padding: var(--s4); }

.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--s3); }
@media (max-width: 999px) { .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s3) var(--s4); display: grid; gap: 2px; box-shadow: var(--shadow-1); min-width: 0; }
.tile-l { font-size: var(--fs-sm); font-weight: 500; color: var(--text-2); }
.tile-v { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.15; }
.tile-v small { font-size: 14px; font-weight: 500; color: var(--text-3); margin-left: 4px; letter-spacing: 0; }
.tile-n { font-size: var(--fs-xs); color: var(--text-3); }
.tiles.compact .tile-v { font-size: 20px; }

/* ---------- tables ---------- */
.table-wrap { overflow: auto; max-height: 28rem; border-radius: var(--r-md); }
.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-sm); }
.table th {
  position: sticky; top: 0; z-index: 1; background: var(--surface); text-align: left; font-size: var(--fs-xs); font-weight: 600;
  color: var(--text-3); padding: var(--s2) var(--s3); border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: var(--s2) var(--s3); border-bottom: 1px solid var(--divider); vertical-align: top; height: 36px; }
.table th.n, .table td.n { text-align: right; }
.table tbody tr:hover td { background: var(--surface-2); }
.table tr.sel td { background: var(--brand-soft); }
.table tr.sel td:first-child { box-shadow: inset 3px 0 0 var(--brand); }
.table td.id { font-weight: 600; }
.table td.ell { max-width: 16rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.table td:empty::after { content: "–"; color: var(--text-3); }
.table th button { all: unset; cursor: pointer; }
.table th button:focus-visible { box-shadow: var(--ring); border-radius: 4px; }

/* ---------- badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 6px; height: 22px; padding: 0 var(--s2); border-radius: var(--r-pill);
  font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; background: var(--surface-3); color: var(--text-2);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.brand { background: var(--brand-soft); color: var(--brand-soft-ink); }
.badge.plain::before { display: none; }

/* ---------- notices ---------- */
.notice { display: flex; gap: var(--s3); align-items: flex-start; border-radius: var(--r-lg); padding: var(--s3) var(--s4); border: 1px solid var(--border); background: var(--surface-2); }
.notice > .icon { width: 20px; height: 20px; margin-top: 1px; }
.notice .notice-body { display: grid; gap: var(--s1); min-width: 0; flex: 1; }
.notice .notice-title { font-weight: 600; }
.notice.ok { background: var(--ok-bg); border-color: var(--ok-line); }
.notice.ok > .icon { color: var(--ok); }
.notice.warn { background: var(--warn-bg); border-color: var(--warn-line); }
.notice.warn > .icon { color: var(--warn); }
.notice.bad { background: var(--bad-bg); border-color: var(--bad-line); }
.notice.bad > .icon { color: var(--bad); }
.notice.info { background: var(--info-bg); border-color: var(--info-line); }
.notice.info > .icon { color: var(--info); }
.notice-actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s2); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  bottom: calc(var(--savebar-h, 0px) + 16px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: center; gap: var(--s2); max-width: min(24rem, calc(100vw - 32px)); width: max-content;
  background: var(--espresso); color: #fff; border-radius: var(--r-md); padding: var(--s3) var(--s4); font-size: 15px; font-weight: 500;
  box-shadow: var(--shadow-pop); pointer-events: none;
}
.toast .icon { width: 18px; height: 18px; }

/* ---------- empty state ---------- */
.empty { display: grid; justify-items: center; text-align: center; gap: var(--s2); padding: var(--s8) var(--s4); color: var(--text-2); }
.empty .icon { width: 32px; height: 32px; color: var(--text-3); }
.empty-title { font-weight: 600; color: var(--text); }
.empty-hint { font-size: var(--fs-sm); color: var(--text-3); max-width: 36ch; }

/* ---------- in-page confirmation ---------- */
.confirm { display: grid; gap: var(--s2); border-radius: var(--r-lg); padding: var(--s3) var(--s4); background: var(--surface-2); border: 1px solid var(--border); }
.confirm.bad { background: var(--bad-bg); border-color: var(--bad-line); }
.confirm.warn { background: var(--warn-bg); border-color: var(--warn-line); }
.confirm-title { font-weight: 600; }
.confirm ul { margin: 0; padding-left: 20px; }
.confirm-actions { display: flex; flex-wrap: wrap; gap: var(--s2); justify-content: flex-end; margin-top: var(--s1); }

/* ---------- label / value facts ---------- */
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 10rem), 1fr)); gap: var(--s3) var(--s4); margin: 0; }
.facts dt { font-size: var(--fs-xs); color: var(--text-3); font-weight: 500; }
.facts dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

/* ---------- admin app bar (shared markup on admin pages) ---------- */
.appbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); }
.appbar-in { max-width: 1280px; margin: 0 auto; min-height: 56px; display: flex; align-items: center; gap: var(--s4); padding: 0 max(var(--s4), env(safe-area-inset-right)) 0 max(var(--s4), env(safe-area-inset-left)); }
.appbar .brand { display: inline-flex; align-items: center; gap: var(--s2); font-weight: 700; font-size: 15px; color: var(--text); text-decoration: none; }
.appbar .brand img, .appbar .brand svg { width: 24px; height: 24px; border-radius: 6px; }
.appbar nav { display: flex; gap: var(--s1); align-self: stretch; }
.appbar nav a { display: inline-flex; align-items: center; padding: 0 var(--s3); color: var(--text-2); font-weight: 500; font-size: 14px; text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.appbar nav a:hover { color: var(--text); }
.appbar nav a[aria-current="page"] { color: var(--text); font-weight: 600; border-bottom-color: var(--brand); }
.appbar .spacer { flex: 1; }
.appbar .meta { color: var(--text-3); font-size: var(--fs-sm); white-space: nowrap; }
.appbar-tools { display: flex; align-items: center; gap: var(--s2); }
@media (max-width: 719px) {
  .appbar-in { flex-wrap: wrap; row-gap: 0; padding-top: var(--s2); }
  .appbar nav { order: 3; width: 100%; overflow-x: auto; min-height: 44px; }
  .appbar .meta { display: none; }
}

/* ---------- print ---------- */
@media print {
  body { background: #fff; color: #000; }
  .appbar, .toast, .no-print { display: none !important; }
  .card, .tile { box-shadow: none; border-color: #999; }
}
