:root {
  --bg: #161512;
  --panel: #262421;
  --panel-2: #302e2b;
  --text: #e8e6e3;
  --muted: #a8a39b;
  --accent: #629924;
  --accent-2: #7fa650;
  --danger: #c33;
  --border: #3b3a37;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: .5px; }
.tabs { display: flex; gap: 6px; }
.user-menu { margin-left: auto; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.tab {
  background: transparent; color: var(--muted); border: none;
  padding: 8px 16px; border-radius: var(--radius); cursor: pointer; font-size: 14px;
}
.tab.active { background: var(--panel-2); color: var(--text); }
.tab:hover { color: var(--text); }

.view { display: none; gap: 18px; padding: 18px; }
.view.active { display: flex; }
.login-view { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
body.authed .login-view { display: none; }
body:not(.authed) .topbar,
body:not(.authed) .view { display: none; }
.login-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
}
.login-card h1 { margin: 0 0 6px; }
.login-card p { color: var(--muted); margin-top: 0; }
.login-card label, .form-grid label {
  display: block;
  color: var(--muted);
  font-size: 13px;
  margin: 12px 0;
}
.login-card input, .form-grid input, .admin-import input, .admin-import select, .admin-import textarea {
  width: 100%;
  margin-top: 5px;
  padding: 9px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.admin-import {
  margin-bottom: 22px;
  padding: 14px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.admin-import h3 { margin: 0 0 6px; }
.admin-import label { display: block; color: var(--muted); font-size: 13px; margin: 12px 0; }
.admin-import textarea {
  min-height: 220px;
  resize: vertical;
  font-family: "SF Mono", ui-monospace, monospace;
  line-height: 1.35;
}
#import-token-output {
  min-height: 88px;
  resize: none;
  word-break: break-all;
}
.download-btn {
  display: inline-block;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  padding: 10px 14px;
  text-decoration: none;
  font-weight: 600;
}
.download-btn:hover { background: var(--accent-2); }
.form-error { color: #e0736b; margin-top: 12px; font-size: 13px; }

.sidebar {
  width: 280px; flex-shrink: 0;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.sidebar h2 { margin-top: 0; font-size: 15px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.sidebar label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.sidebar select {
  width: 100%; padding: 8px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; font-size: 14px;
}
.sidebar input {
  width: 100%; padding: 8px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; font-size: 14px;
}
.mini-actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.mini-actions input { flex: 1; }
.mini-actions .ghost { white-space: nowrap; }
.option-group {
  margin-top: 14px;
  padding: 10px;
  background: rgba(48, 46, 43, .6);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.helper { color: var(--muted); font-size: 12px; line-height: 1.35; margin: 6px 0 8px; }

.content { flex: 1; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; overflow: auto; }
.content.narrow { max-width: 720px; }
.form-grid { max-width: 520px; }
.check-row { display: flex !important; align-items: center; gap: 8px; }
.check-row input { width: auto; margin: 0; }
.admin-users { display: grid; gap: 10px; }
.admin-user {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 10px;
  align-items: center;
  padding: 12px;
  background: var(--panel-2);
  border-radius: 8px;
}
.admin-user .meta { color: var(--muted); font-size: 12px; }
.admin-user label { color: var(--muted); font-size: 13px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li {
  padding: 10px 12px; border-radius: 8px; cursor: pointer; margin-bottom: 4px;
  border: 1px solid transparent; display: flex; justify-content: space-between; gap: 8px;
}
.list li:hover { background: var(--panel-2); }
.list li.active { background: var(--panel-2); border-color: var(--accent); }
.list .meta { color: var(--muted); font-size: 12px; }
.list.lines li { font-family: "SF Mono", ui-monospace, monospace; font-size: 13px; flex-direction: column; align-items: flex-start; }

.hidden { display: none !important; }
.link-btn { background: none; border: none; color: var(--accent-2); cursor: pointer; padding: 0; margin-bottom: 10px; font-size: 14px; }

.detail-meta { color: var(--muted); font-size: 13px; margin-bottom: 12px; }

/* Training board */
.board-area { position: relative; flex-shrink: 0; }
.board-with-eval { display: flex; align-items: stretch; gap: 8px; }
body.hide-eval .board-with-eval { justify-content: center; }
body.hide-eval .eval-bar { display: none; }
.eval-bar {
  position: relative;
  width: 26px;
  height: min(70vh, 620px);
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #111;
  box-shadow: 0 8px 30px rgba(0,0,0,.25);
}
.eval-black, .eval-white {
  position: absolute;
  left: 0;
  width: 100%;
  transition: height .45s ease;
}
.eval-black { top: 0; height: 50%; background: #111; }
.eval-white { bottom: 0; height: 50%; background: #eee; }
.eval-label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  min-width: 64px;
  padding: 2px 5px;
  border-radius: 999px;
  background: rgba(38, 36, 33, .82);
  color: var(--text);
  font-family: "SF Mono", ui-monospace, monospace;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  z-index: 2;
}
.variation-title {
  width: min(70vh, 620px);
  min-height: 42px;
  margin: 0 0 10px;
  padding: 9px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}
.variation-title .title-text {
  font-weight: 700;
  font-size: 15px;
  line-height: 1.25;
}
.title-annot {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}
.title-annot .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}
.title-annot.yes {
  background: rgba(98, 153, 36, .18);
  color: #a5d76e;
}
.title-annot.yes .dot { background: var(--accent-2); }
.title-annot.no {
  background: rgba(214, 169, 79, .18);
  color: #e8c56f;
}
.title-annot.no .dot { background: #d6a94f; }
.cg-wrap {
  width: min(70vh, 620px);
  height: min(70vh, 620px);
  box-sizing: content-box;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
}

.trainpanel { width: 320px; flex-shrink: 0; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.status-row { display: flex; gap: 8px; margin-bottom: 12px; }
.badge { background: var(--panel-2); padding: 6px 10px; border-radius: 8px; font-size: 13px; }
.badge.muted { color: var(--muted); }
.badge.you { background: var(--accent); color: #fff; }
.badge.engine { background: #44607a; color: #fff; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.ghost, .primary {
  border-radius: 8px; cursor: pointer; font-size: 14px; padding: 9px 14px; border: 1px solid var(--border);
}
.ghost { background: var(--panel-2); color: var(--text); }
.ghost:disabled { opacity: .4; cursor: default; }
.ghost.danger { border-color: #8b3a34; color: #ffb0aa; }
.primary { background: var(--accent); color: #fff; border: none; width: 100%; padding: 12px; font-weight: 600; margin-top: 16px; }
.primary:hover { background: var(--accent-2); }

.comment { background: var(--panel-2); border-left: 3px solid var(--accent-2); padding: 10px 12px; border-radius: 6px; font-size: 14px; line-height: 1.45; margin-bottom: 14px; }

.move-log { font-family: "SF Mono", ui-monospace, monospace; font-size: 14px; line-height: 1.9; max-height: 240px; overflow: auto; }
.move-log .mv { padding: 1px 5px; border-radius: 4px; cursor: pointer; }
.move-log .mv:hover { background: var(--panel-2); }
.move-log .mv.you { color: var(--accent-2); }
.move-log .mv.blunder { color: #ffb0aa; border-bottom: 1px dotted #ffb0aa; }
.move-log .mv.test { opacity: .86; font-style: italic; }
.move-log .mv.current { background: var(--accent); color: #fff; }
.move-log .num { color: var(--muted); margin-right: 4px; }

/* Clickable SAN inside annotation text */
.movelink, .fn-move {
  cursor: pointer; color: var(--accent-2); border-bottom: 1px dotted var(--accent-2);
}
.movelink:hover, .fn-move:hover { color: #fff; background: var(--accent); border-radius: 3px; }

/* Full-line notes panel */
.full-notes {
  background: var(--panel-2); border-radius: 6px; padding: 12px 14px;
  font-size: 14px; line-height: 1.5; margin-bottom: 14px; max-height: 360px; overflow: auto;
}
.full-notes .fn-move { font-family: "SF Mono", ui-monospace, monospace; padding: 0 3px; border-bottom: none; font-weight: 600; }
.full-notes .fn-move.you { color: var(--accent-2); }
.full-notes .fn-comment { margin: 4px 0 10px; color: var(--text); line-height: 1.45; }

/* Review banner */
.review-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: #3a3320; border: 1px solid #6b5a1f; color: #e8d8a0;
  padding: 8px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px;
}
.review-banner .link-btn { margin: 0; color: #ffd866; }

.ghost.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Progress bars + summaries */
.progress-summary { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; line-height: 1.6; margin-bottom: 14px; }
.prog { margin-top: 6px; width: 100%; }
.prog .bar { height: 6px; background: #1d1c1a; border-radius: 4px; overflow: hidden; }
.prog .bar span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.list li.with-prog { flex-direction: column; align-items: stretch; gap: 6px; }
.list li.with-prog .row { display: flex; justify-content: space-between; gap: 8px; }

/* Explore line stats */
.line-head { display: flex; justify-content: space-between; gap: 10px; width: 100%; align-items: baseline; }
.linestat { font-size: 11px; color: var(--muted); white-space: nowrap; }
.linestat.done { color: var(--accent-2); }
.annot {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 20px; margin-right: 4px; font-size: 12px; font-weight: 700;
}
.annot.yes { color: var(--accent-2); }
.annot.no { color: #d6a94f; }

.done-banner { margin-top: 14px; padding: 12px; background: var(--accent); color: #fff; border-radius: 8px; text-align: center; font-weight: 600; }

.builder-moves { display: grid; gap: 12px; }
.builder-card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px;
}
.builder-card h3 { margin: 0 0 8px; font-size: 14px; }
.builder-card .blunder { font-family: "SF Mono", ui-monospace, monospace; font-size: 13px; margin: 4px 0; }
.builder-card .status { color: var(--muted); font-size: 12px; }

.toast {
  position: absolute; left: 50%; top: 16px; transform: translateX(-50%);
  background: var(--danger); color: #fff; padding: 10px 18px; border-radius: 8px;
  font-weight: 600; box-shadow: 0 6px 20px rgba(0,0,0,.4); z-index: 20;
  transition: opacity .2s;
}
.toast.ok { background: var(--accent); }

/* Promotion overlay */
.promo {
  position: absolute; inset: 0; background: rgba(0,0,0,.55); z-index: 30;
  display: flex; align-items: center; justify-content: center; gap: 10px;
}
.promo button {
  width: 72px; height: 72px; border-radius: 10px; border: 2px solid var(--border);
  background: var(--panel-2) center/70% no-repeat; cursor: pointer;
}
.promo button:hover { border-color: var(--accent); }

/* Piece set: our downloaded SVGs */
.cg-wrap piece.pawn.white   { background-image: url('/static/pieces/wP.svg'); }
.cg-wrap piece.knight.white { background-image: url('/static/pieces/wN.svg'); }
.cg-wrap piece.bishop.white { background-image: url('/static/pieces/wB.svg'); }
.cg-wrap piece.rook.white   { background-image: url('/static/pieces/wR.svg'); }
.cg-wrap piece.queen.white  { background-image: url('/static/pieces/wQ.svg'); }
.cg-wrap piece.king.white   { background-image: url('/static/pieces/wK.svg'); }
.cg-wrap piece.pawn.black   { background-image: url('/static/pieces/bP.svg'); }
.cg-wrap piece.knight.black { background-image: url('/static/pieces/bN.svg'); }
.cg-wrap piece.bishop.black { background-image: url('/static/pieces/bB.svg'); }
.cg-wrap piece.rook.black   { background-image: url('/static/pieces/bR.svg'); }
.cg-wrap piece.queen.black  { background-image: url('/static/pieces/bQ.svg'); }
.cg-wrap piece.king.black   { background-image: url('/static/pieces/bK.svg'); }

/* Tablet: keep the board prominent and let panels wrap below it. */
@media (max-width: 1180px) {
  .topbar {
    flex-wrap: wrap;
    gap: 10px 14px;
  }
  .tabs {
    order: 3;
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
  }
  .tab {
    flex: 0 0 auto;
  }
  .view.active {
    flex-wrap: wrap;
  }
  .sidebar {
    width: min(340px, 100%);
  }
  .board-area {
    flex: 1 1 520px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .variation-title,
  .cg-wrap {
    width: min(72vw, 620px);
  }
  .cg-wrap {
    height: min(72vw, 620px);
  }
  .eval-bar {
    height: min(72vw, 620px);
  }
  .trainpanel {
    width: 100%;
    flex: 1 1 100%;
  }
  .move-log {
    max-height: 180px;
  }
}

/* Phone: single-column layout, board fits the viewport width. */
@media (max-width: 720px) {
  body {
    overflow-x: hidden;
  }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    padding: 10px 12px;
  }
  .brand {
    font-size: 16px;
  }
  .user-menu {
    margin-left: 0;
    width: 100%;
    justify-content: space-between;
  }
  .tabs {
    gap: 4px;
  }
  .tab {
    padding: 8px 11px;
    font-size: 13px;
  }
  .view {
    padding: 10px;
    gap: 10px;
  }
  .view.active {
    display: block;
  }
  .sidebar,
  .content,
  .trainpanel,
  .board-area {
    width: 100%;
    margin-bottom: 10px;
  }
  .sidebar,
  .content,
  .trainpanel {
    padding: 12px;
  }
  .board-area {
    align-items: stretch;
  }
  .variation-title {
    width: 100%;
    min-height: 0;
    padding: 8px;
    flex-wrap: wrap;
    gap: 6px;
  }
  .variation-title .title-text {
    font-size: 13px;
  }
  .board-with-eval {
    justify-content: center;
    gap: 6px;
  }
  .cg-wrap {
    width: min(calc(100vw - 58px), 430px);
    height: min(calc(100vw - 58px), 430px);
  }
  body.hide-eval .cg-wrap {
    width: min(calc(100vw - 20px), 450px);
    height: min(calc(100vw - 20px), 450px);
  }
  .eval-bar {
    width: 20px;
    height: min(calc(100vw - 58px), 430px);
  }
  .eval-label {
    font-size: 10px;
    min-width: 54px;
  }
  .actions {
    gap: 6px;
  }
  .ghost,
  .primary {
    min-height: 40px;
    padding: 9px 10px;
    font-size: 13px;
  }
  .actions .ghost {
    flex: 1 1 calc(50% - 6px);
  }
  .status-row {
    flex-wrap: wrap;
  }
  .move-log {
    max-height: 150px;
    font-size: 13px;
  }
  .full-notes {
    max-height: 220px;
  }
  .list li,
  .line-head {
    flex-direction: column;
    align-items: flex-start;
  }
  .linestat {
    white-space: normal;
  }
  .mini-actions {
    flex-direction: column;
    align-items: stretch;
  }
  .admin-user {
    grid-template-columns: 1fr;
  }
  .toast {
    width: calc(100% - 28px);
    top: 10px;
    text-align: center;
  }
  .promo button {
    width: 58px;
    height: 58px;
  }
}

@media (max-width: 380px) {
  .cg-wrap {
    width: min(calc(100vw - 52px), 360px);
    height: min(calc(100vw - 52px), 360px);
  }
  body.hide-eval .cg-wrap {
    width: calc(100vw - 20px);
    height: calc(100vw - 20px);
  }
  .eval-bar {
    width: 18px;
    height: min(calc(100vw - 52px), 360px);
  }
  .actions .ghost {
    flex-basis: 100%;
  }
}
