:root {
  --bg: #f4f3ef;
  --ink: #151515;
  --mute: #6b6b6b;
  --line: #dcdad3;
  --card: #fff;
  --accent: #1a62d6;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-text-size-adjust: 100%; }
body { overscroll-behavior: none; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
#app { height: 100%; display: flex; flex-direction: column; }

/* ---------- Screens ---------- */
.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.top { display: flex; align-items: center; gap: 8px; padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: var(--bg); border-bottom: 1px solid var(--line); }
.top h1 { font-size: 17px; font-weight: 700; margin: 0; flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 14px calc(var(--safe-bot) + 24px); }
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 10px 14px; font-size: 15px; }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.small { padding: 7px 11px; font-size: 14px; }
.btn.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.link { border: 0; background: none; padding: 8px 4px; font-size: 15px; color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
h2 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--mute); margin: 22px 0 10px; }
.muted { color: var(--mute); font-size: 14px; }
.star { border: 0; background: none; font-size: 22px; line-height: 1; padding: 4px 6px; color: #c9c6bd; }
.star.on { color: #e8a400; }

/* Home */
.title-big { font-size: 30px; font-weight: 700; margin: 6px 0 4px; }
.game-row { display: flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 10px 12px 14px; margin-bottom: 8px; }
.game-row .meta { flex: 1; min-width: 0; }
.game-row .t { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { padding: 28px 8px; text-align: center; color: var(--mute); }

/* New game */
textarea.src { width: 100%; min-height: 46vh; border: 1px solid var(--line); border-radius: 12px; padding: 14px;
  font-size: 16px; line-height: 1.5; background: var(--card); resize: vertical; }
input.text { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; font-size: 16px; background: var(--card); }
label.lbl { display: block; font-size: 13px; font-weight: 700; color: var(--mute); margin: 14px 0 6px; }

/* Board */
.starts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.starts .btn { text-align: left; padding: 12px; }
.starts .btn b { display: block; font-size: 15px; }
.starts .btn span { display: block; font-size: 12.5px; color: var(--mute); margin-top: 3px; line-height: 1.3; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.tile .thumb { height: 230px; overflow: hidden; position: relative; border-bottom: 1px solid var(--line); pointer-events: none; }
.tile .info { padding: 8px 6px 8px 10px; display: flex; align-items: center; gap: 4px; }
.tile .who { flex: 1; min-width: 0; font-size: 14px; font-weight: 700; }
.tile .who small { display: block; font-weight: 400; color: var(--mute); font-size: 12px; }
.tile .poem { padding: 0 10px 10px; font-size: 13px; color: #333; line-height: 1.35; max-height: 54px; overflow: hidden; }

/* ---------- The page ---------- */
.stage { flex: 1; min-height: 0; position: relative; }
.pg-viewport { position: absolute; inset: 0; overflow: auto; -webkit-overflow-scrolling: touch; background: var(--bg); }
.pg-viewport.pg-static { position: static; overflow: hidden; background: #fff; }
.pg-sizer { position: relative; margin: 0 auto; }
.pg-page { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none; }
.pg-static .pg-page { touch-action: auto; }
.pg-text { position: relative; z-index: 1; }
.pg-text p { margin: 0; }
.pg-text p.gap { margin-top: 0.85em; }
.pg-under, .pg-over { position: absolute; left: 0; top: 0; pointer-events: none; overflow: visible; }
.pg-under { z-index: 0; }
.pg-over { z-index: 2; }
.w { position: relative; }
.w.masked { color: transparent; background: #dedcd6; border-radius: 3px; box-shadow: 0 0 0 0 transparent; }
.w.masked.letters::before { content: attr(data-f); position: absolute; left: 1px; color: #555; }
.w.gone { visibility: hidden; }

/* Play screen */
.toolbar { background: var(--bg); border-top: 1px solid var(--line); padding: 8px 8px calc(var(--safe-bot) + 8px); }
.tools { display: flex; justify-content: space-between; gap: 4px; }
.tool { flex: 1; border: 0; background: none; border-radius: 10px; padding: 6px 2px 4px; display: flex; flex-direction: column;
  align-items: center; gap: 2px; font-size: 10.5px; color: var(--mute); }
.tool svg { width: 26px; height: 26px; }
.tool.on { background: var(--card); color: var(--ink); box-shadow: 0 0 0 1px var(--line); }
.swatches { display: flex; gap: 10px; justify-content: center; padding: 8px 0 2px; }
.sw { width: 32px; height: 32px; border-radius: 50%; border: 3px solid transparent; color: #fff; font-weight: 700; font-size: 13px;
  display: flex; align-items: center; justify-content: center; padding: 0; }
.sw.on { border-color: var(--ink); }
.swatches.off { opacity: .3; pointer-events: none; }

.strip { background: var(--card); border-top: 1px solid var(--line); padding: 8px 14px; font-size: 14px; line-height: 1.45;
  max-height: 64px; overflow: hidden; position: relative; }
.strip.open { max-height: 42vh; overflow-y: auto; }
.strip .ln { margin: 2px 0; }
.num { display: inline-flex; width: 18px; height: 18px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700;
  align-items: center; justify-content: center; margin-right: 6px; vertical-align: 1px; }
.strip .hint { color: var(--mute); }

.minimap { position: absolute; right: 2px; top: 6px; bottom: 6px; width: 14px; z-index: 5; border-radius: 7px; background: rgba(0,0,0,.05); }
.minimap i { position: absolute; left: 3px; right: 3px; height: 3px; border-radius: 2px; background: var(--ink); }
.minimap b { position: absolute; left: 0; right: 0; border-radius: 7px; background: rgba(26,98,214,.18); }

.legend { position: absolute; left: 8px; top: 8px; z-index: 5; display: flex; gap: 4px; pointer-events: none; }
.legend span { width: 20px; height: 20px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center; }

/* Card view */
.readout { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; font-size: 16px; line-height: 1.5; }
.readout .ln { margin: 4px 0; }
.view-page { height: 62vh; position: relative; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; margin-bottom: 12px; }

/* Final piece */
.fp-stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff; }
.fp-chips { position: absolute; inset: 0; }
.fp-chip { position: absolute; padding: 4px 5px; line-height: 1.2; white-space: nowrap; user-select: none; -webkit-user-select: none; border-radius: 6px; }
.fp-chip:active { background: rgba(0,0,0,.05); }
.fp-viewport { background: var(--bg); }

.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 50; display: flex; align-items: flex-end; }
.sheet { background: var(--bg); width: 100%; border-radius: 16px 16px 0 0; padding: 18px 16px calc(var(--safe-bot) + 18px); max-height: 80vh; overflow-y: auto; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.sheet .btn { width: 100%; text-align: left; margin-bottom: 8px; }
.sheet .btn span { display: block; font-size: 13px; color: var(--mute); margin-top: 2px; }
.toast { position: fixed; left: 50%; bottom: calc(var(--safe-bot) + 120px); transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; }

.top h1.editable { cursor: text; text-decoration: underline dotted #b9b6ad; text-underline-offset: 4px; }
