:root {
  --bg: #0f1117;
  --panel: rgba(255,255,255,0.05);
  --border: rgba(255,255,255,0.09);
  --text: #e8eaf2;
  --muted: #8a90a6;
  --light: #e9dcc3;
  --dark: #9c7b5b;
  --good: #3ddc97;
  --bad: #ff5d73;
  --accent: #8b7bff;
  --panel-size: 300px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Outfit', system-ui, sans-serif;
  color: var(--text);
  background: radial-gradient(ellipse at 20% 0%, #1d1f3a 0%, var(--bg) 60%), var(--bg);
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

#app {
  height: 100dvh; width: 100vw;
  display: flex; align-items: center; justify-content: center;
  gap: 16px; padding: 12px;
}
/* ---------- Board sizing ---------- */
#board-wrap { position: relative; display: flex; flex-direction: column; align-items: center; }
#board {
  --size: min(calc(100dvh - 80px), calc(100vw - var(--panel-size) - 56px));
  width: var(--size); height: var(--size);
  display: grid; grid-template: repeat(8, 1fr) / repeat(8, 1fr);
  border-radius: 6px; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,.55), 0 0 0 1px var(--border);
  transition: box-shadow .25s, transform .25s;
}
#board.flash-good { box-shadow: 0 0 0 4px var(--good), 0 0 50px rgba(61,220,151,.5); }
#board.flash-bad  { box-shadow: 0 0 0 4px var(--bad), 0 0 50px rgba(255,93,115,.5); }
#board.enter { animation: enter .28s ease-out; }
@keyframes enter { from { opacity: .2; transform: scale(.985); } }
.sq { position: relative; }
.sq.l { background: var(--light); }
.sq.d { background: var(--dark); }
.sq img { width: 100%; height: 100%; display: block; pointer-events: none; }
.sq .coord { position: absolute; font-size: clamp(8px, 1.4vmin, 13px); font-weight: 600; opacity: .75; }
.sq .rank { top: 2px; left: 3px; }
.sq .file { bottom: 1px; right: 3px; }
.sq.l .coord { color: var(--dark); } .sq.d .coord { color: var(--light); }

.zone-hint { font-size: 12px; letter-spacing: .15em; text-transform: uppercase; color: var(--muted); height: 24px; line-height: 24px; opacity: .45; padding: 0 12px; border-radius: 12px; margin: 2px 0; transition: all .2s; }
#hint-top.turn { opacity: 1; background: #111; color: #fff; font-weight: 800; box-shadow: 0 0 0 2px #fff3, 0 0 18px rgba(0,0,0,.8); }
#hint-bottom.turn { opacity: 1; background: #f4f4f4; color: #111; font-weight: 800; box-shadow: 0 0 18px rgba(255,255,255,.45); }
#timer { position: absolute; left: 0; right: 0; bottom: -2px; height: 5px; border-radius: 3px; background: var(--border); overflow: hidden; }
#timer-fill { height: 100%; width: 100%; background: linear-gradient(90deg, var(--bad), #ffb547, var(--good)); transform-origin: left; }

/* ---------- Panel ---------- */
#panel {
  width: var(--panel-size); align-self: center;
  display: flex; flex-direction: column; gap: 14px;
}
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 12px 14px; backdrop-filter: blur(10px);
  display: flex; flex-direction: column;
}
.stat .label { font-size: 11px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); }
.stat .value { font-size: 28px; font-weight: 800; }
.stat .value.bump { animation: bump .35s; }
@keyframes bump { 50% { transform: scale(1.25); color: var(--good); } }

.feedback {
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 16px; text-align: center; transition: border-color .2s, background .2s;
}
.feedback.good { border-color: var(--good); background: rgba(61,220,151,.08); }
.feedback.bad  { border-color: var(--bad);  background: rgba(255,93,115,.08); }
#verdict { font-size: 26px; font-weight: 800; }
.good #verdict { color: var(--good); } .bad #verdict { color: var(--bad); }
#sub { font-size: 13px; color: var(--muted); margin-top: 4px; min-height: 18px; }
#evalbar {
  position: relative; margin-top: 12px; height: 22px; border-radius: 11px; overflow: hidden;
  background: #2a2c36; opacity: 0; transition: opacity .2s;
}
.good #evalbar, .bad #evalbar { opacity: 1; }
#evalfill { position: absolute; inset: 0 auto 0 0; width: 50%; background: #f4f4f4; transition: width .4s cubic-bezier(.2,.8,.2,1); }
#evaltext { position: relative; font-size: 12px; font-weight: 600; line-height: 22px; mix-blend-mode: difference; color: #fff; }

#tomove { font-size: 16px; font-weight: 800; text-align: center; padding: 8px; border-radius: 12px; }
#tomove.w { background: #f4f4f4; color: #111; }
#tomove.b { background: #111; color: #fff; border: 1px solid #fff3; }
#history { display: flex; flex-wrap: wrap; gap: 5px; justify-content: center; min-height: 12px; }
#history i { width: 10px; height: 10px; border-radius: 50%; display: block; }
#history i.g { background: var(--good); } #history i.b { background: var(--bad); } #history i.t { background: var(--muted); }

/* ---------- Portrait ---------- */
@media (orientation: portrait) {
  :root { --panel-size: 0px; }
  #app { flex-direction: column; gap: 10px; padding: 8px; justify-content: flex-start; }
  #board { --size: min(calc(100vw - 16px), calc(100dvh - 250px)); }
  #panel { width: min(100%, calc(100vw - 16px)); gap: 8px; }
  .stats { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .stat { padding: 8px; align-items: center; }
  .stat .value { font-size: 20px; }
  .feedback { padding: 10px; }
  #verdict { font-size: 20px; }
}

/* ---------- Start overlay ---------- */
#start {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: rgba(10,11,18,.75); backdrop-filter: blur(8px); z-index: 10; cursor: pointer;
}
#start.hidden { display: none; }
.start-card {
  text-align: center; padding: 32px 36px; border-radius: 22px; max-width: 360px; margin: 16px;
  background: linear-gradient(160deg, rgba(139,123,255,.18), rgba(255,255,255,.04));
  border: 1px solid var(--border); box-shadow: 0 30px 80px rgba(0,0,0,.5);
}
.start-card .logo { font-size: 56px; }
.start-card h2 { font-size: 32px; margin-bottom: 12px; }
.start-card p { margin: 10px 0; line-height: 1.5; }
.start-card .muted { color: var(--muted); font-size: 14px; }
kbd { background: #2a2c36; border: 1px solid var(--border); border-radius: 5px; padding: 1px 7px; font-family: inherit; }
.go { margin-top: 18px; font-weight: 600; color: var(--accent); animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .45; } }
