/* Design notes
   Reference: a field notebook on a drafting table, not a SaaS dashboard.
   - flat card stock + hairline rules, no shadows, no blur, no gradients
   - one accent (vermilion, the margin-line red) used only as a selection tick
   - serif for titles, mono for readouts, system sans for controls
   - 2px corners; nothing is a pill                                              */

:root {
  --bg: #f4f0e6;
  --paper: #fbf9f3;
  --card: #f0ebde;
  --card2: #e7e1d1;
  --ink: #1c2233;
  --on-ink: #fbf9f3; /* text/tick colour that sits on a solid --ink fill */
  --ink2: #565b6d;
  --ink3: #8b8a8c;
  --rule: #d6cfbd;
  --red: #c2432b;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1915;
    --card: #25221c;
    --card2: #302c24;
    --ink: #ece7d8;
    --on-ink: #1b1915;
    --ink2: #b3ad9b;
    --ink3: #7d7868;
    --rule: #3b372d;
    --red: #e0634a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 var(--sans);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
#app { height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: 0.32; cursor: default; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.ico { display: block; flex: none; }
[hidden] { display: none !important; }

/* ---- buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 40px; padding: 0 14px;
  background: transparent; border: 1px solid var(--ink); border-radius: 2px;
  font-size: 14px; font-weight: 500;
}
.btn.solid { background: var(--ink); color: var(--on-ink); }
.btn.danger { border-color: var(--red); color: var(--red); }
.btn:active { transform: translateY(1px); }
.iconbtn, .back {
  display: grid; place-items: center; min-width: 44px; height: 44px;
  background: none; border: 0; border-radius: 2px; color: var(--ink);
}
.back { display: flex; align-items: center; gap: 2px; padding-right: 10px; font-weight: 500; }
.iconbtn:active, .back:active { background: var(--card2); }

/* ---- home --------------------------------------------------------------- */
.home {
  height: 100dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: calc(var(--sat) + 30px) max(22px, var(--sar)) calc(var(--sab) + 40px) max(22px, var(--sal));
  max-width: 1100px; margin: 0 auto;
}
.home header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 26px; padding-bottom: 14px; border-bottom: 1px solid var(--rule); }
.home h1 { margin: 0; font: italic 500 40px/1 var(--serif); letter-spacing: -0.01em; }
.count { margin: 8px 0 0; font: 12px var(--mono); letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink2); }
.head-actions { display: flex; align-items: center; gap: 10px; }
.install { margin: 0; font-size: 13px; color: var(--ink2); max-width: 24ch; }

.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(146px, 1fr)); gap: 24px 16px; }
.tile { position: relative; }
.tile .open, .tile.new { display: block; width: 100%; padding: 0; background: none; border: 0; text-align: left; }
.plate {
  display: block; aspect-ratio: 16 / 11; overflow: hidden;
  background: var(--paper); border: 1px solid var(--rule); border-radius: 2px;
}
.plate img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.new .plate { display: grid; place-items: center; border-style: dashed; border-color: var(--ink3); background: transparent; color: var(--ink2); }
.tile .open:active .plate, .tile.new:active .plate { border-color: var(--ink); }
.cap { display: block; padding: 9px 2px 0; }
.cap b { display: block; font: italic 500 17px/1.25 var(--serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cap b.untitled { color: var(--ink3); }
.cap small { display: block; margin-top: 3px; font: 11px var(--mono); letter-spacing: 0.04em; color: var(--ink2); }
.del { position: absolute; right: 4px; top: 4px; width: 38px; height: 38px; display: grid; place-items: center; background: var(--paper); border: 1px solid var(--rule); border-radius: 2px; color: var(--ink2); opacity: 0.0; }
.tile:hover .del, .tile:focus-within .del { opacity: 1; }
@media (hover: none) { .del { opacity: 0.85; } }

/* ---- editor ------------------------------------------------------------- */
.editor { position: fixed; inset: 0; background: var(--paper); overflow: hidden; }
.stage { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
.layer { position: absolute; inset: 0; width: 100%; height: 100%; display: block; background: transparent; }
.layer.base { background: #fbf9f3; } /* paper shows even if a repaint is ever late */
.layer.live { pointer-events: none; }
.layer.live.hl { mix-blend-mode: multiply; }
.ring {
  position: absolute; pointer-events: none; z-index: 2; border: 1.5px solid var(--ink); border-radius: 50%;
  transform: translate(-50%, -50%); background: rgba(255, 255, 255, 0.35);
}

.topbar {
  position: absolute; z-index: 5; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 6px;
  height: calc(48px + var(--sat)); padding: var(--sat) max(6px, var(--sar)) 0 max(6px, var(--sal));
  background: var(--card); border-bottom: 1px solid var(--rule);
}
.title {
  flex: 1; min-width: 0; height: 40px; padding: 0 8px; background: transparent; border: 0;
  border-bottom: 1px dashed transparent; border-radius: 0;
  font: italic 500 19px var(--serif); color: var(--ink); -webkit-user-select: text; user-select: text;
}
.title::placeholder { color: var(--ink3); }
.title:focus { outline: none; border-bottom-color: var(--ink3); }
.readout {
  max-width: 34%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font: 11px var(--mono); letter-spacing: 0.07em; text-transform: uppercase; color: var(--ink2);
}
.readout.warn { color: var(--red); }

.rail {
  position: absolute; z-index: 4; left: 0; top: calc(48px + var(--sat) + 14px);
  display: flex; flex-direction: column; padding: 4px 0 4px var(--sal);
  background: var(--card); border: 1px solid var(--rule); border-left: 0;
}
.tool { position: relative; width: 54px; height: 52px; display: grid; place-items: center; background: none; border: 0; color: var(--ink3); padding: 0; }
.tool[aria-pressed="true"] { color: var(--ink); }
.tool[aria-pressed="true"]::before { content: ""; position: absolute; left: 0; top: 9px; bottom: 9px; width: 3px; background: var(--red); }
.tool .band { position: absolute; left: 17px; right: 17px; bottom: 7px; height: 3px; background: var(--c, transparent); }
.tool[data-tool="move"] .band, .tool[data-tool="eraser"] .band { display: none; }
.tool:active:not(:disabled) { background: var(--card2); }
.rail .sep { height: 1px; margin: 4px 12px; background: var(--rule); }

.tray {
  position: absolute; z-index: 4; left: calc(54px + var(--sal)); top: calc(48px + var(--sat) + 14px);
  width: 252px; max-height: calc(100% - 160px); overflow: auto;
  padding: 12px 14px 14px; background: var(--card); border: 1px solid var(--rule);
}
.tray h4 { margin: 0 0 6px; font: 10.5px var(--mono); letter-spacing: 0.13em; text-transform: uppercase; color: var(--ink3); }
.tray h4:not(:first-child) { margin-top: 14px; }
.note { margin: 10px 0 0; font-size: 13px; color: var(--ink2); }
.inks { display: grid; }
.ink {
  display: flex; align-items: center; gap: 10px; height: 40px; padding: 0 8px;
  background: none; border: 0; text-align: left; color: var(--c); position: relative;
}
.ink span { color: var(--ink); font-size: 14px; }
.ink[aria-checked="true"] { background: var(--card2); }
.ink[aria-checked="true"]::before { content: ""; position: absolute; left: 0; top: 6px; bottom: 6px; width: 3px; background: var(--red); }
.nibs { display: flex; border: 1px solid var(--rule); }
.nib {
  flex: 1; height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 7px;
  background: none; border: 0; border-right: 1px solid var(--rule); padding: 0; color: var(--ink);
}
.nib:last-child { border-right: 0; }
.nib i { display: block; background: var(--ink); }
.nib.line i { width: 26px; border-radius: 99px; }
.nib.disc i { border-radius: 50%; background: transparent; border: 1.5px solid var(--ink); }
.nib em { font: normal 10px var(--mono); color: var(--ink2); letter-spacing: 0.04em; }
.nib[aria-checked="true"] { background: var(--card2); box-shadow: inset 0 -3px 0 var(--red); }

/* options sheet */
.scrim { position: absolute; z-index: 6; inset: 0; background: rgba(24, 20, 14, 0.3); opacity: 0; pointer-events: none; transition: opacity 0.2s; }
.scrim.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: absolute; z-index: 7; top: 0; right: 0; bottom: 0; width: min(380px, 100%);
  background: var(--card); border-left: 1px solid var(--rule);
  transform: translateX(102%); transition: transform 0.24s cubic-bezier(0.2, 0.7, 0.2, 1);
  display: flex; flex-direction: column; padding-top: var(--sat);
}
.sheet.open { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 6px 20px; border-bottom: 1px solid var(--rule); }
.sheet-head h2 { margin: 0; font: italic 500 24px var(--serif); }
.sheet-body { overflow-y: auto; padding: 4px 20px calc(var(--sab) + 28px); -webkit-overflow-scrolling: touch; }
.sheet section { padding: 18px 0 20px; border-bottom: 1px solid var(--rule); }
.sheet section:last-child { border-bottom: 0; }
.sheet h3 { margin: 0 0 12px; font: 10.5px var(--mono); letter-spacing: 0.13em; text-transform: uppercase; color: var(--ink3); font-weight: 400; }

.papers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.paper { padding: 0; background: none; border: 0; text-align: center; }
.paper i { display: block; height: 52px; background: var(--paper); border: 1px solid var(--rule); margin-bottom: 6px; }
.paper span { font: 11px var(--mono); letter-spacing: 0.04em; color: var(--ink2); }
.paper[aria-checked="true"] i { border-color: var(--ink); box-shadow: 0 0 0 1px var(--ink); }
.paper[aria-checked="true"] span { color: var(--ink); }
.p-lines i { background: linear-gradient(90deg, transparent 13px, rgba(190, 62, 48, 0.4) 13px 14px, transparent 14px), repeating-linear-gradient(to bottom, transparent 0 9px, rgba(58, 84, 128, 0.35) 9px 10px), var(--paper); }
.p-grid i { background: repeating-linear-gradient(to right, transparent 0 9px, rgba(58, 84, 128, 0.3) 9px 10px), repeating-linear-gradient(to bottom, transparent 0 9px, rgba(58, 84, 128, 0.3) 9px 10px), var(--paper); }
.p-dots i { background: radial-gradient(circle, rgba(58, 84, 128, 0.7) 1px, transparent 1.4px) 0 0 / 10px 10px, var(--paper); }

.row { display: flex; align-items: flex-start; gap: 12px; padding: 9px 0; }
.row.opt { cursor: pointer; }
.row .grow { flex: 1; min-width: 0; }
.row b { display: block; font-weight: 500; font-size: 15px; }
.row small { display: block; margin-top: 2px; font-size: 12.5px; line-height: 1.4; color: var(--ink2); }
.row input { position: absolute; opacity: 0; pointer-events: none; }
.mark { flex: none; width: 18px; height: 18px; margin-top: 2px; border: 1.5px solid var(--ink2); border-radius: 2px; position: relative; }
.mark.radio { border-radius: 50%; }
.row input:checked + .mark { border-color: var(--ink); background: var(--ink); }
.row input:checked + .mark::after { content: ""; position: absolute; left: 4.5px; top: 1px; width: 5px; height: 9px; border: solid var(--on-ink); border-width: 0 2px 2px 0; transform: rotate(40deg); }
.row input:checked + .mark.radio::after { left: 4px; top: 4px; width: 6px; height: 6px; border: 0; border-radius: 50%; background: var(--on-ink); transform: none; }
.row input:focus-visible + .mark { outline: 2px solid var(--ink); outline-offset: 2px; }
.stepper-row { align-items: center; }
.stepper { display: flex; align-items: stretch; border: 1px solid var(--rule); }
.stepper button { width: 38px; height: 38px; display: grid; place-items: center; background: none; border: 0; }
.stepper button:active { background: var(--card2); }
.stepper .val { min-width: 62px; display: grid; place-items: center; font: 13px var(--mono); border-inline: 1px solid var(--rule); }
.sheet .btn { margin-top: 10px; }
.actions { display: grid; gap: 8px; }
.actions .btn { margin: 0; justify-content: flex-start; }

.padbox { margin-bottom: 4px; }
.pad { position: relative; height: 190px; background: var(--paper); border: 1px dashed var(--ink3); touch-action: none; }
.padlog { list-style: none; margin: 8px 0 0; padding: 0; min-height: 7.6em; font: 11.5px/1.65 var(--mono); }
.padlog li::before { content: "› "; color: var(--ink3); }
.padlog li.no { color: var(--red); }

/* dialog + toast */
.dlg-wrap { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 20px; background: rgba(24, 20, 14, 0.4); }
.dlg { width: min(360px, 100%); padding: 22px 22px 18px; background: var(--card); border: 1px solid var(--ink); border-radius: 2px; }
.dlg h2 { margin: 0 0 8px; font: italic 500 22px var(--serif); }
.dlg p { margin: 0 0 20px; color: var(--ink2); font-size: 14.5px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; }
.choices { display: grid; gap: 8px; margin-bottom: 16px; }
.choice { display: block; width: 100%; padding: 12px 14px; background: transparent; border: 1px solid var(--rule); border-radius: 2px; text-align: left; }
.choice b { display: block; font-weight: 500; font-size: 15px; }
.choice small { display: block; margin-top: 2px; font-size: 12.5px; line-height: 1.4; color: var(--ink2); }
.choice:active { border-color: var(--ink); background: var(--card2); }
#toasts { position: fixed; z-index: 60; left: 0; right: 0; bottom: calc(var(--sab) + 18px); display: grid; justify-items: center; gap: 8px; pointer-events: none; }
.toast { max-width: min(440px, calc(100% - 32px)); padding: 10px 14px; background: var(--ink); color: var(--on-ink); border-radius: 2px; font-size: 14px; opacity: 0; transform: translateY(6px); transition: opacity 0.2s, transform 0.2s; }
.toast.in { opacity: 1; transform: none; }

/* ---- narrow / portrait: rail moves to the bottom ------------------------- */
@media (max-width: 720px) {
  .rail {
    left: 0; right: 0; top: auto; bottom: 0; flex-direction: row; justify-content: center;
    padding: 0 var(--sar) var(--sab) var(--sal); border: 0; border-top: 1px solid var(--rule);
  }
  .tool { width: auto; flex: 1; max-width: 76px; height: 54px; }
  .tool[aria-pressed="true"]::before { left: 12px; right: 12px; top: 0; bottom: auto; width: auto; height: 3px; }
  .rail .sep { width: 1px; height: auto; margin: 10px 4px; }
  .tray { left: 0; right: 0; top: auto; bottom: calc(54px + var(--sab)); width: auto; max-height: 52dvh; border-width: 1px 0 0; padding-inline: max(16px, var(--sal)); }
  .inks { grid-template-columns: 1fr 1fr; column-gap: 12px; }
  body:has(.editor) #toasts { bottom: calc(var(--sab) + 70px); }
  .readout { display: none; }
  .back span { display: none; }
  .back { padding-right: 0; }
}
@media (max-height: 520px) and (min-width: 721px) {
  .tool { height: 42px; }
  .rail .sep { margin: 2px 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .sheet, .scrim, .toast { transition: none; }
}
