:root {
  --bg: #FFF8EC; --panel: #FFFFFF; --ink: #1E2433; --muted: #6B7280; --line: #E8E0CF;
  --accent: #FF5A36; --accent-ink: #1E2433; --beam: #FF5A36; --beam-glow: rgba(255, 90, 54, .35);
  --gem: #1BC8C0; --gem-lit: #FFD23F; --gem-lit-ring: #F59E0B; --mirror: #4F46E5; --mirror-lock: #0EA5E9;
  --wall: #3D4A5C; --socket: #22C55E; --cell: #FFFDF7; --cell-hover: #FFF1C8; --cursor: #4F46E5;
  --good: #15803D; --bad: #DC2626; --shadow: 0 10px 30px rgba(30, 36, 51, .08);
  --beam2: var(--mirror); --beam2-glow: rgba(79, 70, 229, .25);
  --radius: 14px; --tap: 44px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0F172A; --panel: #16213A; --ink: #F1F5F9; --muted: #94A3B8; --line: #26324B;
    --beam: #FF7A5C; --beam-glow: rgba(255, 122, 92, .35); --gem: #22D3EE; --gem-lit: #FDE047; --gem-lit-ring: #FACC15;
    --mirror: #A5B4FC; --mirror-lock: #7DD3FC; --wall: #475569; --socket: #4ADE80; --cell: #1E293B; --cell-hover: #2A3A57;
    --cursor: #A5B4FC; --good: #16A34A; --shadow: 0 10px 30px rgba(0, 0, 0, .35); color-scheme: dark;
    --beam2: #E879F9; --beam2-glow: rgba(232, 121, 249, .3);
  }
}
:root[data-theme="dark"] {
  --bg: #0F172A; --panel: #16213A; --ink: #F1F5F9; --muted: #94A3B8; --line: #26324B;
  --beam: #FF7A5C; --beam-glow: rgba(255, 122, 92, .35); --gem: #22D3EE; --gem-lit: #FDE047; --gem-lit-ring: #FACC15;
  --mirror: #A5B4FC; --mirror-lock: #7DD3FC; --wall: #475569; --socket: #4ADE80; --cell: #1E293B; --cell-hover: #2A3A57;
  --cursor: #A5B4FC; --good: #16A34A; --shadow: 0 10px 30px rgba(0, 0, 0, .35); color-scheme: dark;
  --beam2: #E879F9; --beam2-glow: rgba(232, 121, 249, .3);
}
/* Okabe-Ito colorblind-safe palette */
:root[data-cb="1"] {
  --beam: #D55E00; --beam-glow: rgba(213, 94, 0, .35); --gem: #0072B2; --gem-lit: #F0E442; --gem-lit-ring: #000000;
  --mirror: #000000; --mirror-lock: #CC79A7; --socket: #009E73; --accent: #D55E00; --accent-ink: #000000;
}
:root[data-cb="1"][data-theme="dark"] { --mirror: #FFFFFF; --beam2: #56B4E9; --beam2-glow: rgba(86, 180, 233, .3); }
@media (prefers-color-scheme: dark) { :root[data-cb="1"]:not([data-theme="light"]) { --mirror: #FFFFFF; --beam2: #56B4E9; --beam2-glow: rgba(86, 180, 233, .3); } }
:root[data-cb="1"][data-theme="light"] { --mirror: #000000; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100dvh; overflow-x: hidden; -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.skip { position: absolute; left: -999px; top: 0; background: var(--panel); padding: 8px; }
.skip:focus { left: 8px; z-index: 50; }
[hidden] { display: none !important; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px; max-width: 560px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 800; font-size: 20px; letter-spacing: -.3px; }
.brand svg { width: 28px; height: 28px; }
.nav { display: flex; gap: 2px; }
.icon-btn {
  width: var(--tap); height: var(--tap); border: 0; border-radius: 12px; background: transparent; color: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; position: relative;
}
.icon-btn svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:hover, .icon-btn:focus-visible { background: var(--cell-hover); outline: none; }
.icon-btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

main { max-width: 560px; margin: 0 auto; padding: 0 16px 32px; }
.view { animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.meta { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin: 4px 0 10px; }
.meta h1 { font-size: 20px; margin: 0; }
.meta .date { color: var(--muted); }
.pill { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--cell-hover); color: var(--ink); }
.pill:empty { display: none; }
.pill.d1 { background: #DCFCE7; color: #166534; } .pill.d2 { background: #E0F2FE; color: #075985; }
.pill.d3 { background: #FEF3C7; color: #92400E; } .pill.d4 { background: #FFE4E6; color: #9F1239; } .pill.d5 { background: #EDE9FE; color: #5B21B6; }
.timer { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; }

.board-wrap { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; }
.board { display: block; width: 100%; height: auto; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.board .cell { fill: var(--cell); stroke: var(--line); stroke-width: .3; }
.board .cell.empty { cursor: pointer; }
.board .cell.empty:hover { fill: var(--cell-hover); }
.board .cell.path { fill: var(--cell-hover); }
.board .wall { fill: var(--wall); }
.board .wall-line { stroke: rgba(255, 255, 255, .25); stroke-width: .5; }
.board .gem { fill: var(--gem); stroke: rgba(0, 0, 0, .15); stroke-width: .4; transition: fill .2s; }
.board .gem.lit { fill: var(--gem-lit); stroke: var(--gem-lit-ring); stroke-width: .8; }
.board .gem-ring { fill: none; stroke: var(--gem-lit-ring); stroke-width: .6; opacity: 0; }
.board .gem.lit + .gem-ring { opacity: 1; }
.board .mirror { stroke: var(--mirror); stroke-width: 1.6; stroke-linecap: round; pointer-events: none; }
.board .mirror.locked { stroke: var(--mirror-lock); stroke-dasharray: 1.2 .9; }
.board .mirror-hi { stroke: rgba(255, 255, 255, .7); stroke-width: .45; stroke-linecap: round; pointer-events: none; }
.board .beam-glow { fill: none; stroke: var(--beam-glow); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.board .beam { fill: none; stroke: var(--beam); stroke-width: 1.1; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.board .beam.lost { stroke-dasharray: 1.5 1; opacity: .6; }
.board .beam.second { stroke: var(--beam2); }
.board .beam-glow.second { stroke: var(--beam2-glow); }
.board .beam.draw { animation: draw .35s ease-out; }
@keyframes draw { from { stroke-dashoffset: var(--len); } to { stroke-dashoffset: 0; } }
.board .laser { fill: var(--beam); }
.board .laser-base { fill: var(--wall); }
.board .socket { fill: none; stroke: var(--socket); stroke-width: 1.3; stroke-linecap: round; stroke-linejoin: round; }
.board .socket.on { fill: var(--socket); fill-opacity: .25; }
.board .socket-dot { fill: var(--socket); }
.board .cursor { fill: none; stroke: var(--cursor); stroke-width: .7; stroke-dasharray: 1.2 .8; pointer-events: none; display: none; }
.board.kb .cursor { display: block; }
.board .pulse { fill: none; stroke: var(--accent); stroke-width: .8; animation: pulse 1s ease-in-out infinite; pointer-events: none; }
@keyframes pulse { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }

.tray { display: flex; align-items: center; justify-content: space-between; margin: 12px 0 8px; gap: 12px; }
.tray-label { color: var(--muted); font-size: 14px; }
.tray-mirrors { display: flex; gap: 6px; flex-wrap: wrap; }
.tray-mirrors span { width: 26px; height: 26px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); display: inline-grid; place-items: center; }
.tray-mirrors span::before { content: ""; width: 3px; height: 16px; border-radius: 2px; background: var(--mirror); transform: rotate(45deg); }
.tray-mirrors span.used { opacity: .25; }
.controls { display: flex; gap: 8px; }
.btn {
  min-height: var(--tap); padding: 8px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  font-weight: 700; display: inline-flex; align-items: center; justify-content: center; gap: 6px; flex: 1;
}
.btn:hover, .btn:focus-visible { background: var(--cell-hover); outline: none; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.05); background: var(--accent); }
.btn.small { flex: 0; min-height: 36px; padding: 4px 12px; font-size: 14px; white-space: nowrap; }
.status { min-height: 1.4em; margin: 10px 0 0; color: var(--muted); font-size: 14px; text-align: center; }
.status.good { color: var(--good); font-weight: 700; }

.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; }
.card h2 { margin: 0 0 8px; font-size: 18px; }
.card p { margin: 6px 0; }
.muted { color: var(--muted); }
.stack { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.day-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.day { text-align: left; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); min-height: var(--tap); }
.day b { display: block; }
.day small { color: var(--muted); }
.day.won { border-color: var(--good); }
.day.won small { color: var(--good); }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--panel); border-radius: 12px; padding: 10px; text-align: center; border: 1px solid var(--line); }
.stat b { display: block; font-size: 24px; }
.stat small { color: var(--muted); }

.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); min-height: var(--tap); }
.field:last-child { border-bottom: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 8px 12px; min-height: 40px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.switch { width: 48px; height: 28px; border-radius: 999px; border: 0; background: var(--line); position: relative; flex-shrink: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0, 0, 0, .3); }
.switch[aria-checked="true"] { background: var(--good); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }

.overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, .55); display: flex; align-items: flex-end; justify-content: center; z-index: 40; padding: 0; }
@media (min-width: 560px) { .overlay { align-items: center; padding: 16px; } }
.sheet {
  background: var(--bg); color: var(--ink); width: 100%; max-width: 480px; max-height: 92dvh; overflow: auto;
  border-radius: 20px 20px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); box-shadow: var(--shadow);
  animation: up .25s ease;
}
@media (min-width: 560px) { .sheet { border-radius: 20px; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet h2 { margin: 0 0 6px; font-size: 22px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.share-card {
  font: 15px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 10px 0;
}
.share-row { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; margin: 0 0 4px; }
.share-btn {
  min-height: 56px; min-width: 0; padding: 6px 2px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 12px; font-weight: 600; line-height: 1.15; text-decoration: none;
}
.share-btn svg { width: 20px; height: 20px; flex-shrink: 0; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.share-btn span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share-btn:hover, .share-btn:focus-visible { background: var(--cell-hover); }
.share-btn:focus-visible { outline: 2px solid var(--cursor); outline-offset: 1px; }
.share-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.share-btn.primary:hover { filter: brightness(1.05); background: var(--accent); }
/* Instagram and Discord have no web share link; only a phone's system share sheet reaches them, so the hint is touch-only */
.share-help { display: none; margin: 4px 0 8px; font-size: 13px; color: var(--muted); text-align: center; }
@media (hover: none) and (pointer: coarse) { .share-help { display: block; } }
.countdown { font-variant-numeric: tabular-nums; }

.ad-slot { min-height: 0; margin: 12px 0; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 12px; }
.ad-slot.active { min-height: 100px; background: var(--panel); border-radius: 12px; }
:root[data-mode="licensed"] .ad-slot, :root[data-mode="licensed"] .share-only { display: none !important; }
/* the footer pages link back to "/" without ?mode=licensed, which would bring share and ads back inside an embed */
:root[data-mode="licensed"] footer { display: none; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-weight: 600; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 60;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.tut-board { max-width: 240px; margin: 8px auto; }
.tut-step { min-height: 2.8em; text-align: center; font-weight: 600; }
.rules { padding-left: 18px; margin: 8px 0; }
.rules li { margin: 4px 0; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; font-size: 14px; margin: 8px 0; }
.legend span { display: flex; align-items: center; gap: 8px; }
.legend i { width: 18px; height: 18px; border-radius: 4px; display: inline-block; flex-shrink: 0; }
footer { text-align: center; color: var(--muted); font-size: 13px; padding: 16px; }
footer a { color: var(--muted); }

:root[data-motion="reduced"] *, :root[data-motion="reduced"] *::before, :root[data-motion="reduced"] *::after { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) {
  :root:not([data-motion="full"]) *, :root:not([data-motion="full"]) *::before, :root:not([data-motion="full"]) *::after { animation: none !important; transition: none !important; }
}
@media (max-width: 480px) { .brand span { position: absolute; left: -9999px; } .top { padding: 8px 12px; } }
@media (max-width: 359px) {
  .icon-btn { width: 40px; } .nav { gap: 0; } .controls { flex-wrap: wrap; } .btn { padding: 8px 10px; } main { padding: 0 12px 24px; }
  /* icon-only share row; every button keeps its aria-label */
  .share-btn { min-height: var(--tap); } .share-btn span { display: none; }
}
