:root {
  --bg: #0b0d12;
  --panel: #141821;
  --panel2: #1b2030;
  --line: #262c3d;
  --text: #e8ecf4;
  --muted: #8d97ad;
  --accent: #7cc4ff;
  --ink: #06121f;
  --warn: #ffcc66;
  --err: #ff8b8b;
  --ok: #8ee0b0;
  --desk: #1a1e28;
  --paper: #fff;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #e7e1d6;
    --panel: #fff;
    --panel2: #f3efe7;
    --line: #ddd4c6;
    --text: #1c1915;
    --muted: #6d655b;
    --accent: #0a66c2;
    --ink: #fff;
    --warn: #8a5a00;
    --err: #b42318;
    --ok: #0e7a45;
    --desk: #d9d1c3;
    --shadow: 0 10px 28px rgba(60, 40, 10, .18);
    color-scheme: light;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
html { background: var(--panel); }
body {
  position: fixed;
  inset: 0;
  width: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
.sr { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; }
.primary, .ghost {
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
}
.primary { border: 0; background: var(--accent); color: var(--ink); font-weight: 700; }
.ghost { border: 1px solid var(--line); background: transparent; color: var(--muted); }
.ghost:hover, .primary:hover { filter: brightness(1.05); }
a { color: var(--accent); }

.gate {
  height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  display: grid;
  place-items: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(24px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(24px, env(safe-area-inset-left));
}
.gate-card { width: min(440px, 100%); }
.gate h1, .admin h1 { margin: 8px 0 8px; font-size: 2rem; letter-spacing: -.03em; }
.back, .back a { color: var(--muted); text-decoration: none; }
#gate-lead { margin: 0 0 12px; color: var(--muted); }
#gate-msg { min-height: 1.4em; }
.gate-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

.studio, .admin { height: 100%; min-height: 0; }
/* Paper gets everything the two bars do not use. No tool panel scrolls. */
.studio {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding:
    max(8px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    8px
    max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.studio > .top {
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  gap: 2px;
  padding-bottom: 2px;
}
.top-main, .top-sub {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.top-sub { min-height: 30px; }
.top-sub .ghost { flex: none; }
.slim { min-height: 30px; padding: 3px 10px; font-size: .8rem; }
.link-btn {
  flex: none;
  min-height: 30px;
  padding: 0 6px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: .85rem;
  font-weight: 650;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  text-decoration: none;
  color: var(--muted);
  font-size: 1.2rem;
}
.printer-pick { flex: 1; min-width: 0; }
.printer-pick select, .row-form input, .row-form select, .person-form input, .card .rename {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel2);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}
.status { margin: 0; flex: 1; min-width: 0; color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.bad { color: var(--err); }
.status.good { color: var(--ok); }
.status.warn { color: var(--warn); }
#print { flex: none; }

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  background: var(--desk);
  touch-action: none;
  padding-bottom: 12px;
}
.stage.dropping { outline: 3px dashed var(--accent); outline-offset: -6px; }
.paper-wrap {
  position: relative;
  margin: 12px auto 24px;
  background: var(--paper);
  box-shadow: var(--shadow);
}
.paper, .layer {
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.paper { background: #fff; }
.layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.text-layer { position: absolute; inset: 0; pointer-events: none; }
.text-hit {
  position: absolute;
  box-sizing: border-box;
  border: 1px dashed rgba(10, 102, 194, .7);
  border-radius: 2px;
}
.text-hit.selected {
  border: 2px solid #0a66c2;
  background: rgba(10, 102, 194, .05);
}
.text-handle {
  position: absolute;
  width: 26px;
  height: 26px;
  right: -15px;
  bottom: -15px;
  border-radius: 50%;
  background: #0a66c2;
  border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
}
.paper-wrap .typer {
  position: absolute;
  z-index: 2;
  display: block;
  margin: 0;
  border: 0;
  border-radius: 0;
  min-height: 0;
  outline: 2px solid #0a66c2;
  background: transparent;
  color: #000;
  caret-color: #0a66c2;
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  transform-origin: 0 0;
  touch-action: manipulation;
  -webkit-appearance: none;
  appearance: none;
  color-scheme: light;
}

.dock {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding:
    0
    max(6px, env(safe-area-inset-right))
    env(safe-area-inset-bottom)
    max(6px, env(safe-area-inset-left));
}
/* One short row of options for the current tool or the selected thing. */
.ctx {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding: 6px 0 0;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior: contain;
  touch-action: pan-x;
  scrollbar-width: none;
}
.ctx::-webkit-scrollbar { display: none; }
.ctx-hint { flex: 1; min-width: 0; color: var(--muted); font-size: .85rem; padding-left: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip, .dot-btn {
  flex: none;
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  font-size: .9rem;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--ink); border-color: var(--accent); font-weight: 700; }
.chip.danger { color: var(--err); }
.dot-btn { display: grid; place-items: center; padding: 0; width: 48px; }
.dot-btn[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 1px; }
.dot-btn i { display: block; background: var(--text); border-radius: 99px; }
.dot-btn:nth-child(1) i { width: 4px; height: 4px; }
.dot-btn:nth-child(2) i { width: 8px; height: 8px; }
.dot-btn:nth-child(3) i { width: 14px; height: 14px; }
.dot-btn:nth-child(4) i { width: 22px; height: 22px; }
.toolbar {
  display: flex;
  gap: 2px;
  padding: 4px 0;
}
.toolbar button {
  flex: 1 1 0;
  min-width: 0;
  min-height: 50px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: .68rem;
  padding: 4px 0;
}
.toolbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.toolbar svg circle { fill: currentColor; stroke: none; }
.toolbar button[aria-pressed="true"] { background: var(--accent); color: var(--ink); font-weight: 700; }
.toolbar button[aria-expanded="true"] { color: var(--text); background: var(--panel2); }
.toolbar button[data-armed="1"] { color: var(--err); }
.more {
  display: none;
  flex-direction: column;
  gap: 8px;
  position: absolute;
  right: max(6px, env(safe-area-inset-right));
  bottom: calc(100% + 6px);
  z-index: 5;
  width: min(320px, calc(100% - 12px));
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.more.open { display: flex; }
.stack { display: flex; flex-direction: column; gap: 4px; min-width: 0; font-size: .8rem; color: var(--muted); }
.stack output, .length output { color: var(--text); font-weight: 650; }
input[type="range"] { display: block; width: 100%; max-width: 100%; min-width: 0; margin: 0; accent-color: var(--accent); }
.check { display: flex; gap: 8px; align-items: center; min-height: 44px; }
.check input { width: 20px; height: 20px; }
.hint { margin: 0; min-width: 0; color: var(--muted); font-size: .82rem; overflow-wrap: anywhere; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
#printer-width { flex-wrap: wrap; }
#printer-width button { flex: 1 1 7.5rem; }
.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text);
  min-height: 44px;
  padding: 8px 4px;
}
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--ink); font-weight: 700; }
.admin h2 { margin: 4px 0; font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.empty { color: var(--muted); font-size: .85rem; margin: 0; }

.admin { overflow: auto; overscroll-behavior: contain; background: var(--bg); }
.admin .top { position: sticky; top: 0; z-index: 1; }
.admin .top h1 { flex: 1; font-size: 1.3rem; margin: 0; }
.admin-grid { display: grid; gap: 22px; padding: 16px; max-width: 980px; margin: 0 auto; }
.admin-grid .span { grid-column: 1 / -1; }
.card .rename { margin-bottom: 8px; }
.row-form, .person-form { display: grid; gap: 8px; margin-bottom: 10px; }
.cards { display: flex; flex-direction: column; gap: 8px; }
.card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: var(--panel);
}
.card h3 { margin: 0 0 4px; font-size: 1rem; }
.card .meta { color: var(--muted); font-size: .82rem; margin: 0 0 8px; }
.secret {
  width: 100%;
  font: 12px/1.4 ui-monospace, "DejaVu Sans Mono", Menlo, Consolas, monospace;
  border: 1px solid var(--line);
  background: var(--panel2);
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 8px;
}
.card .actions, .checks { display: flex; flex-wrap: wrap; gap: 6px; }
.checks label { display: flex; gap: 6px; align-items: center; min-height: 36px; }
#admin-msg { padding: 0 16px calc(24px + env(safe-area-inset-bottom)); }
#device-msg { white-space: normal; overflow: visible; text-overflow: unset; overflow-wrap: anywhere; }

/* Desktop and an unfolded phone: tools in a slim side column. */
@media (min-width: 720px) {
  .studio {
    grid-template-columns: minmax(0, 1fr) minmax(240px, 300px);
    grid-template-rows: auto minmax(0, 1fr);
  }
  .studio > .top { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 12px; padding-bottom: 8px; }
  .studio > .top .top-main { flex: 1 1 auto; }
  .studio > .top .top-sub { flex: 0 1 45%; }
  .stage {
    grid-column: 1;
    grid-row: 2;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .dock {
    grid-column: 2;
    grid-row: 2;
    border-top: 0;
    border-left: 1px solid var(--line);
    padding: 10px max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) 12px;
    gap: 10px;
    overflow: hidden;
  }
  .toolbar { order: -1; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 0; }
  .toolbar button { background: var(--panel2); font-size: .75rem; }
  #more-btn { display: none; }
  .ctx { flex-wrap: wrap; overflow: visible; padding: 0; }
  .more { display: flex; position: static; width: auto; padding: 10px 0 0; border: 0; border-top: 1px solid var(--line); border-radius: 0; box-shadow: none; background: transparent; }
  .admin-grid { grid-template-columns: 1fr 1fr; align-items: start; }
  .status { white-space: normal; }
}
