/* Layout: an analysis desk. Board column on the left, engine / coach / score column on the right; stacks on phones. */
:root {
  --bg: #ECEFF1;
  --panel: #FFFFFF;
  --panel-2: #F4F6F7;
  --ink: #152029;
  --muted: #5C6873;
  --line: #D4DBE0;
  --accent: #C47A0F;
  --accent-ink: #8C5606;
  --accent-soft: rgba(196, 122, 15, .13);
  --sq-light: #DCE3E8;
  --sq-dark: #86A0B1;
  --co-light: #6F8898;
  --co-dark: #EEF3F6;
  --hl-last: rgba(222, 170, 46, .46);
  --hl-sel: rgba(44, 120, 196, .40);
  --dot: rgba(21, 32, 41, .26);
  --piece-white: #FCFCFB;
  --piece-edge: #161B20;
  --piece-black: #1B2026;
  --bar-white: #F6F7F8;
  --bar-black: #262E35;
  --draw: #9AA6B0;
  --good: #1D8A59;
  --inacc: #B07C06;
  --mistake: #CC5F1C;
  --blunder: #C42F2F;
  --cloud: #2F6FA8;
  --brilliant: #13968F;
  --great: #3473C9;
  --threat: #D1332B;
  --shadow: 0 1px 2px rgba(21, 32, 41, .05), 0 8px 24px rgba(21, 32, 41, .06);
  --f-display: "Hahmlet", "Noto Serif KR", Georgia, serif;
  --f-body: "IBM Plex Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  --f-piece: "Noto Sans Symbols 2", "Segoe UI Symbol", "DejaVu Sans", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E1318; --panel: #161D23; --panel-2: #1C242B; --ink: #E4E9ED; --muted: #8E9AA5; --line: #28323B;
    --accent: #E2A23E; --accent-ink: #F0BC62; --accent-soft: rgba(226, 162, 62, .16);
    --sq-light: #B9C6CF; --sq-dark: #6E8798; --co-light: #5E7787; --co-dark: #E3EAEF;
    --hl-last: rgba(226, 170, 50, .48); --hl-sel: rgba(70, 140, 210, .48); --dot: rgba(10, 16, 22, .34);
    --piece-white: #F7F7F5; --piece-edge: #12171C; --piece-black: #161B20;
    --bar-white: #E9EDF0; --bar-black: #070A0D; --draw: #5D6974;
    --good: #3DB27B; --inacc: #D9A42A; --mistake: #E8834A; --blunder: #EE5A5A; --cloud: #6FA8DC;
    --brilliant: #2CC5BC; --great: #6AA3F0; --threat: #F0534A;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--f-body); font-size: 14px; line-height: 1.55; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  max-width: 1240px; margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 16px 28px;
  display: flex; flex-direction: column; gap: 16px;
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap; }
.brand { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.brand h1 { font-family: var(--f-display); font-weight: 700; font-size: 28px; line-height: 1.15; letter-spacing: -0.01em; margin: 0; text-wrap: balance; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.engine-status {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 8px 6px 12px; font-size: 13px; min-width: 0;
}
.engine-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--inacc); flex: none; }
.engine-status[data-state="ready"] .dot { background: var(--good); }
.engine-status[data-state="error"] .dot { background: var(--blunder); }
.engine-status .meter { width: 90px; height: 6px; border-radius: 3px; background: var(--panel-2); border: 1px solid var(--line); overflow: hidden; }
.engine-status .meter span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.engine-status button { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 2px 10px; font-size: 12.5px; }
.engine-status button:hover { border-color: var(--accent); }

/* Desk layout */
.desk { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 420px); gap: 20px; align-items: start; }
@media (max-width: 920px) { .desk { grid-template-columns: minmax(0, 1fr); } }
.board-col { display: flex; flex-direction: column; gap: 10px; min-width: 0; align-items: center; }
.board-col > * { width: min(100%, 700px, max(330px, calc(100vh - 150px))); }
.side-col { display: flex; flex-direction: column; gap: 14px; min-width: 0; }

/* Board */
.board-wrap { display: flex; gap: 8px; align-items: stretch; }
.evalbar {
  position: relative; width: 18px; flex: none; border-radius: 4px; overflow: hidden;
  background: var(--bar-black); border: 1px solid var(--line);
  display: flex; flex-direction: column-reverse;
}
.evalbar.flipped { flex-direction: column; }
.evalbar-white { background: var(--bar-white); height: 50%; transition: height .45s cubic-bezier(.3,.7,.3,1); }
.evalbar-num {
  position: absolute; left: 0; right: 0; text-align: center;
  font-family: var(--f-mono); font-size: 9.5px; font-weight: 600; letter-spacing: -0.04em;
}
.board-box { position: relative; flex: 1; min-width: 0; aspect-ratio: 1; max-width: 100%; }
#board { display: block; width: 100%; height: 100%; border-radius: 6px; touch-action: none; user-select: none; -webkit-user-select: none; box-shadow: var(--shadow); }
#board .sl { fill: var(--sq-light); }
#board .sd { fill: var(--sq-dark); }
#board .hl-last { fill: var(--hl-last); }
#board .hl-sel { fill: var(--hl-sel); }
#board .dot { fill: var(--dot); }
#board .ring { fill: none; stroke: var(--dot); stroke-width: 7; }
#board .co { font-family: var(--f-mono); font-size: 15px; font-weight: 600; pointer-events: none; }
#board .col { fill: var(--co-light); }
#board .cod { fill: var(--co-dark); }
#board .ar { fill: var(--accent); pointer-events: none; transition: opacity .15s; }
#board .ar.tb { fill: var(--good); }
#board .ar.threat { fill: var(--threat); opacity: .78; }
#board .ud { fill: none; stroke-width: 7; opacity: .8; pointer-events: none; }
#board .ud-ar { opacity: .8; pointer-events: none; }
#board .ud.ud-green { stroke: #1F9D4C; } #board .ud-ar.ud-green { fill: #1F9D4C; }
#board .ud.ud-red { stroke: #D23A2E; } #board .ud-ar.ud-red { fill: #D23A2E; }
#board .ud.ud-blue { stroke: #2F6FC6; } #board .ud-ar.ud-blue { fill: #2F6FC6; }
#board .ud.ud-yellow { stroke: #E2A21B; } #board .ud-ar.ud-yellow { fill: #E2A21B; }
#board .ar0 { opacity: .86; }
#board .ar1 { opacity: .48; }
#board .ar2 { opacity: .32; }
#board .ar3, #board .ar4 { opacity: .22; }
#board.has-hot .ar { opacity: .14; }
#board.has-hot .ar.hot { opacity: .92; }
#gPc { filter: drop-shadow(0 1.5px 1px rgba(0, 0, 0, .22)); }
.pc.ghosted { opacity: .28; }
text.pf-w, text.pl-w, text.pf-b { font-family: var(--f-piece); font-size: 86px; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
text.pf-w { fill: var(--piece-white); stroke: var(--piece-white); stroke-width: 2.5px; stroke-linejoin: round; }
text.pl-w { fill: var(--piece-edge); }
text.pf-b { fill: var(--piece-black); }

.promo { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(10, 16, 22, .35); border-radius: 6px; }
.promo-box { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.promo-box p { margin: 0; font-weight: 600; }
.promo-row { display: flex; gap: 6px; }
.promo-row button { width: 64px; height: 64px; border-radius: 8px; border: 1px solid var(--line); background: var(--sq-light); padding: 4px; }
.promo-row button:hover { border-color: var(--accent); }
.promo-row svg, .pal svg { width: 100%; height: 100%; display: block; }
.promo-box > button { border: none; background: none; color: var(--muted); font-size: 13px; }

/* Toolbar under the board */
.toolbar { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }
.tbtn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; font-size: 13.5px;
}
.tbtn.icon { width: 40px; padding: 0; justify-content: center; }
.tbtn svg { width: 16px; height: 16px; flex: none; }
.tbtn:hover:not(:disabled) { border-color: var(--accent); }
.tbtn:disabled { opacity: .4; cursor: default; }
.tbtn.primary { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.tbtn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.editor { display: flex; flex-direction: column; gap: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.palette { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.pal { aspect-ratio: 1; border: 1px solid var(--line); border-radius: 8px; background: var(--sq-light); padding: 3px; display: grid; place-items: center; max-height: 54px; }
.pal.tool { background: var(--panel-2); font-size: 12px; font-weight: 600; color: var(--muted); }
.pal[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.editor-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.editor-msg { margin: 0; color: var(--blunder); font-size: 13px; min-height: 1em; }
.editor-msg:empty { display: none; }

.fen-row { display: flex; gap: 6px; align-items: center; }
.fen-row label { font-family: var(--f-mono); font-size: 11.5px; font-weight: 600; color: var(--muted); letter-spacing: .06em; }
.fen-row input { flex: 1; min-width: 0; height: 34px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 0 10px; font-family: var(--f-mono); font-size: 12px; }
.caption { margin: 0; color: var(--muted); font-size: 12.5px; }
.caption:empty { display: none; }

/* Cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-family: var(--f-display); font-size: 17px; font-weight: 700; letter-spacing: -0.005em; }
.hint { color: var(--muted); font-size: 12.5px; }
.ctrls { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }
.ctrls select { height: 28px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel-2); padding: 0 4px; font-size: 12.5px; color: var(--ink); }
.ctrls select:disabled { opacity: .5; }
.ctrls-row { padding: 8px 10px; background: var(--panel-2); border-radius: 8px; gap: 6px 10px; }
.ctrls-row .grp { display: inline-flex; align-items: center; gap: 6px; }

/* Engine card */
.verdict { display: flex; align-items: center; gap: 12px; }
.big-eval { font-family: var(--f-mono); font-size: 26px; font-weight: 600; letter-spacing: -0.02em; min-width: 92px; font-variant-numeric: tabular-nums; }
.verdict-text { flex: 1; min-width: 0; }
.verdict-main { font-weight: 600; font-size: 15px; }
.verdict-sub { color: var(--muted); font-size: 12px; font-family: var(--f-mono); font-variant-numeric: tabular-nums; }
.src-badge { display: inline-block; font-family: var(--f-body); font-size: 11px; font-weight: 600; padding: 0 6px; border-radius: 999px; border: 1px solid currentColor; margin-right: 4px; }
.src-badge.cloud { color: var(--cloud); }
.src-badge.tb { color: var(--good); }
.ghost-btn { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 5px 10px; font-size: 12.5px; flex: none; }
.ghost-btn:hover:not(:disabled) { border-color: var(--accent); }
.ghost-btn:disabled { opacity: .45; cursor: default; }
.wdl { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); font-family: var(--f-mono); font-variant-numeric: tabular-nums; flex-wrap: wrap; }
.wdl-bar { display: flex; flex: 1 1 120px; max-width: 220px; height: 8px; border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.wdl-bar span { display: block; height: 100%; transition: width .4s; }
#wdlW { background: var(--bar-white); }
#wdlD { background: var(--draw); }
#wdlL { background: var(--bar-black); }
.progress { height: 3px; background: var(--panel-2); border-radius: 2px; overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s linear; }
.progress.infinite span { width: 30% !important; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
@media (prefers-reduced-motion: reduce) { .progress.infinite span { animation: none; width: 100% !important; opacity: .5; } .evalbar-white, .wdl-bar span { transition: none; } }

.lines { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.line { display: flex; gap: 8px; align-items: flex-start; padding: 6px 6px; border-radius: 8px; border: 1px solid transparent; }
.line:hover { background: var(--panel-2); border-color: var(--line); }
.line.is-shown { border-color: var(--accent); }
.line-empty { color: var(--muted); padding: 6px; }
.pill { flex: none; min-width: 58px; text-align: center; font-family: var(--f-mono); font-size: 12.5px; font-weight: 600; padding: 1px 6px; border-radius: 6px; font-variant-numeric: tabular-nums; border: 1px solid var(--line); }
.pill.pw { background: var(--bar-white); color: #1A2128; }
.pill.pb { background: var(--bar-black); color: #F2F4F5; border-color: var(--bar-black); }
.pv { flex: 1; min-width: 0; font-family: var(--f-mono); font-size: 12.5px; line-height: 1.7; }
.pv .mn { color: var(--muted); margin-right: 2px; }
.pv .mv { border: none; background: none; padding: 0 2px; border-radius: 4px; font-family: inherit; font-size: inherit; color: var(--ink); }
.pv .mv:hover { background: var(--accent-soft); color: var(--accent-ink); }
.pv .mv.first { font-weight: 600; }
.line .d { flex: none; color: var(--muted); font-family: var(--f-mono); font-size: 11px; padding-top: 2px; }
.line .d.cloud { color: var(--cloud); }

/* Endgame tablebase rows */
.tb-head { font-size: 12.5px; color: var(--good); font-weight: 600; }
.tb-row { display: flex; gap: 10px; align-items: center; padding: 5px 6px; border-radius: 8px; }
.tb-row:hover { background: var(--panel-2); }
.tb-row .mv { font-family: var(--f-mono); font-weight: 600; border: none; background: none; padding: 0 4px; border-radius: 4px; min-width: 54px; text-align: left; }
.tb-row .mv:hover { background: var(--accent-soft); }
.tb-chip { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; border: 1px solid currentColor; }
.tb-chip.win { color: var(--good); }
.tb-chip.draw, .tb-chip.cursed { color: var(--muted); }
.tb-chip.loss { color: var(--blunder); }
.tb-chip.unknown { color: var(--inacc); }
.tb-row .tb-extra { color: var(--muted); font-size: 12px; font-family: var(--f-mono); }

.last-move { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; padding-top: 8px; border-top: 1px solid var(--line); }
.last-move .lbl { color: var(--muted); }
.last-move b { font-family: var(--f-mono); font-weight: 600; }
.tag { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; border: 1px solid currentColor; }
.t-best, .t-good { color: var(--good); }
.t-brilliant { color: var(--brilliant); }
.t-great { color: var(--great); }
.threat { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); border-left: 3px solid var(--threat); }
.threat .lbl { color: var(--threat); font-weight: 600; }
.threat b { font-family: var(--f-mono); font-weight: 600; }
.threat .threat-line { color: var(--muted); font-family: var(--f-mono); font-size: 12px; }
.t-inacc { color: var(--inacc); }
.t-mistake { color: var(--mistake); }
.t-blunder { color: var(--blunder); }

/* Coach */
.coach-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.chip-btn { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.chip-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent-ink); }
.chip-btn:disabled { opacity: .45; cursor: default; }
.coach-form { display: flex; gap: 6px; }
.coach-form input { flex: 1; min-width: 0; height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 0 10px; font-size: 13.5px; }
.coach-form button { height: 36px; border-radius: 8px; border: 1px solid var(--ink); background: var(--ink); color: var(--panel); padding: 0 14px; font-weight: 600; font-size: 13.5px; }
.coach-form button:disabled { opacity: .45; cursor: default; }
.coach-key { display: flex; flex-direction: column; gap: 6px; padding: 10px; border: 1px dashed var(--line); border-radius: 10px; }
.coach-key label { font-weight: 600; font-size: 13px; }
.coach-key .hint a, .coach-keyline a { color: inherit; }
.coach-keyline { font-size: 12.5px; color: var(--muted); }
.linkbtn { border: none; background: none; padding: 0; color: var(--accent-ink); text-decoration: underline; font-size: inherit; }
.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 12.5px; color: var(--muted); }
.coach-out { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.coach-out.stale .coach-text { opacity: .55; }
.coach-meta { font-size: 12px; color: var(--muted); font-family: var(--f-mono); }
.coach-text { font-size: 14px; line-height: 1.7; overflow-wrap: anywhere; }
.coach-text p { margin: 0 0 6px; }
.coach-text ul { margin: 0 0 6px; padding-left: 18px; }
.coach-text li { margin: 2px 0; }
.coach-text .san { font-family: var(--f-mono); font-size: .92em; font-weight: 600; color: var(--accent-ink); border: none; background: none; padding: 0 1px; border-radius: 3px; text-decoration: underline dotted; text-underline-offset: 3px; cursor: pointer; }
.coach-text .san:hover { background: var(--accent-soft); }
.coach-text .turn { display: flex; flex-direction: column; gap: 4px; }
.coach-text .turn + .turn { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.coach-text .q { align-self: flex-end; max-width: 90%; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px 10px 2px 10px; padding: 4px 10px; font-size: 13px; color: var(--muted); }
.coach-text .thinking { color: var(--muted); }
.coach-tool { font-size: 12px; color: var(--muted); font-family: var(--f-mono); }
.coach-foot { display: flex; gap: 8px; align-items: center; font-size: 12.5px; color: var(--muted); }
.coach-off { margin: 0; color: var(--muted); font-size: 13px; }

/* Score sheet */
.moves { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; position: relative; display: grid; grid-template-columns: 1fr; font-family: var(--f-mono); font-size: 13px; }
.moves li { display: grid; grid-template-columns: 38px 1fr 1fr; align-items: center; }
.moves li:nth-child(odd) { background: var(--panel-2); }
.moves .num { color: var(--muted); padding-left: 6px; font-variant-numeric: tabular-nums; }
.moves .ph { color: var(--muted); padding: 3px 6px; }
.mcell { text-align: left; border: none; background: none; padding: 3px 6px; border-radius: 4px; font-family: inherit; font-size: inherit; }
.mcell:hover { background: var(--accent-soft); }
.mcell.cur { background: var(--accent); color: #FFF; font-weight: 600; }
.mcell .sym { margin-left: 3px; font-weight: 700; }
.mcell .sym.inacc { color: var(--inacc); }
.mcell .sym.mistake { color: var(--mistake); }
.mcell .sym.blunder { color: var(--blunder); }
.mcell .sym.brilliant { color: var(--brilliant); }
.mcell .sym.great { color: var(--great); }
.mcell.cur .sym { color: #FFF; }
.moves-empty { color: var(--muted); font-family: var(--f-body); padding: 6px; display: block !important; }

/* Modal + toast */
.modal { position: fixed; inset: 0; background: rgba(8, 12, 16, .45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-box { width: min(560px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); }
.modal-box h2 { margin: 0; font-family: var(--f-display); font-size: 18px; }
.modal-box p { margin: 0; color: var(--muted); font-size: 13px; }
.modal-box textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); padding: 10px; font-family: var(--f-mono); font-size: 12.5px; resize: vertical; }
.modal-box .err { color: var(--blunder); }
.modal-box .err:empty { display: none; }
.modal-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.toast { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 8px 14px; border-radius: 8px; font-size: 13px; z-index: 30; max-width: calc(100% - 32px); }

footer { color: var(--muted); font-size: 12px; line-height: 1.6; border-top: 1px solid var(--line); padding-top: 12px; }
footer a { color: inherit; }

@media (max-width: 520px) {
  .brand h1 { font-size: 23px; }
  .tbtn .lbl { display: none; }
  .tbtn { padding: 0 10px; }
  .big-eval { font-size: 22px; min-width: 76px; }
}
