:root {
  --bg: #f3f6f8;
  --surface: #ffffff;
  --text: #13222b;
  --muted: #5d6f7a;
  --border: #d6dee3;
  --accent: #0b6e99;
  --accent-text: #ffffff;
  --water: rgba(30, 150, 210, 0.22);
  --water-stroke: #1583b8;
  --grid: #e4eaee;
  --baseline: #6b7c86;
  --feature: #d9730d;
  --error: #c62828;
  --warn: #b26a00;
  --info: #2a6f97;
  --selected: #7b2cbf;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1519;
    --surface: #17222a;
    --text: #e4edf2;
    --muted: #93a6b2;
    --border: #2a3a44;
    --accent: #3aa6d8;
    --accent-text: #04141c;
    --water: rgba(58, 166, 216, 0.25);
    --water-stroke: #5cbbe6;
    --grid: #22313a;
    --baseline: #8da0ab;
    --feature: #f0953a;
    --error: #ef6b6b;
    --warn: #e0a43a;
    --info: #74b7dc;
    --selected: #c08bff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

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

input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  min-height: 44px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[aria-invalid="true"] { border-color: var(--error); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; margin: 0; white-space: nowrap; }
.topbar select { flex: 1; min-width: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger, .danger { color: var(--error); }

.menu { position: relative; }
.menu summary { list-style: none; font-size: 20px; }
.menu summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  display: flex;
  flex-direction: column;
  min-width: 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  overflow: hidden;
}
.menu-list button {
  text-align: left;
  padding: 12px 16px;
  border: 0;
  background: none;
  cursor: pointer;
}
.menu-list button + button { border-top: 1px solid var(--border); }

.banner { background: var(--warn); color: #fff; padding: 8px 16px; font-size: 14px; }

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 12px 16px 80px;
  display: grid;
  gap: 12px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card-title { font-size: 15px; font-weight: 650; margin: 0 0 10px; display: inline-flex; gap: 8px; align-items: center; }
details.card > summary { cursor: pointer; }
details.card > summary .card-title { margin: 0; }
details.card[open] > summary { margin-bottom: 10px; }

.muted { color: var(--muted); font-size: 14px; }
.pill { font-size: 13px; background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 1px 9px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.grid2 .span2 { grid-column: span 2; }

.field { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.field-label { font-size: 14px; color: var(--muted); }
.ftin { display: flex; align-items: center; gap: 6px; color: var(--text); }
.ftin input { flex: 1; min-width: 0; text-align: center; font-size: 18px; }

.setup { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; align-items: end; }

.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { flex: 1; text-align: center; min-height: 44px; display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--text); }
.seg label + label { border-left: 1px solid var(--border); }
.seg input { position: absolute; opacity: 0; pointer-events: none; width: auto; min-height: 0; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-text); font-weight: 600; }

.plot-card { padding: 8px; }
.plot-wrap { width: 100%; aspect-ratio: 4 / 3; max-height: 60vh; }
#plot { width: 100%; height: 100%; display: block; touch-action: manipulation; }
#plot text { font-family: system-ui, sans-serif; fill: var(--text); }
#plot .grid { stroke: var(--grid); stroke-width: 1; }
#plot .grid-label { fill: var(--muted); }
#plot .outline { fill: var(--water); stroke: var(--water-stroke); stroke-width: 2; stroke-linejoin: round; }
#plot .outline-open { fill: none; stroke: var(--water-stroke); stroke-width: 2; stroke-dasharray: 6 4; }
#plot .baseline { stroke: var(--baseline); stroke-width: 2; stroke-dasharray: 8 5; }
#plot .ref { fill: var(--text); }
#plot .ref-label { fill: var(--surface); font-weight: 700; }
#plot .pt { fill: var(--surface); stroke: var(--water-stroke); stroke-width: 2; cursor: pointer; }
#plot .pt.feature { stroke: var(--feature); }
#plot .pt.bad { stroke: var(--error); }
#plot .pt.sel { stroke: var(--selected); stroke-width: 3; }
#plot .pt-label { font-weight: 600; pointer-events: none; }
#plot .ray { stroke: var(--selected); stroke-width: 1.5; stroke-dasharray: 3 3; }
#plot .pending { fill: none; stroke: var(--selected); stroke-width: 2; stroke-dasharray: 4 3; }
#plot .empty-msg { fill: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 4px 4px; }
.stats div { background: var(--bg); border-radius: 8px; padding: 8px 10px; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 0; font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; }

.warnings { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.warnings li { padding: 8px 10px; border-radius: 8px; border-left: 4px solid; background: var(--bg); font-size: 14px; }
.warnings li[data-point] { cursor: pointer; }
.warnings .error { border-color: var(--error); }
.warnings .warn { border-color: var(--warn); }
.warnings .info { border-color: var(--info); }

.entry { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.entry .add { grid-column: span 2; font-size: 17px; min-height: 52px; }
.entry .field:nth-of-type(3) { grid-column: span 2; }
.hint { grid-column: span 2; margin: 0; font-size: 14px; color: var(--muted); min-height: 1.4em; }
.hint.error { color: var(--error); }

.points { list-style: none; margin: 0; padding: 0; }
.points li {
  display: grid;
  grid-template-columns: 2.2em 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 10px 4px;
  border-top: 1px solid var(--border);
  cursor: pointer;
}
.points li:first-child { border-top: 0; }
.points li.sel { background: color-mix(in srgb, var(--selected) 10%, transparent); border-radius: 8px; }
.points .num { font-weight: 700; text-align: center; }
.points .vals { font-variant-numeric: tabular-nums; }
.points .vals b { color: var(--muted); font-weight: 500; font-size: 13px; }
.points .note { display: block; font-size: 13px; color: var(--muted); }
.points .tag { font-size: 12px; border-radius: 999px; padding: 2px 8px; border: 1px solid var(--border); white-space: nowrap; }
.points .tag.feature { border-color: var(--feature); color: var(--feature); }
.points .flag { color: var(--error); font-weight: 700; margin-left: 4px; }
.points .flag.warn { color: var(--warn); }

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 16px;
  width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
dialog form { display: grid; gap: 12px; }
.dialog-row { display: flex; gap: 8px; flex-wrap: wrap; }
.dialog-row.end { justify-content: flex-end; }
.dialog-row .btn { flex: 1; }
.check { display: flex; gap: 10px; align-items: center; font-size: 15px; }
.check input { width: 22px; min-height: 22px; height: 22px; }
.help { padding-left: 20px; margin: 0; display: grid; gap: 8px; }

#printSheet { display: none; }

@media (max-width: 560px) {
  .topbar h1 { display: none; }
  .setup { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr 1fr; }
  .grid2 { grid-template-columns: 1fr; }
  .grid2 .span2 { grid-column: auto; }
}

@media print {
  :root { --text: #000; --muted: #444; --border: #999; }
  body { background: #fff; font-size: 11pt; }
  .topbar, main, .banner, dialog { display: none !important; }
  #printSheet { display: block; color: #000; }
  #printSheet h1 { font-size: 16pt; margin: 0 0 4pt; }
  #printSheet table { width: 100%; border-collapse: collapse; font-size: 9.5pt; margin-top: 8pt; }
  #printSheet th, #printSheet td { border: 1px solid #999; padding: 2pt 4pt; text-align: left; }
  #printSheet .meta td { border: 0; padding: 1pt 8pt 1pt 0; }
  #printSheet svg { width: 100%; height: 4.5in; border: 1px solid #999; margin-top: 8pt; }
  #printSheet svg text { fill: #000; font-family: sans-serif; }
  #printSheet svg .grid { stroke: #ddd; }
  #printSheet svg .outline { fill: #dceefa; stroke: #000; stroke-width: 1.5; }
  #printSheet svg .outline-open { fill: none; stroke: #000; stroke-dasharray: 6 4; }
  #printSheet svg .baseline { stroke: #000; stroke-dasharray: 8 5; }
  #printSheet svg .ref { fill: #000; }
  #printSheet svg .ref-label { fill: #fff; }
  #printSheet svg .pt { fill: #fff; stroke: #000; }
  #printSheet svg .pt.feature { stroke: #b35900; }
  #printSheet svg .ray, #printSheet svg .pending { display: none; }
  #printSheet tr { break-inside: avoid; }
}
