:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --field: #f4f5f7;
  --text: #16181d;
  --muted: #5d6470;
  --line: #dde0e6;
  --accent: #2f6fed;
  --accent-hover: #2560d6;
  --accent-text: #ffffff;
  --accent-soft: #e8effd;
  --warn-bg: #fff5e0;
  --warn-text: #7a4b00;
  --ok-bg: #e7f6ec;
  --ok-text: #135c2c;
  --err-bg: #fdecec;
  --err-text: #8a1c1c;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05);
  color-scheme: light;
}

/* Dark tokens: system preference unless the visitor picked light, or an explicit dark choice. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317;
    --card: #1a1d23;
    --field: #22262d;
    --text: #e9ebef;
    --muted: #9aa1ad;
    --line: #2f343d;
    --accent: #5b8cff;
    --accent-hover: #7aa2ff;
    --accent-text: #0b0d12;
    --accent-soft: #1f2a44;
    --warn-bg: #3a2c0f;
    --warn-text: #f3cf86;
    --ok-bg: #12321e;
    --ok-text: #8fe0aa;
    --err-bg: #3b1616;
    --err-text: #f2a3a3;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111317;
  --card: #1a1d23;
  --field: #22262d;
  --text: #e9ebef;
  --muted: #9aa1ad;
  --line: #2f343d;
  --accent: #5b8cff;
  --accent-hover: #7aa2ff;
  --accent-text: #0b0d12;
  --accent-soft: #1f2a44;
  --warn-bg: #3a2c0f;
  --warn-text: #f3cf86;
  --ok-bg: #12321e;
  --ok-text: #8fe0aa;
  --err-bg: #3b1616;
  --err-text: #f2a3a3;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

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

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 40px 16px 24px;
}

.title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
h1 { margin: 0 0 6px; font-size: 28px; letter-spacing: -0.02em; }
.lead { margin: 0 0 20px; color: var(--muted); }

/* Theme toggle: shows the moon in light mode and the sun in dark mode. */
.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  flex: none;
}
.icon-btn:hover { border-color: var(--muted); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.i-sun { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }

/* Mode tabs */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; margin-bottom: 16px; }

.tabs {
  display: inline-flex;
  padding: 4px;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.tabs { flex: 1 1 320px; }
.tabs label { flex: 1; position: relative; cursor: pointer; }
.tabs input { position: absolute; top: 0; left: 0; margin: 0; opacity: 0; pointer-events: none; }
.tabs span {
  display: block;
  padding: 9px 14px;
  border-radius: 8px;
  text-align: center;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  transition: background-color 0.15s, color 0.15s;
}
.tabs label:hover span { color: var(--text); }
.tabs input:checked + span { background: var(--accent); color: var(--accent-text); }
.tabs input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 16px;
  display: grid;
  gap: 14px;
  box-shadow: var(--shadow);
}

.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row.nowrap { flex-wrap: nowrap; }
.row.nowrap .field.grow { flex-basis: 0; }
.field { display: grid; gap: 6px; min-width: 0; }
.field.grow { flex: 1 1 220px; }
.field.ext { flex: 0 1 200px; }
.field.unit { flex: 0 0 90px; }
.field > span, .field > label { font-size: 13px; font-weight: 600; color: var(--muted); }

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

input:not([type]), input[inputmode], select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--field);
}
input[inputmode] { font-variant-numeric: tabular-nums; }

/* Arrow steppers: the field and a stacked pair of buttons read as one control. */
.stepper { display: flex; align-items: stretch; border-radius: 9px; }
.stepper input { flex: 1 1 auto; min-width: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; }
/* The focus ring goes around the whole control, not just the text box. */
.stepper input:focus-visible { outline: none; }
.stepper:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.stepper-btns {
  flex: none;
  display: grid;
  grid-template-rows: 1fr 1fr;
  width: 44px;
  border: 1px solid var(--line);
  border-left: 0;
  border-radius: 0 9px 9px 0;
  background: var(--field);
  overflow: hidden;
}
.stepper-btns button {
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  touch-action: manipulation;
}
.stepper-btns button:first-child { border-bottom: 1px solid var(--line); }
.stepper-btns button:hover { background: var(--accent-soft); color: var(--accent); }
.stepper-btns button:active { background: var(--accent-soft); color: var(--accent); }
.stepper-btns button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.stepper-btns svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }

input:focus-visible, select:focus-visible, button:focus-visible, .drop:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Quick size presets */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: -4px; }
.chips button {
  padding: 5px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.chips button:hover { border-color: var(--accent); color: var(--text); }
.chips button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }

/* Fill choice: three cards with a short legend */
.fill { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.fill label { position: relative; cursor: pointer; display: flex; }
.fill input { position: absolute; top: 0; left: 0; margin: 0; opacity: 0; pointer-events: none; }
.fill-card {
  display: grid;
  align-content: start;
  gap: 4px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  transition: border-color 0.15s, background-color 0.15s;
}
.fill label:hover .fill-card { border-color: var(--accent); }
.fill input:checked + .fill-card { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.fill input:focus-visible + .fill-card { outline: 2px solid var(--accent); outline-offset: 2px; }
.fill-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-bottom: 2px; }
.fill-sample {
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--muted);
  font: 600 11px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.04em;
}
.fill b { font-size: 14px; color: var(--text); }
.fill em {
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--ok-bg);
  color: var(--ok-text);
  font-size: 11px;
  font-style: normal;
  font-weight: 600;
  white-space: nowrap;
}
.fill small { font-size: 12.5px; line-height: 1.4; color: var(--muted); }
.fill.locked label { cursor: not-allowed; }
.fill.locked .fill-card { opacity: 0.45; }
.fill.locked label:hover .fill-card { border-color: var(--line); }
.fill.locked input:checked + .fill-card { border-color: var(--line); background: var(--field); box-shadow: none; }
.fill-lock { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }

.check { display: flex; gap: 10px; align-items: center; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }

button {
  padding: 12px 18px;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
}
button.primary { background: var(--accent); color: var(--accent-text); transition: background-color 0.15s; }
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.ghost { background: transparent; border-color: var(--line); justify-self: start; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

.drop {
  display: grid;
  place-items: center;
  gap: 2px;
  padding: 28px 16px;
  border: 2px dashed var(--line);
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  overflow-wrap: anywhere;
  transition: border-color 0.15s, background-color 0.15s;
}
.drop:hover { border-color: var(--muted); }
.drop svg { width: 28px; height: 28px; margin-bottom: 6px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.drop strong { color: var(--text); }
.drop.has-file { border-style: solid; border-color: var(--accent); background: var(--accent-soft); padding: 18px 16px; }
.drop.has-file svg { display: none; }
.drop.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }

/* Drop anywhere on the page */
.page-drop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(2px);
  pointer-events: none;
}
.page-drop p {
  margin: 0;
  padding: 28px 32px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: var(--card);
  font-weight: 600;
  text-align: center;
}

.note {
  margin: 0;
  padding: 10px 12px;
  border-radius: 9px;
  background: var(--field);
  color: var(--muted);
  font-size: 14px;
}
.note:empty { display: none; }
.note.warn { background: var(--warn-bg); color: var(--warn-text); }
.note.err { background: var(--err-bg); color: var(--err-text); }

.bar { height: 8px; border-radius: 4px; background: var(--field); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
/* Unknown progress: a sliding stripe instead of an empty bar. */
.bar.indeterminate > div { width: 30% !important; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
@media (prefers-reduced-motion: reduce) {
  .bar.indeterminate > div { animation: none; width: 100% !important; opacity: 0.4; }
  body, .tabs span, .drop { transition: none; }
}

#s-text { margin: 0; }
#s-result { display: grid; gap: 10px; }
#s-result .ok { padding: 12px; border-radius: 9px; background: var(--ok-bg); color: var(--ok-text); overflow-wrap: anywhere; }
#s-result .ok b { font-variant-numeric: tabular-nums; }
#s-result .ok .sides { display: block; margin-top: 4px; opacity: .85; font-variant-numeric: tabular-nums; }
#s-result .err { padding: 12px; border-radius: 9px; background: var(--err-bg); color: var(--err-text); }
.primary-link,
#s-result a.primary {
  display: inline-block;
  justify-self: start;
  padding: 12px 18px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  text-decoration: none;
}
.primary-link:hover,
#s-result a.primary:hover { background: var(--accent-hover); }

footer { color: var(--muted); font-size: 13px; }
footer a { color: inherit; }

@media (max-width: 480px) {
  main { padding-top: 24px; }
  h1 { font-size: 24px; }
  .tabs { width: 100%; }
  .fill { grid-template-columns: 1fr; }
  .tabs span { padding: 9px 8px; }
  .field.ext { flex: 1 1 100%; }
  button.primary { width: 100%; }
}
