:root {
  color-scheme: light;
  --paper: #f3efe4;
  --card: #fffcf7;
  --ink: #1b1914;
  --muted: #5c564c;
  --line: #ddd4c4;
  --accent: #0e6b5e;
  --on-accent: #f3efe4;
  --poster: #14312d;
  --poster-ink: #f4f0e6;
  --poster-dim: #b7d4ce;
  --dot: rgba(28, 25, 21, 0.07);
  --note: #f3e0c4;
  --note-ink: #3d2a12;
  --danger: #8d3418;
  --serif: "iA Writer Quattro S", "Liberation Serif", Georgia, serif;
  --sans: "Adwaita Sans", "Liberation Sans", "Segoe UI", sans-serif;
  --mono: "iA Writer Mono S", "Liberation Mono", ui-monospace, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #121410;
  --card: #1c1e19;
  --ink: #f3efe4;
  --muted: #b7b0a2;
  --line: #34382f;
  --accent: #8fd0c4;
  --on-accent: #10211e;
  --poster: #0c2420;
  --poster-ink: #f4f0e6;
  --poster-dim: #9fc4bc;
  --dot: rgba(243, 239, 228, 0.05);
  --note: #3a2e1c;
  --note-ink: #f3e0c4;
  --danger: #f0a06a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  min-height: 100%;
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
  background-color: var(--paper);
  background-image: radial-gradient(var(--dot) 0.9px, transparent 0.9px);
  background-size: 18px 18px;
}

::selection { background: var(--accent); color: var(--on-accent); }

:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.skip {
  position: absolute;
  left: 12px;
  top: -48px;
  background: var(--card);
  color: var(--ink);
  padding: 8px 12px;
  border-radius: 8px;
  z-index: 2;
}
.skip:focus { top: 12px; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  max-width: 1040px;
  margin: 0 auto;
  padding: 16px 20px 8px;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.mark { width: 36px; height: 36px; flex: none; color: var(--poster); }
h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}
.tag { margin: 2px 0 0; color: var(--muted); font-size: 0.92rem; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }

.btn {
  font: inherit;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  min-height: 40px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.primary:focus-visible { outline-color: var(--ink); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.file { position: relative; display: inline-flex; align-items: center; }
.file input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}
.file:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.note {
  position: fixed;
  z-index: 5;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  width: max-content;
  max-width: min(440px, calc(100% - 24px));
  margin: 0;
  padding: 10px 14px;
  background: var(--note);
  color: var(--note-ink);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(28, 25, 21, 0.16);
}
.note:empty { display: none; }

.layout {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  grid-template-areas:
    "tools stage"
    "shelf stage";
  gap: 8px 18px;
  align-items: start;
  max-width: 1040px;
  margin: 0 auto;
  padding: 12px 20px 40px;
}
.tool-scroll { grid-area: tools; display: flex; flex-direction: column; gap: 4px; }
#stage { grid-area: stage; min-height: 420px; }
.shelf { grid-area: shelf; }

.tool {
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}
.tool-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tool-name { font-weight: 650; }
.tool-hint { display: block; color: var(--muted); font-size: 0.78rem; margin-top: 1px; }
.tool kbd {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
}
.tool[aria-current="page"] {
  background: var(--card);
  box-shadow: inset 3px 0 0 var(--accent);
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px 18px 16px;
}

.stage-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.eyebrow {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  color: var(--accent);
}
.stage-head h2 {
  margin: 2px 0 0;
  font-family: var(--serif);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}
.lede { margin: 6px 0 0; color: var(--muted); max-width: 46ch; }

.fields, .stack { margin-top: 14px; }
.fields { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}
.field.grow { grid-column: 1 / -1; }

input, textarea, select {
  font: inherit;
  color: inherit;
  text-transform: none;
  letter-spacing: 0;
  font-size: 1rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}
textarea { resize: vertical; min-height: 0; line-height: 1.35; }
input::placeholder, textarea::placeholder { color: var(--muted); }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.95rem;
  color: var(--ink);
}
.check input { width: 1.1rem; height: 1.1rem; min-height: 0; accent-color: var(--accent); }

.convert {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: end;
}
.convert .field { min-width: 0; }

.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.presets button[aria-pressed="true"] {
  background: var(--poster);
  color: var(--poster-ink);
  border-color: transparent;
}

.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.tallies { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.tallies li { display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto auto; gap: 6px; align-items: center; }
.count {
  min-width: 3ch;
  text-align: center;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.05rem;
}
.icon {
  font: inherit;
  color: inherit;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
}
.icon:hover { border-color: var(--accent); }
.ghost { padding: 0 10px; }

.result {
  margin-top: 14px;
  background: var(--poster);
  color: var(--poster-ink);
  border-radius: 14px;
  padding: 16px 16px 14px;
}
.figure {
  margin: 0;
  font-family: var(--serif);
  font-size: clamp(2rem, 5vw, 2.8rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  user-select: all;
}
.figure.note-size { font-family: var(--serif); font-size: 1.35rem; letter-spacing: -0.02em; user-select: text; }
.sub { margin: 8px 0 0; color: var(--poster-dim); }
.shares { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.shares li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  background: rgba(243, 239, 228, 0.08);
  border-radius: 8px;
}
.shares span:last-child { font-family: var(--mono); font-variant-numeric: tabular-nums; }

pre.json {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--mono);
  font-size: 0.86rem;
  line-height: 1.45;
}
.cases { display: flex; flex-direction: column; gap: 8px; }
.case-row { display: grid; grid-template-columns: 4.5rem minmax(0, 1fr) auto; gap: 8px; align-items: center; }
.case-k { color: var(--poster-dim); font-size: 0.78rem; letter-spacing: 0.04em; text-transform: uppercase; }
.case-row code {
  font-family: var(--mono);
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}
.copy {
  font: inherit;
  color: var(--poster-ink);
  background: transparent;
  border: 1px solid rgba(243, 239, 228, 0.35);
  border-radius: 999px;
  min-height: 36px;
  padding: 4px 10px;
  cursor: pointer;
}
.copy:hover { border-color: var(--poster-ink); }

.shelf-head h2 {
  margin: 8px 0;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 650;
}
.pin { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.pin input { min-width: 0; flex: 1 1 120px; background: var(--card); }
.empty { margin: 0 0 8px; color: var(--muted); font-size: 0.88rem; }
#shelf-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
#shelf-list:empty { display: none; }
.pin-row { display: flex; gap: 4px; }
.restore {
  flex: 1;
  min-width: 0;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}
.restore small { display: block; color: var(--muted); font-size: 0.75rem; }
.restore:hover { border-color: var(--accent); }
.shelf-io { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

@media (max-width: 800px) {
  .fields { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 720px) {
  .top { flex-wrap: wrap; padding: 14px 14px 6px; }
  .actions { width: 100%; }
  .actions .btn { flex: 1; }
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas: "tools" "stage" "shelf";
    padding: 8px 12px 32px;
  }
  .tool-scroll {
    flex-direction: row;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 6px;
  }
  .tool {
    width: auto;
    min-width: 0;
    flex: 0 0 auto;
    background: var(--card);
    border: 1px solid var(--line);
    scroll-snap-align: start;
    padding: 10px 14px;
  }
  .tool-hint { display: none; }
  .tool-scroll {
    scroll-snap-type: x mandatory;
    mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
  }
  .tool kbd { display: none; }
  .tool[aria-current="page"] { box-shadow: inset 0 -3px 0 var(--accent); }
  .shares { grid-template-columns: 1fr; }
  .convert { grid-template-columns: 1fr; }
  .case-row { grid-template-columns: 1fr auto; }
  .case-k { grid-column: 1 / -1; }
}

@media (max-width: 420px) {
  .fields { grid-template-columns: 1fr; }
  .tallies li { grid-template-columns: minmax(0, 1fr) auto auto auto; }
  .ghost { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
