:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #d9d6cf;
  --focus: #2f6fdf;

  --pron-bg: #fff4bf; --pron: #d9a900;
  --verb-bg: #dcf2d3; --verb: #3f9e43;
  --desc-bg: #dbe8fb; --desc: #3a78cf;
  --noun-bg: #ffe2c4; --noun: #e57d17;
  --social-bg: #fde0ec; --social: #d6508e;
  --neg-bg: #fbd9d9; --neg: #d33131;
  --q-bg: #ebdffa; --q: #8455cf;
  --folder-bg: #ebe9e4; --folder: #8c8a84;

  --gap: 10px;
  --radius: 18px;
  --bar-h: 104px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

/* The board stays light in dark mode on purpose: symbols are drawn on white and
   the colour coding must look the same everywhere. Only the chrome dims. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --bg: #2a2a2c; --line: #4a4a4e; --muted: #b8b8bd; }
}
:root[data-theme="dark"] { --bg: #2a2a2c; --line: #4a4a4e; --muted: #b8b8bd; }

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  /* Child-proofing: no text selection, callouts, zoom or rubber-band scroll */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
  overflow: hidden;
}

img { -webkit-user-drag: none; pointer-events: none; }

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

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 10px) calc(var(--safe-l) + 12px);
  gap: 8px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  gap: 10px;
  height: var(--bar-h);
  flex: none;
}

.bar-btn {
  flex: none;
  width: 84px;
  border: 3px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  display: grid;
  place-items: center;
  padding: 0;
}
.bar-btn svg { width: 46px; height: 46px; fill: none; stroke: var(--ink); stroke-width: 3.5; stroke-linejoin: round; stroke-linecap: round; }
.bar-btn .x { stroke: var(--neg); stroke-width: 4.5; }
.home-btn svg path { fill: var(--pron-bg); }
.bar-btn:active { transform: scale(0.95); }

.edit-btn { grid-auto-flow: row; gap: 2px; align-content: center; font-weight: 700; font-size: 15px; }
.edit-btn svg { width: 34px; height: 34px; }
.edit-btn[aria-pressed="true"] { border-color: var(--q); background: var(--q-bg); }
.lock-btn { width: 58px; border-color: transparent; background: transparent; }
.lock-btn svg { width: 40px; height: 40px; }
.lock-btn .ring-bg { stroke: var(--line); stroke-width: 3; }
.lock-btn .ring { stroke: var(--focus); stroke-width: 4; stroke-dasharray: 132; stroke-dashoffset: 132; transform: rotate(-90deg); transform-origin: center; }
.lock-btn .gear { stroke: var(--muted); stroke-width: 3; }
.lock-btn.holding .ring { transition: stroke-dashoffset var(--hold-ms, 1500ms) linear; stroke-dashoffset: 0; }

.strip {
  flex: 1;
  min-width: 0;
  border: 3px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  display: flex;
  align-items: center;
  padding: 6px 10px;
}
.strip::-webkit-scrollbar { display: none; }
.strip.speaking { border-color: var(--verb); box-shadow: 0 0 0 4px var(--verb-bg); }
.strip-words { display: flex; gap: 6px; height: 100%; }
.strip-word {
  height: 100%;
  aspect-ratio: 0.85;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  padding: 3px;
  background: var(--tile-bg, #fff);
  border: 2px solid var(--tile-line, transparent);
}
.strip-word img { width: 100%; flex: 1; min-height: 0; object-fit: contain; }
.strip-word span { font-size: 14px; font-weight: 650; line-height: 1.1; white-space: nowrap; }
.strip-word.on { outline: 4px solid var(--verb); }

/* ---------- Page heading ---------- */
.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 32px;
  flex: none;
}
.page-head h1 { margin: 0 4px; font-size: 22px; font-weight: 700; color: var(--muted); }
.edit-banner { display: flex; align-items: center; gap: 12px; background: var(--q-bg); border: 2px solid var(--q); padding: 4px 6px 4px 14px; border-radius: 999px; font-weight: 600; }
.edit-banner[hidden] { display: none; }

/* ---------- Grid ---------- */
.grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols, 6), minmax(0, 1fr));
  grid-auto-rows: var(--row-h, 120px);
  gap: var(--gap);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  align-content: start;
}
.grid::-webkit-scrollbar { display: none; }

.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 8px 6px 6px;
  border-radius: var(--radius);
  border: 4px solid var(--tile-line);
  background: var(--tile-bg);
  color: var(--ink);
  min-width: 0;
  overflow: hidden;
  transition: transform 80ms ease-out;
}
.tile img { flex: 1; min-height: 0; width: 100%; object-fit: contain; }
.tile .label {
  flex: none;
  font-size: clamp(15px, calc(var(--row-h, 120px) * 0.14), 26px);
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.no-labels .grid .tile:not(.folder) .label { display: none; }
.tile:active, .tile.pressed { transform: scale(0.94); filter: brightness(0.95); }

.t-pron   { --tile-bg: var(--pron-bg);   --tile-line: var(--pron); }
.t-verb   { --tile-bg: var(--verb-bg);   --tile-line: var(--verb); }
.t-desc   { --tile-bg: var(--desc-bg);   --tile-line: var(--desc); }
.t-noun   { --tile-bg: var(--noun-bg);   --tile-line: var(--noun); }
.t-social { --tile-bg: var(--social-bg); --tile-line: var(--social); }
.t-neg    { --tile-bg: var(--neg-bg);    --tile-line: var(--neg); }
.t-q      { --tile-bg: var(--q-bg);      --tile-line: var(--q); }
.t-folder { --tile-bg: var(--folder-bg); --tile-line: var(--folder); }

/* Folder tiles get a tab so they look different from word buttons */
.tile.folder { border-top-left-radius: 6px; }
.tile.folder::before {
  content: "";
  position: absolute;
  top: -4px; left: -4px;
  width: 42%; height: 16px;
  background: var(--folder);
  border-radius: 0 0 12px 0;
}
.tile.back { --tile-bg: var(--surface); --tile-line: var(--line); }
.tile.back svg { flex: 1; min-height: 0; width: 60%; fill: none; stroke: var(--ink); stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; }

.tile-gap { pointer-events: none; }
.tile img[hidden] + .label { font-size: clamp(18px, calc(var(--row-h, 120px) * 0.2), 34px); white-space: normal; }

/* Edit mode */
.editing .tile { animation: none; }
.editing .tile.hidden-item { opacity: 0.38; border-style: dashed; }
.editing .tile.hidden-item::after { content: "hidden"; position: absolute; top: 6px; right: 8px; font-size: 12px; font-weight: 700; background: #fff; border-radius: 6px; padding: 1px 5px; }
.tile.add-tile { --tile-bg: transparent; --tile-line: var(--q); border-style: dashed; color: var(--q); }
.tile.add-tile .plus { font-size: 54px; line-height: 1; font-weight: 300; }

/* ---------- Sheets (dialogs) ---------- */
dialog.sheet {
  border: none;
  border-radius: 24px;
  padding: 0;
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 24px 80px rgba(0,0,0,0.3);
  -webkit-user-select: text;
  user-select: text;
}
dialog.sheet.tall { width: min(900px, calc(100vw - 32px)); height: calc(100dvh - 48px); }
dialog::backdrop { background: rgba(20,20,24,0.45); }

.sheet-inner { display: flex; flex-direction: column; max-height: inherit; height: 100%; }
.sheet-head, .sheet-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); flex: none; }
.sheet-foot { border-bottom: none; border-top: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 22px; }
.sheet-inner > section, .editor-body { padding: 8px 20px 16px; }
.sheet-inner { overflow-y: auto; }
.sheet h3 { font-size: 15px; text-transform: uppercase; letter-spacing: 0.05em; color: #6b6b70; margin: 16px 0 8px; }
.hint { color: #6b6b70; font-size: 15px; margin: 4px 0 10px; }
.about p { font-size: 15px; line-height: 1.45; }
.about a { color: var(--focus); }

.row { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 52px; border-bottom: 1px solid #eeece7; font-size: 17px; }
.row input[type=checkbox] { width: 28px; height: 28px; accent-color: var(--verb); }
.row select, .field input { border: 2px solid var(--line); border-radius: 12px; padding: 8px 12px; background: #fff; min-height: 44px; max-width: 60%; }
.row input[type=range] { width: 50%; accent-color: var(--focus); }
.field { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; font-size: 15px; font-weight: 600; color: #48484c; }
.field input { max-width: none; font-size: 19px; font-weight: 500; color: var(--ink); }

.pill { border: 2px solid var(--line); background: #fff; border-radius: 999px; padding: 8px 18px; font-weight: 650; min-height: 44px; }
.wide-btn { display: inline-flex; align-items: center; justify-content: center; min-height: 52px; padding: 10px 18px; border-radius: 14px; border: 2px solid transparent; background: var(--focus); color: #fff; font-weight: 650; font-size: 17px; width: 100%; margin: 6px 0; text-align: center; }
.wide-btn.ghost { background: #fff; color: var(--ink); border-color: var(--line); }
.wide-btn.danger { background: #fff; color: var(--neg); border-color: var(--neg-bg); }
.wide-btn.primary { background: var(--verb); }
.file-btn { cursor: pointer; }
.btn-row { display: flex; gap: 10px; }
.btn-col { display: flex; flex-direction: column; flex: 1; }
.sheet-foot .wide-btn { width: auto; flex: 1; margin: 0; }

.swatches { display: flex; gap: 8px; }
.swatch { width: 40px; height: 40px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px var(--line); padding: 0; }
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--focus); }

.editor-preview { display: flex; gap: 16px; align-items: stretch; margin-top: 12px; }
.crop-body { padding: 16px 20px 8px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.crop-stage { align-self: center; width: min(360px, 70vw, 48dvh); aspect-ratio: 1; }
#cropCanvas { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; border-radius: 6px; box-shadow: 0 0 0 1px var(--line); }
#cropCanvas:active { cursor: grabbing; }
.crop-tip { text-align: center; margin: 0; }
.crop-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 15px; font-weight: 600; color: #48484c; }
.crop-row > span:first-child { min-width: 92px; }
.crop-row input[type=range] { flex: 1; accent-color: var(--focus); }
.crop-gap { flex: 1; }
.chip { border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 8px 14px; min-height: 44px; font-weight: 650; font-size: 15px; }
.chip[aria-pressed="true"] { border-color: var(--focus); background: #eaf1fd; }

.paste-box {
  margin-top: 10px; padding: 14px; min-height: 52px; border: 2px dashed var(--line); border-radius: 14px;
  text-align: center; font-size: 16px; color: #48484c; background: #fafaf8;
  -webkit-user-select: text; user-select: text; caret-color: transparent; outline: none;
}
.paste-box:focus { border-color: var(--focus); background: #f2f6fd; }
.photo-status:empty { display: none; }
.photo-status { color: var(--ink); font-weight: 600; margin: 8px 0 0; }
.preview-tile { width: 150px; height: 150px; flex: none; --row-h: 150px; }
.type-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.type-chip { border-radius: 12px; border: 3px solid var(--tile-line); background: var(--tile-bg); padding: 8px 12px; font-weight: 650; font-size: 15px; min-height: 44px; }
.type-chip[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--ink); }

.gp-status { font-size: 18px; line-height: 1.4; margin: 16px 0; }
a.wide-btn { text-decoration: none; }

#pickerSearch { flex: 1; border: 2px solid var(--line); border-radius: 12px; padding: 10px 14px; font-size: 18px; min-height: 48px; }
.picker-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; padding: 16px 20px; align-content: start; }
.pick { display: flex; flex-direction: column; align-items: center; gap: 4px; border: 2px solid var(--line); border-radius: 14px; background: #fff; padding: 8px; aspect-ratio: 1 / 1.1; min-width: 0; }
.pick img { flex: 1; min-height: 0; width: 100%; object-fit: contain; }
.pick span { font-size: 12px; color: #48484c; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-empty { grid-column: 1 / -1; color: #6b6b70; text-align: center; padding: 24px; margin: 0; }
.picker-section { display: contents; }
.picker-note { grid-column: 1 / -1; margin: -4px 0 0; font-size: 14px; color: #6b6b70; }
.picker-heading { grid-column: 1 / -1; margin: 8px 0 0; font-size: 14px; text-transform: uppercase; letter-spacing: 0.05em; color: #6b6b70; }

/* Phones and small windows: shrink the chrome */
@media (max-width: 700px) {
  :root { --bar-h: 76px; --gap: 6px; --radius: 14px; }
  .bar-btn { width: 60px; }
  .bar-btn svg { width: 34px; height: 34px; }
  .lock-btn { width: 44px; }
  .strip-word span { font-size: 11px; }
  .preview-tile { width: 110px; height: 110px; }
}
