:root {
  --bg: #fff7e8;
  --panel: #ffffff;
  --text: #2b2b2b;
  --muted: #7a7266;
  --accent: #ff8a3d;
  --accent-soft: #ffe2cc;
  --danger: #e53935;
  --radius: 18px;
  --shadow: 0 4px 16px rgb(0 0 0 / 0.08);
  font-family: ui-rounded, 'SF Pro Rounded', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--text);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  background: var(--bg);
  height: 100%;
  overscroll-behavior: none;
}

html {
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
}

body {
  background: var(--bg);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
  cursor: pointer;
}

button:disabled {
  opacity: 0.35;
  cursor: default;
}

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

/* ---- Layout ----
   The paper gets all the space the controls don't need. Controls sit at the edges,
   with touch targets of 44pt or more:
   - Landscape: side rails. Tools and colors are on the non-drawing hand's side (left for
     right-handers, like Procreate's sidebar); undo and actions are on the other side.
     Height is the scarce dimension on an iPad mini in landscape, so nothing goes on top.
   - Portrait: a slim action bar on top, tools and colors along the bottom. */

.app {
  height: 100%;
  display: grid;
  gap: 8px;
  padding: 8px;
  grid-template-columns: auto auto 1fr;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    'history actions  actions'
    'stage   stage    stage'
    'tools   sizes    palettes'
    'colors  colors   colors';
}

/* In portrait the rails dissolve and their groups sit directly in the grid. */
.rail {
  display: contents;
}

.history { grid-area: history; }
.actions { grid-area: actions; }
.stage { grid-area: stage; }
.tools { grid-area: tools; }
.sizes { grid-area: sizes; }
.palettes { grid-area: palettes; }
.colors { grid-area: colors; }

.group {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  min-width: 0;
  min-height: 0;
}

.actions {
  justify-self: end;
}

.sizes {
  justify-self: start;
}

.colors {
  justify-self: center;
  flex-wrap: wrap;
  justify-content: center;
}

.spacer {
  width: 12px;
  height: 12px;
}

@media (orientation: landscape) and (min-height: 460px) {
  .app {
    grid-template-columns: auto auto minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: 'start colors stage end';
  }

  body[data-hand='left'] .app {
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-areas: 'end stage colors start';
  }

  .rail {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-height: 0;
  }

  .rail-start {
    grid-area: start;
    overflow-y: auto; /* only if a short window can't fit tools, sizes and palettes */
  }

  .rail-end {
    grid-area: end;
  }

  .rail .group {
    grid-area: auto;
    flex: none;
  }

  .rail .actions {
    flex: 0 1 auto;
  }

  .group {
    flex-direction: column;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .colors {
    align-self: start;
    flex-wrap: nowrap;
  }

  .spacer {
    height: 6px;
  }
}

/* ---- Buttons ---- */

.action {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 58px;
  height: 56px;
  padding: 2px;
  border: none;
  border-radius: 12px;
  background: none;
  line-height: 1.1;
}

.action .icon {
  font-size: 1.45rem;
}

.action .caption,
.tool .caption {
  font-size: 0.68rem;
  color: var(--muted);
}

/* Picture-only buttons for kids who don't read yet. The caption stays in the page
   for screen readers; the icon grows to fill the space. */
body[data-labels='off'] .caption {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

body[data-labels='off'] .action,
body[data-labels='off'] .tool {
  width: 56px;
}

body[data-labels='off'] .action .icon {
  font-size: 1.9rem;
}

body[data-labels='off'] .tool .icon {
  font-size: 2.1rem;
}

.action.magic {
  background: #fff1b8;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 60px;
  height: 56px;
  padding: 2px;
  border: 3px solid transparent;
  border-radius: 14px;
  background: none;
  font-size: 1.6rem;
  line-height: 1.1;
}

.tool[aria-pressed='true'] {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.size {
  display: grid;
  place-items: center;
  flex: none;
  width: 48px;
  height: 48px;
  border: 3px solid transparent;
  border-radius: 50%;
  background: #f4f1ec;
}

.size .dot {
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.15);
}

.size[aria-pressed='true'] {
  border-color: var(--accent);
}

.swatch {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid rgb(0 0 0 / 0.1);
  background: var(--swatch);
  transition: transform 0.12s ease;
}

.swatch[aria-pressed='true'] {
  transform: scale(1.1);
  border-color: var(--panel);
  box-shadow: 0 0 0 3px var(--accent);
}

/* ---- Paper ---- */

.stage {
  min-height: 0;
  min-width: 0;
  display: grid;
  place-items: center;
  container-type: size;
}

.paper {
  --page-w: 4;
  --page-h: 3;
  position: relative;
  width: min(100cqw, calc(100cqh * var(--page-w) / var(--page-h)));
  aspect-ratio: var(--page-w) / var(--page-h);
  max-width: 100%;
  border-radius: 14px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 4px 18px rgb(0 0 0 / 0.12);
  touch-action: none;
  cursor: crosshair;
}

.paper canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* ---- Dialogs ---- */

dialog {
  border: none;
  border-radius: 24px;
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
  padding: 0;
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.35);
}

.sheet {
  width: min(640px, 94vw);
  max-height: 88dvh;
}

.sheet form {
  padding: 0 24px 20px;
}

.sheet-header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 0 8px;
  background: var(--panel);
  z-index: 1;
}

.sheet h2 {
  margin: 0;
}

.sheet h3 {
  margin: 18px 0 8px;
  font-size: 1rem;
  color: var(--muted);
}

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

.choice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #f7f4ef;
  cursor: pointer;
}

.choice input {
  flex: none;
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
  margin: 0;
}

.choice-body {
  display: flex;
  align-items: center;
  gap: 8px;
}

.choice .icon {
  font-size: 1.4rem;
}

.swatch-preview {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid rgb(0 0 0 / 0.2);
}

.hint {
  margin: 8px 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.sheet-footer {
  margin-top: 22px;
}

.primary,
.danger,
.confirm-actions button {
  padding: 10px 18px;
  border: none;
  border-radius: 12px;
  font-weight: 600;
}

.primary {
  background: var(--accent);
  color: #fff;
}

.danger {
  background: #fdecea;
  color: var(--danger);
}

.confirm form {
  padding: 24px;
  text-align: center;
}

.confirm-title {
  margin: 0;
  font-size: 1.4rem;
  font-weight: 700;
}

.confirm-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 18px;
}

.confirm-actions button:not(.danger, .primary) {
  background: #f2efe9;
}

/* ---- Toast ---- */

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top, 0px) + 76px);
  transform: translateX(-50%);
  max-width: min(90vw, 560px);
  padding: 12px 20px;
  border-radius: 999px;
  background: rgb(30 30 30 / 0.88);
  color: #fff;
  font-size: 1.05rem;
  text-align: center;
  pointer-events: none;
}

/* ---- Save preview ---- */

.save-preview {
  width: min(720px, 94vw);
}

.save-preview form {
  display: grid;
  gap: 12px;
  padding: 20px;
  text-align: center;
}

.save-preview img {
  width: 100%;
  border-radius: 12px;
  box-shadow: var(--shadow);
  -webkit-touch-callout: default;
  user-select: auto;
  -webkit-user-select: auto;
}

.save-preview .confirm-actions {
  flex-direction: row;
  justify-content: center;
  margin-top: 0;
}

/* ---- Coloring page generator ---- */

.provider-grid {
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  margin-top: 8px;
}

.field-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px 12px;
  margin-top: 10px;
}

.field-grid label {
  font-size: 0.9rem;
  color: var(--muted);
}

.field-grid input,
.field-grid select,
.create input[type='text'] {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid #e8e2d8;
  border-radius: 12px;
  font: inherit;
  user-select: text;
  -webkit-user-select: text;
}

.field-grid input:focus,
.field-grid select:focus,
.create input[type='text']:focus {
  outline: none;
  border-color: var(--accent);
}

.create {
  width: min(560px, 94vw);
}

.idea-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.create input[type='text'] {
  flex: 1;
  min-width: 0;
  font-size: 1.15rem;
}

.voice-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 52px;
  border: 2px solid #e8e2d8;
  border-radius: 12px;
  background: #fff;
  padding: 0;
}

.voice-btn[hidden] {
  display: none;
}

.voice-btn .icon .glyph {
  width: 34px;
  height: 34px;
}

.voice-btn:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.voice-btn.listening {
  border-color: #ee5a45;
  background: #fdecea;
  animation: listening 1.2s ease-in-out infinite;
}

@keyframes listening {
  50% {
    box-shadow: 0 0 0 6px rgb(238 90 69 / 0.25);
  }
}

@media (prefers-reduced-motion: reduce) {
  .voice-btn.listening {
    animation: none;
  }
}

.idea-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-top: 10px;
}

.idea-chips button {
  padding: 6px 12px;
  border: none;
  border-radius: 999px;
  background: #f2efe9;
  font-size: 0.9rem;
}

#create-status:empty {
  display: none;
}

.create[aria-busy='true'] #create-go {
  opacity: 0.6;
}

.model-row {
  display: flex;
  gap: 8px;
  min-width: 0;
}

.model-row select {
  flex: 1;
  min-width: 0;
  background: #fff;
}

.secondary {
  flex: none;
  padding: 8px 14px;
  border: none;
  border-radius: 12px;
  background: #f2efe9;
  font-weight: 600;
}

#settings-model-status:empty,
#gate-hint:empty {
  display: none;
}

/* ---- Parent gate ---- */

.gate {
  width: min(380px, 92vw);
}

.gate-question {
  margin: 16px 0 8px;
  font-size: 2.2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.gate-choices {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 8px;
}

.gate-choices button {
  padding: 14px 0;
  border: 2px solid #e8e2d8;
  border-radius: 14px;
  background: #fff;
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.gate-choices button:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.gate.shake {
  animation: shake 0.35s ease;
}

@keyframes shake {
  25% {
    translate: -8px 0;
  }
  75% {
    translate: 8px 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .gate.shake {
    animation: none;
  }
}

/* ---- Settings managed by config.local.json ---- */

.managed-note {
  padding: 10px 12px;
  border-radius: 12px;
  background: #f2efe9;
}

.choice.managed {
  opacity: 0.6;
}

.choice.managed .choice-body::after {
  content: '🔒';
  margin-left: 4px;
  font-size: 0.8em;
}

/* ---- Glyphs (js/icons.js) ----
   Sized from the font size each button already sets, so every button size and the
   picture-only mode keep working. Parts with class "ink" show the chosen colour. */

.icon {
  display: inline-flex;
  line-height: 1;
}

.icon .glyph {
  width: 1.45em;
  height: 1.45em;
  display: block;
  overflow: visible;
}

.glyph .ink {
  fill: var(--ink, #ee5a45);
}

/* ---- Palette picker ---- */

.palettes {
  justify-self: start;
}

.palette {
  display: grid;
  grid-template-columns: repeat(2, 12px);
  gap: 3px;
  place-content: center;
  flex: none;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 3px solid transparent;
  border-radius: 12px;
  background: #f4f1ec;
}

.palette[aria-pressed='true'] {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.palette-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1.5px rgb(46 42 38 / 0.35);
}

.voice-hint {
  max-width: 44ch;
  margin-inline: auto;
  text-align: left;
}

/* ---- Log panel ---- */

.log-open {
  margin-top: 12px;
}

.log-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  margin-top: 8px;
}

.log-tile {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  border-radius: 14px;
  background: #f7f4ef;
}

.log-tile p {
  margin: 0;
}

.log-tile-label {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.log-tile-value {
  font-size: 1.9rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.log-tile-value-small {
  font-size: 1.2rem;
}

.log-tile .secondary {
  align-self: flex-start;
  margin-top: 4px;
}

.log-entries {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.log-entry,
.log-empty {
  padding: 10px 12px;
  border-radius: 12px;
  background: #f7f4ef;
}

.log-entry p {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}

.log-entry-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}

.log-entry-cost {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text, #2b2b2b);
}

.log-entry.failed .log-entry-cost,
.log-entry-error {
  color: var(--danger);
}

.log-entry-text {
  font-size: 1.05rem;
}

.log-entry-meta,
.log-entry-error {
  font-size: 0.8rem;
  color: var(--muted);
}

.log-empty {
  color: var(--muted);
}
