:root {
  --paper: #e9e5da;
  --paper-deep: #ddd7c8;
  --ink: #241f22;
  --ink-soft: #635b55;
  --line: #cec7b5;
  --line-soft: #e0d9c9;
  --accent: #3d3a52;
  --radius: 9px;
  --side: 300px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }

/* `hidden` loses to `display: flex`/`grid`, and both stage panes use them */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--paper-deep);
  overflow: hidden;
}

/* ---------------------------------------------------------------- shell -- */

.app {
  display: grid;
  grid-template-columns: var(--side) 1fr;
  height: 100vh;
}

/* ---------------------------------------------------------------- side --- */

.side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #fffdf7;
  border-right: 1px solid var(--line);
}

.brand {
  padding: 16px 16px 13px;
  border-bottom: 1px solid var(--line);
}
.brand h1 {
  font-family: var(--serif);
  font-size: 1.28rem;
  margin: 0 0 4px;
  letter-spacing: .01em;
}
.brand p {
  margin: 0;
  font-size: .76rem;
  line-height: 1.45;
  color: var(--ink-soft);
}

.side-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-bottom: 24px;
}
.side-scroll::-webkit-scrollbar { width: 10px; }
.side-scroll::-webkit-scrollbar-thumb {
  background: var(--line);
  border: 3px solid #fffdf7;
  border-radius: 6px;
}

.grp {
  padding: 13px 16px;
  border-bottom: 1px solid var(--line-soft);
  display: grid;
  gap: 10px;
}
details.grp > summary {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--ink-soft);
  cursor: pointer;
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
details.grp > summary::-webkit-details-marker { display: none; }
details.grp > summary::before {
  content: "▸";
  font-size: .8em;
  transition: transform .12s ease;
}
details.grp[open] > summary::before { transform: rotate(90deg); }

.fields { display: grid; gap: 9px; }

.field { display: grid; gap: 4px; min-width: 0; }
.field > span {
  font-size: .69rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--ink-soft);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.field > span b { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 600; }

.trait { display: grid; grid-template-columns: 1fr auto; gap: 5px; align-items: end; }
.trait .field { min-width: 0; }

input[type="text"], select, textarea {
  font: inherit;
  font-size: .84rem;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  width: 100%;
  min-width: 0;
}
input[type="text"]:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
select.is-pinned { border-color: var(--accent); background: #f4f2fb; }

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  height: 18px;
  margin: 0;
}

button {
  font: inherit;
  font-size: .8rem;
  padding: 7px 11px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: #faf8f2; border-color: #b9b1a0; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); border-color: var(--accent); color: #f6f4ec; }
button.primary:hover { filter: brightness(1.18); }
button.wide { width: 100%; }
button.icon { padding: 7px 9px; }
button.dice {
  padding: 5px 7px;
  font-size: .75rem;
  line-height: 1;
  height: 30px;
  color: var(--ink-soft);
}

.btn-row { display: flex; gap: 6px; }
.btn-row > .primary { flex: 1; }
.btn-col { display: grid; gap: 6px; }

.check {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: .8rem;
  color: var(--ink-soft);
  cursor: pointer;
}

.note {
  margin: 0;
  font-size: .74rem;
  line-height: 1.5;
  color: var(--ink-soft);
}
.note b { color: var(--ink); font-weight: 600; }
.note code, .note em { font-family: var(--mono); font-size: .92em; font-style: normal; }

.err {
  margin: 0;
  font-size: .74rem;
  color: #8c2f2f;
  background: rgba(160,60,60,.08);
  border: 1px solid rgba(160,60,60,.25);
  border-radius: 6px;
  padding: 6px 8px;
}

textarea.genome {
  font-family: var(--mono);
  font-size: .68rem;
  line-height: 1.5;
  height: 230px;
  resize: vertical;
  tab-size: 2;
}

/* mode-dependent controls */
body[data-mode="sheet"] .focus-only,
body[data-mode="compare"] .focus-only,
body[data-mode="focus"] .sheet-only,
body[data-mode="compare"] .sheet-only { display: none !important; }

/* --------------------------------------------------------------- stage --- */

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--paper-deep);
  background-image:
    radial-gradient(circle at 22% 8%, rgba(255,255,255,.5), transparent 55%),
    radial-gradient(circle at 82% 88%, rgba(120,105,80,.09), transparent 50%);
}

.stage-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,253,247,.72);
  backdrop-filter: blur(6px);
}

.tabs { display: flex; gap: 4px; background: rgba(0,0,0,.045); padding: 3px; border-radius: 9px; }
.tab {
  border: 0;
  background: none;
  padding: 6px 14px;
  border-radius: 7px;
  font-size: .82rem;
  color: var(--ink-soft);
}
.tab:hover { background: rgba(255,255,255,.6); }
.tab.is-on { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(60,50,30,.14); }

.stage-meta {
  font-family: var(--mono);
  font-size: .76rem;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------------------------------------------------------------- sheet -- */

.sheet {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--cell, 140px), 1fr));
  gap: 8px;
  justify-items: center;
  align-content: start;
}
.sheet.one-sheet {
  gap: 0;
  background-image:
    radial-gradient(circle at 30% 20%, rgba(255,255,255,.5), transparent 45%),
    radial-gradient(circle at 75% 70%, rgba(130,112,80,.09), transparent 45%);
}

.cell {
  padding: 0;
  border: 0;
  background: none;
  border-radius: 8px;
  cursor: zoom-in;
  line-height: 0;
  transition: transform .12s ease;
}
.cell:hover { transform: translateY(-2px) scale(1.03); z-index: 2; }
.cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sheet:not(.one-sheet) .cell canvas {
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(60,50,30,.18);
}
.cell.is-pending { background: rgba(0,0,0,.03); border-radius: 8px; }

/* ---------------------------------------------------------------- focus -- */

.focus {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 18px;
}

.portrait-wrap { display: grid; gap: 10px; justify-items: center; }

.portrait {
  line-height: 0;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 34px -18px rgba(40,32,20,.65), 0 1px 0 rgba(255,255,255,.6);
  cursor: ew-resize;
  touch-action: none;
  user-select: none;
  background: var(--paper);
}
.portrait canvas { display: block; }

.drag-hint {
  margin: 0;
  font-size: .74rem;
  color: var(--ink-soft);
  text-align: center;
}

.nav {
  flex: 0 0 auto;
  width: 38px;
  height: 62px;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--ink-soft);
  background: rgba(255,253,247,.7);
}
.nav:hover { background: #fff; }

/* ---------------------------------------------------------------- photo -- */

.note.keep {
  border-left: 2px solid var(--accent);
  padding-left: 8px;
}

.drop-zone {
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  transition: border-color .12s, background .12s;
}
.drop-zone.is-over {
  border-color: var(--accent);
  background: rgba(0, 0, 0, .03);
}
.drop-zone .hint {
  margin: 6px 0 0;
  font-size: .68rem;
  color: var(--ink-soft);
}

/* The same line carries errors and progress; a note is not a failure. */
.err.is-note { color: var(--ink-soft); }

.photo-preview {
  margin: 8px 0;
  line-height: 0;
  text-align: center;
}
.photo-preview canvas {
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
}

/* -------------------------------------------------------------- compare -- */

.compare {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 18px;
}
.slot { display: grid; gap: 8px; justify-items: center; min-width: 0; }
.slot-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: .72rem;
  color: var(--ink-soft);
  max-width: 100%;
}
.slot-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
button.mini { padding: 2px 8px; font-size: .9rem; line-height: 1.2; }
.slot-mid { display: grid; gap: 6px; align-content: center; }
.compare .portrait { cursor: default; }

/* ----------------------------------------------------------- favourites -- */

.fav-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  border-top: 1px solid var(--line);
  background: rgba(255,253,247,.8);
}
.fav-label {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--ink-soft);
  flex: 0 0 auto;
}
.fav-list {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.fav {
  position: relative;
  flex: 0 0 auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  line-height: 0;
  background: var(--paper);
}
.fav:hover { border-color: var(--accent); }
.fav .drop {
  position: absolute;
  top: 1px;
  right: 1px;
  width: 16px;
  height: 16px;
  padding: 0;
  border: 0;
  border-radius: 0 6px 0 6px;
  background: rgba(255,253,247,.85);
  font-size: .62rem;
  line-height: 16px;
  color: var(--ink-soft);
}
.fav .drop:hover { background: #fff; color: #8c2f2f; }

button.star { font-size: 1rem; padding: 4px 10px; line-height: 1.2; }
button.star.is-on { background: var(--accent); border-color: var(--accent); color: #f6f4ec; }

@media (max-width: 860px) {
  /* On a phone the controls came first and pushed every face below the fold.
     Put the stage on top, let the page scroll, and stack the panel underneath. */
  body { overflow: auto; }
  .app { grid-template-columns: 1fr; height: auto; }

  .stage { order: -1; min-height: 78vh; }
  .sheet { overflow: visible; }
  .focus, .compare { padding: 12px; }
  .compare { flex-wrap: wrap; gap: 10px; }
  .slot-mid { grid-auto-flow: column; gap: 6px; }

  .side {
    order: 1;
    border-right: 0;
    border-top: 1px solid var(--line);
  }
  .side-scroll { max-height: none; overflow: visible; }
  .brand { padding: 12px 16px 10px; }
  .brand p { display: none; }
  .nav { width: 30px; height: 52px; }
}
