:root {
  --ink: #1F2A30;
  --paper: #F4F3EE;
  --card: #FFFFFF;
  --line: #D8D6CD;   /* dividers and cards: decoration only */
  --edge: #747D84;   /* the outline of anything you can tap or type in: 3 to 1 or better against white and paper */
  --muted: #5B6770;
  --focus: #0B63CE;
  --danger: #B3261E;
  --tile: 116px;
  --label: 15px;
}
body.size-small { --tile: 90px;  --label: 13px; }
body.size-large { --tile: 150px; --label: 19px; }

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.35;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 4px solid var(--focus); outline-offset: 2px; }

/* ---- top: what was just said, and the tools ---- */
.top {
  display: flex; flex-wrap: wrap; align-items: stretch; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--ink); color: #fff;
}
.said { flex: 1 1 320px; display: flex; align-items: center; gap: 10px; min-width: 0; }
.said p { margin: 0; flex: 1; font-size: 24px; font-weight: 800; line-height: 1.15; overflow-wrap: anywhere; }
.said p.idle { font-weight: 600; font-size: 18px; opacity: 0.8; }
.tools { display: flex; gap: 6px; align-items: center; }
.top button {
  border: 2px solid rgba(255, 255, 255, 0.45); background: transparent; color: #fff;
  border-radius: 12px; padding: 10px 14px; font-weight: 700; font-size: 15px; white-space: nowrap;
}
.top button:disabled { opacity: 0.35; cursor: default; }
.top button.on { background: #fff; color: var(--ink); border-color: #fff; }
.top :focus-visible { outline-color: #FFD54F; }

.edit-hint { margin: 0; padding: 8px 14px; background: #FFF4CC; border-bottom: 1px solid #E6D48A; font-weight: 700; font-size: 15px; }

/* ---- the board ---- */
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.board { padding: 14px 14px 0; columns: calc(var(--tile) * 3 + 56px); column-gap: 14px; }

.section {
  --c: var(--muted);
  break-inside: avoid;
  margin: 0 0 14px;
  padding: 10px 12px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 8px solid var(--c);
  border-radius: 16px;
}
.section.urgent { column-span: all; border: 2px solid var(--c); border-left-width: 8px; }
.section.urgent .tiles { grid-template-columns: repeat(auto-fit, minmax(var(--tile), 1fr)); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 0 0 8px; }
.section h2 { margin: 0; font-size: 16px; font-weight: 800; color: var(--c); }
.section-edit { border: 2px solid var(--edge); background: #fff; border-radius: 10px; padding: 6px 10px; font-size: 13px; font-weight: 700; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--tile), 1fr)); gap: 8px; }

.tile {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: calc(var(--tile) * 1.04);
  padding: 8px 6px;
  background: #fff;
  border: 2px solid var(--c);
  border-bottom-width: 5px;
  border-radius: 14px;
  font-size: var(--label); font-weight: 800; line-height: 1.15; text-align: center;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: transform 80ms ease;
}
.tile img { width: 62%; max-width: 110px; aspect-ratio: 1; object-fit: contain; pointer-events: none; }
.tile .label { overflow-wrap: anywhere; }
.tile.words-only .label { font-size: calc(var(--label) * 1.35); }
.tile.words-only { min-height: calc(var(--tile) * 0.62); }
.tile:active { transform: translateY(2px); border-bottom-width: 3px; }
.tile.speaking { background: var(--c); color: #fff; }
.tile.speaking img { background: #fff; border-radius: 10px; }

.editing .tile::after {
  content: "Change"; position: absolute; top: 4px; right: 4px;
  padding: 2px 6px; border-radius: 8px; background: var(--ink); color: #fff; font-size: 10px; font-weight: 700;
}
.tile.add, .add-section {
  border: 2px dashed var(--muted); background: transparent; color: var(--muted); font-weight: 800;
}
.editing .tile.add::after { content: none; }
.add-section { display: block; width: 100%; padding: 16px; border-radius: 16px; margin: 0 0 14px; font-size: 16px; break-inside: avoid; }

/* ---- always on screen ---- */
.pinned {
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--card); border-top: 2px solid var(--line);
  display: flex; flex-direction: column; gap: 6px;
}
.pinned .section { margin: 0; padding: 0; border: 0; background: none; border-radius: 0; }
.pinned .section-head { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.pinned .tiles { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); }
.pinned .tile { flex-direction: row; gap: 8px; min-height: 60px; padding: 6px 8px; }
.pinned .tile img { width: 40px; flex: none; }

/* ---- one screen, no scrolling. The sizes here must match FIT in data.js. ---- */
.fit .scroll { overflow: hidden; }
.fit .board { columns: auto; height: 100%; padding: 8px 10px; display: flex; flex-direction: column; gap: 8px; }
.fit .shelf { flex: 1 1 0; min-height: 0; display: flex; gap: 8px; }
.fit .board .section {
  flex: 1 1 0; min-width: 0; min-height: 0; margin: 0; padding: 5px 7px 7px;
  display: flex; flex-direction: column;
  border-width: 1px 1px 1px 6px; border-radius: 12px;
}
.fit .board .section.urgent { border-color: var(--c); background: #FFF6F5; }
.fit .board .section-head { margin: 0 0 3px; }
.fit .board h2 { font-size: 14px; line-height: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fit .board .tiles {
  flex: 1; min-height: 0; gap: 5px;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
}
.fit .board .tile {
  min-height: 0; min-width: 0; padding: 3px; gap: 1px; overflow: hidden;
  border-bottom-width: 4px; border-radius: 10px;
  font-size: var(--fit-label); line-height: 1.08;
}
.fit .board .tile img { flex: 1 1 0; min-height: 0; width: 100%; max-width: none; aspect-ratio: auto; }
.fit .board .tile.words-only .label { font-size: calc(var(--fit-label) * 2); }
.measuring .label { overflow-wrap: normal; }   /* while trying a size, a word too long for its tile counts as not fitting */
.fit .pinned { padding-top: 6px; padding-bottom: 4px; }
.fit .pinned .tile { min-height: 52px; font-size: clamp(15px, 1.7vw, 20px); }
.fit .credit { padding: 0 12px calc(3px + env(safe-area-inset-bottom)); background: var(--card); font-size: 10px; line-height: 13px; }
.fit .credit p { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.credit { padding: 2px 16px 16px; color: var(--muted); font-size: 12px; }
.credit p { margin: 0; }
a { color: inherit; }
.noscript { padding: 40px; text-align: center; font-size: 18px; }

/* ---- pop-ups ---- */
dialog:not([open]) { display: none; }
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto;
  padding: 18px; border: 0; border-radius: 18px; color: var(--ink); background: #fff;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}
dialog::backdrop { background: rgba(20, 28, 32, 0.55); }
dialog h2 { margin: 0 0 12px; font-size: 21px; }
dialog h3 { margin: 18px 0 8px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 16px; }
.field { display: flex; flex-direction: column; gap: 5px; margin: 0 0 12px; font-weight: 700; font-size: 15px; }
.field input[type="text"], .field input[type="search"], .field select {
  width: 100%; min-height: 48px; padding: 10px 12px; border: 2px solid var(--edge); border-radius: 12px; background: #fff;
  font-size: 17px; font-weight: 600;
}
.field input[type="color"] { width: 72px; height: 48px; padding: 2px; border: 2px solid var(--edge); border-radius: 12px; background: #fff; }
.field input[type="range"] { width: 100%; height: 44px; }
.hint { margin: 0 0 10px; color: var(--muted); font-size: 15px; font-weight: 500; }
.error { margin: 0 0 10px; color: var(--danger); font-weight: 700; font-size: 15px; }
::placeholder { color: var(--muted); opacity: 1; }
.error:empty, .hint:empty { display: none; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 10px; }
.row.end { justify-content: flex-end; margin: 14px 0 0; }
/* The closing buttons stay in view however long the pop-up is. */
dialog:not(.draw) .row.end { position: sticky; bottom: -18px; margin: 14px -18px -18px; padding: 12px 18px; background: #fff; border-top: 1px solid var(--line); }
dialog button, .draw-bar button {
  min-height: 48px; padding: 10px 16px; border-radius: 12px; border: 2px solid var(--edge); background: #fff; font-weight: 700; font-size: 16px;
}
button.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
button.danger { border-color: var(--danger); color: var(--danger); }
button.danger.armed { background: var(--danger); color: #fff; }

.pic-row { display: flex; gap: 8px; align-items: center; }
.pic-row input { flex: 1; min-width: 0; }
.pic-now {
  flex: none; width: 56px; height: 56px; border: 2px solid var(--edge); border-radius: 12px;
  display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--muted); text-align: center;
}
.pic-now img { width: 46px; height: 46px; object-fit: contain; }
.pic-results { display: grid; grid-template-columns: repeat(auto-fill, minmax(68px, 1fr)); gap: 6px; margin: 0 0 8px; }
.pic-results:empty { display: none; }
.pic-results button { min-height: 0; padding: 4px; aspect-ratio: 1; }
.pic-results button[aria-pressed="true"] { border-color: var(--focus); background: #E7F0FD; }
.pic-results img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }

.history-list { display: flex; flex-direction: column; gap: 6px; }
.history-list button { display: flex; justify-content: space-between; gap: 12px; text-align: left; width: 100%; }
.history-list time { color: var(--muted); font-weight: 500; font-size: 13px; white-space: nowrap; }

/* ---- write or draw ---- */
.draw { width: 100vw; max-width: none; height: 100vh; height: 100dvh; max-height: none; margin: 0; padding: 0; border-radius: 0; overflow: hidden; }
.draw[open] { display: flex; flex-direction: column; }
.draw-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px; background: var(--paper); border-bottom: 2px solid var(--line);
}
.spacer { flex: 1; }
.pens { display: flex; gap: 8px; }
.draw-bar .pen { width: 44px; height: 44px; min-height: 0; padding: 0; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px var(--edge); }
.draw-bar .pen[aria-pressed="true"] { box-shadow: 0 0 0 4px var(--ink); }
.pen-size { display: flex; align-items: center; gap: 6px; font-size: 15px; font-weight: 700; }
.pen-size input { width: 100px; }
#canvas { flex: 1; width: 100%; min-height: 0; display: block; touch-action: none; cursor: crosshair; }

/* ---- small screens ---- */
@media (max-width: 560px) {
  .said p { font-size: 20px; }
  .tools { width: 100%; }
  .tools button { flex: 1; padding: 9px 4px; font-size: 14px; }
  .board { padding: 10px 10px 0; }
  .pinned .tile { flex-direction: column; gap: 2px; font-size: 12px; min-height: 66px; }
  .pinned .tile img { width: 30px; }
}

@media (prefers-reduced-motion: reduce) { .tile { transition: none; } }

/* ---- paper ---- */
@media print {
  html, body { height: auto; }
  body { display: block; background: #fff; }
  .top, .edit-hint, dialog, .add-section, .tile.add, .section-edit { display: none !important; }
  .editing .tile::after { content: none; }
  .scroll, .fit .scroll { overflow: visible; }
  .board { columns: 2; padding: 0; }
  .fit .board { height: auto; }
  .fit .shelf { flex: none; break-inside: avoid; }
  .fit .board .tile { min-height: 74px; }
  .fit .board .tile img { flex: none; height: 44px; }
  .fit .credit p { white-space: normal; }
  .section, .tile { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .pinned { border-top: 0; padding: 0; break-inside: avoid; }
  .credit { padding: 8px 0 0; color: #000; }
}
