/* TGPlay Chess — design system components.
 *
 * All rules are scoped to .tgc so they coexist peacefully with the legacy
 * main.css until the migration is complete. To opt a page into the new
 * design system, add `class="tgc"` to <body> (or any wrapper).
 *
 * Sections:
 *   1. Reset & base (scoped)
 *   2. Layout: shell / page / screen
 *   3. Buttons & primitives (chip, dot)
 *   4. Topbar
 *   5. Sidebar
 *   6. Card / Mode card / Stat tile
 *   7. Player strip & captured rack
 *   8. Chess board
 *   9. Helpers (typography, scroller, eyebrow, section-title)
 */

/* --- 1. Reset & base ------------------------------------- */
.tgc, .tgc *, .tgc *::before, .tgc *::after { box-sizing: border-box; }
.tgc {
  font-family: var(--font-body);
  font-size: 14px; line-height: 1.5;
  color: var(--text-1);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.tgc button, .tgc input, .tgc select, .tgc textarea { font: inherit; color: inherit; }
.tgc button { cursor: pointer; border: none; background: none; padding: 0; }
.tgc a { color: inherit; text-decoration: none; }

/* --- 2. Layout ------------------------------------------- */
.tgc .screen {
  width: 100%; min-height: 100vh;
  background: var(--ink-0); color: var(--text-1);
  position: relative; overflow: hidden;
}
.tgc .screen::before {
  content: ''; position: absolute; inset: 0;
  background-image:
    radial-gradient(60% 40% at 80% -10%, oklch(0.45 0.22 var(--hue-primary) / 0.18), transparent 60%),
    radial-gradient(40% 30% at 10% 0%, oklch(0.45 0.22 calc(var(--hue-primary) + 60) / 0.10), transparent 60%);
  pointer-events: none; z-index: 0;
}
.tgc .shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  position: relative; z-index: 1;
}
.tgc .shell--no-side { grid-template-columns: 1fr; }
.tgc .page { overflow-y: auto; overflow-x: hidden; min-height: 100vh; padding: 0 var(--page-pad) 40px; }
.tgc .page__inner { max-width: 1100px; margin: 0 auto; }

/* --- 3. Buttons & primitives ----------------------------- */
.tgc .btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 20px; min-height: 40px;
  background: var(--brand); color: white;
  border-radius: 10px; font-weight: 600;
  box-shadow: 0 8px 20px -8px var(--brand-glow);
  transition: transform .1s var(--tap), background .15s;
}
.tgc .btn-primary:hover { background: var(--brand-hi); transform: translateY(-1px); }
.tgc .btn-amber {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 20px; min-height: 40px;
  background: var(--amber); color: oklch(0.2 0.04 55);
  border-radius: 10px; font-weight: 700;
  box-shadow: 0 8px 20px -8px var(--amber-glow);
  transition: transform .1s var(--tap);
}
.tgc .btn-amber:hover { transform: translateY(-1px); }
.tgc .btn-ghost {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; min-height: 40px;
  background: oklch(1 0 0 / 0.07); color: var(--text-1);
  border-radius: 10px; font-weight: 500;
  transition: background .15s;
}
.tgc .btn-ghost:hover { background: oklch(1 0 0 / 0.13); }
.tgc .btn-danger {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; min-height: 40px;
  background: oklch(0.35 0.12 25 / 0.25); color: oklch(0.82 0.14 25);
  border: 1px solid oklch(0.5 0.2 25 / 0.3);
  border-radius: 10px; font-weight: 600;
}
.tgc .btn-danger:hover { background: oklch(0.4 0.14 25 / 0.35); }

.tgc .chips { display: flex; gap: 8px; flex-wrap: wrap; }
.tgc .chip {
  padding: 6px 12px; border-radius: 999px;
  background: var(--ink-2); border: 1px solid var(--ink-4);
  font-size: 12px; color: var(--text-2); font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer;
  transition: background .15s var(--tap), color .15s, border-color .15s;
}
.tgc .chip:hover { background: var(--ink-3); color: var(--text-1); }
.tgc .chip--active { background: var(--brand); border-color: var(--brand); color: white; }
.tgc .chip--amber {
  background: oklch(0.3 0.08 var(--hue-chess) / 0.2);
  border-color: oklch(0.5 0.15 var(--hue-chess) / 0.3);
  color: var(--amber);
}

.tgc .dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; }
.tgc .dot--live   { background: var(--live); box-shadow: 0 0 8px var(--live); animation: tgc-pulse 1.6s infinite; }
.tgc .dot--online { background: var(--live); }
.tgc .dot--away   { background: var(--amber); }
@keyframes tgc-pulse { 0%,100%{opacity:1} 50%{opacity:.35} }

/* --- 4. Topbar ------------------------------------------- */
.tgc .top {
  display: flex; align-items: center; gap: 12px;
  padding: 14px var(--page-pad);
  position: sticky; top: 0; z-index: 5;
  background: linear-gradient(180deg, var(--ink-0) 70%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.tgc .top__title {
  font-family: var(--font-display);
  font-size: 15px; font-weight: 700; letter-spacing: -0.01em;
  display: flex; align-items: center; gap: 8px;
}
.tgc .top__title small { color: var(--text-3); font-weight: 500; font-family: var(--font-body); font-size: 12px; }
.tgc .top__right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.tgc .top__icon {
  width: 34px; height: 34px; border-radius: 10px;
  background: var(--ink-2); border: 1px solid var(--ink-4);
  color: var(--text-2);
  display: grid; place-items: center;
  transition: background .12s, color .12s;
}
.tgc .top__icon:hover { color: var(--text-1); background: var(--ink-3); }
.tgc .top__avatar {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px; height: 34px;
  border-radius: 100px; background: var(--ink-2); border: 1px solid var(--ink-4);
  font-size: 13px; font-weight: 600;
}
.tgc .top__avatar i {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, var(--hot), oklch(0.55 0.22 40));
  display: grid; place-items: center; color: white; font-size: 11px; font-weight: 700;
  font-style: normal;
}

/* --- 5. Sidebar ------------------------------------------ */
.tgc .side {
  height: 100vh;
  padding: 18px 14px;
  border-right: 1px solid var(--ink-4);
  background: linear-gradient(180deg, var(--ink-0) 0%, oklch(0.09 0.018 265) 100%);
  display: flex; flex-direction: column; gap: 14px;
  overflow-y: auto;
  position: sticky; top: 0;
}
.tgc .side__logo { display: flex; align-items: center; gap: 10px; padding: 2px 8px 14px; }
.tgc .side__mark {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--brand-hi), var(--brand-lo));
  display: grid; place-items: center;
  font-family: var(--font-display); font-weight: 800; color: white; font-size: 13px;
  box-shadow: 0 6px 20px -6px var(--brand-glow);
}
.tgc .side__brand { font-family: var(--font-display); font-weight: 800; font-size: 17px; letter-spacing: -0.02em; }
.tgc .side__brand span { color: var(--brand-hi); }
.tgc .side__crumb {
  margin-left: auto;
  font-family: var(--font-display);
  font-size: 12px; font-weight: 600; color: var(--text-3);
  padding: 4px 8px; border-radius: 6px; background: var(--ink-2);
  display: flex; align-items: center; gap: 6px;
}
.tgc .side__back { color: var(--text-3); }
.tgc .side__section { display: flex; flex-direction: column; gap: 2px; }
.tgc .side__label {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--text-3); padding: 6px 12px 4px;
}
.tgc .side__item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: 13px; font-weight: 500;
  width: 100%; text-align: left;
  position: relative; min-height: 36px;
  transition: background .15s, color .15s;
}
.tgc .side__item:hover { background: var(--ink-2); color: var(--text-1); }
.tgc .side__item--active { background: var(--ink-2); color: var(--text-1); }
.tgc .side__item--active::before {
  content: ''; position: absolute; left: -14px; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 18px; background: var(--brand); border-radius: 0 3px 3px 0;
}
.tgc .side__item i {
  width: 18px; display: inline-flex; justify-content: center;
  color: var(--text-3); font-style: normal;
}
.tgc .side__item--active i { color: var(--brand-hi); }
.tgc .side__item u {
  margin-left: auto; font-size: 10px; color: var(--text-4);
  font-variant-numeric: tabular-nums; text-decoration: none; font-family: var(--font-mono);
}
.tgc .side__foot {
  margin-top: auto; padding: 12px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, oklch(0.22 0.08 var(--hue-primary) / 0.3), oklch(0.18 0.05 var(--hue-primary) / 0.2));
  border: 1px solid oklch(0.5 0.2 var(--hue-primary) / 0.2);
}
.tgc .side__foot h4 { margin: 0 0 2px; font-size: 12px; font-weight: 700; font-family: var(--font-display); }
.tgc .side__foot p { margin: 0 0 8px; font-size: 11px; color: var(--text-2); line-height: 1.4; }
.tgc .side__foot button {
  width: 100%; padding: 7px;
  border-radius: 6px; background: var(--brand); color: white;
  font-size: 11px; font-weight: 600;
}

/* --- 6. Card / Mode / Stat ------------------------------- */
.tgc .card {
  background: var(--ink-2);
  border: 1px solid var(--ink-4);
  border-radius: var(--radius-lg);
  overflow: hidden; position: relative;
  transition: transform .2s var(--tap), border-color .15s, box-shadow .2s;
}
.tgc .card:hover {
  transform: translateY(-2px);
  border-color: oklch(0.45 0.15 var(--hue-primary) / 0.5);
  box-shadow: var(--shadow-card);
}
.tgc .card__pad { padding: 18px; }
.tgc .card__name { font-family: var(--font-display); font-size: 15px; font-weight: 700; letter-spacing: -0.01em; margin: 0 0 2px; }
.tgc .card__desc { font-size: 12px; color: var(--text-3); margin: 0; }

.tgc .mode { display: flex; flex-direction: column; min-height: 180px; cursor: pointer; }
.tgc .mode__hdr { display: flex; align-items: center; gap: 10px; padding: 16px 16px 0; }
.tgc .mode__body { padding: 12px 16px 16px; flex: 1; display: flex; flex-direction: column; gap: 6px; }
.tgc .mode__icon {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center;
  background: oklch(0.28 0.08 var(--hue-primary) / 0.5);
  color: var(--brand-hi); font-size: 17px;
}
.tgc .mode__icon--amber { background: oklch(0.32 0.08 var(--hue-chess) / 0.4); color: var(--amber); }
.tgc .mode__icon--live  { background: oklch(0.28 0.08 150 / 0.4); color: var(--live); }
.tgc .mode__icon--hot   { background: oklch(0.30 0.10 var(--hue-accent-hot) / 0.35); color: var(--hot); }
.tgc .mode__title { font-family: var(--font-display); font-weight: 700; font-size: 16px; letter-spacing: -0.01em; }
.tgc .mode__sub { font-size: 12px; color: var(--text-3); }
.tgc .mode__meta {
  display: flex; gap: 12px;
  font-size: 11px; color: var(--text-3); font-family: var(--font-mono);
  margin-top: auto; padding-top: 8px; border-top: 1px solid var(--ink-3);
}
.tgc .mode__meta b { color: var(--text-1); font-weight: 600; }
.tgc .mode__motif { position: absolute; inset: 0; opacity: 0.08; pointer-events: none; }

.tgc .stat {
  padding: 14px 16px;
  background: var(--ink-2);
  border: 1px solid var(--ink-4);
  border-radius: var(--radius-md);
}
.tgc .stat__v { font-family: var(--font-display); font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }
.tgc .stat__v--brand { color: var(--brand-hi); }
.tgc .stat__v--amber { color: var(--amber); }
.tgc .stat__v--live  { color: var(--live); }
.tgc .stat__k { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; margin-top: 2px; }
.tgc .stat__spark { height: 24px; margin-top: 6px; display: flex; align-items: flex-end; gap: 2px; }
.tgc .stat__spark span { flex: 1; background: var(--brand-lo); border-radius: 1px; opacity: 0.6; }
.tgc .stat__spark span:last-child { background: var(--brand-hi); opacity: 1; }

/* --- 7. Player strip & rack ------------------------------ */
.tgc .pstrip {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  background: var(--ink-2); border: 1px solid var(--ink-4);
  border-radius: var(--radius-md);
}
.tgc .pstrip--turn {
  border-color: oklch(0.5 0.18 var(--hue-chess) / 0.5);
  box-shadow: 0 0 0 1px oklch(0.5 0.18 var(--hue-chess) / 0.3), 0 10px 30px -12px var(--amber-glow);
}
.tgc .pstrip__avatar {
  width: 40px; height: 40px; border-radius: 10px;
  display: grid; place-items: center; font-weight: 700; color: white;
  background: linear-gradient(135deg, oklch(0.5 0.15 var(--hue-primary)), oklch(0.35 0.1 var(--hue-primary)));
  flex-shrink: 0;
}
.tgc .pstrip__avatar--bot { background: linear-gradient(135deg, oklch(0.5 0.1 210), oklch(0.32 0.08 210)); }
.tgc .pstrip__avatar--you { background: linear-gradient(135deg, var(--hot), oklch(0.5 0.22 40)); }
.tgc .pstrip__info { flex: 1; min-width: 0; }
.tgc .pstrip__name { font-family: var(--font-display); font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.tgc .pstrip__rating { font-size: 11px; color: var(--text-3); font-family: var(--font-mono); }
.tgc .pstrip__clock {
  font-family: var(--font-mono);
  font-size: 26px; font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-1);
  padding: 4px 12px;
  background: oklch(0 0 0 / 0.3);
  border-radius: 8px;
  min-width: 96px; text-align: center;
  font-variant-numeric: tabular-nums;
}
.tgc .pstrip__clock--active { color: oklch(0.2 0.04 55); background: var(--amber); box-shadow: 0 0 24px -6px var(--amber-glow); }
.tgc .pstrip__clock--low { color: oklch(0.85 0.16 25); }

.tgc .rack { display: flex; flex-wrap: wrap; gap: 2px; align-items: center; min-height: 22px; font-size: 18px; line-height: 1; }
.tgc .rack__plus { margin-left: 6px; font-size: 11px; font-family: var(--font-mono); color: var(--amber); font-weight: 700; }

/* --- 8. Chess board (document-wide; no .tgc scope) ------ */
.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1; width: 100%;
  background: var(--sq-dark);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow:
    0 1px 0 oklch(1 0 0 / 0.06) inset,
    0 20px 50px -20px oklch(0 0 0 / 0.8),
    0 0 0 1px var(--ink-4);
  position: relative; user-select: none;
  /* Container queries let pieces auto-size to actual board width. */
  container-type: inline-size;
  /* Block native browser drag of glyphs as fallback */
  -webkit-user-select: none;
  touch-action: none;
}
.board__sq {
  position: relative;
  display: grid; place-items: center;
  font-size: 9.5cqi;          /* fallback for older browsers handled below */
  line-height: 1;
  transition: background .12s;
}
@supports not (font-size: 1cqi) {
  .board__sq { font-size: clamp(18px, 4vw, 44px); }
}
.board__sq--l { background: var(--sq-light); }
.board__sq--d { background: var(--sq-dark); }
.board__sq--sel-l { background: var(--sq-light-sel); }
.board__sq--sel-d { background: var(--sq-dark-sel); }
.board__sq--last-l { background: color-mix(in oklch, var(--sq-light) 60%, var(--sq-last)); }
.board__sq--last-d { background: color-mix(in oklch, var(--sq-dark) 60%, var(--sq-last)); }
.board__sq--check { background: var(--sq-check) !important; }

.board__hint {
  position: absolute;
  width: 28%; height: 28%; border-radius: 50%;
  background: oklch(0 0 0 / 0.32);
  pointer-events: none;
}
.board__hint--capture { width: 82%; height: 82%; background: transparent; border: 4px solid oklch(0 0 0 / 0.3); }

.board__piece {
  font-family: var(--font-piece);
  text-shadow: 0 2px 3px oklch(0 0 0 / 0.25);
  position: relative; z-index: 1;
  pointer-events: none;
}
.board__piece--w { color: oklch(0.98 0.01 90); }
.board__piece--b { color: oklch(0.15 0.02 265); }

/* Animated move: piece slides from origin square. JS sets --mx/--my. */
.board__piece--moving {
  --mx: 0px;
  --my: 0px;
  transition: transform 220ms cubic-bezier(0.2, 0.9, 0.2, 1);
  transform: translate(var(--mx), var(--my));
  z-index: 2;
}

/* Drag ghost — fixed-position copy that follows the cursor. */
.board__drag {
  position: fixed;
  top: 0; left: 0;
  z-index: 9999;
  display: grid; place-items: center;
  pointer-events: none;
  font-family: var(--font-piece);
  text-shadow: 0 6px 10px oklch(0 0 0 / 0.5);
  transform: translate(-9999px, -9999px); /* hidden until first move event */
  will-change: transform;
}

/* Coordinate overlay */
.board__rank, .board__file {
  position: absolute;
  font-size: 9px; font-weight: 700;
  font-family: var(--font-mono);
  opacity: 0.55; pointer-events: none;
}
.board__rank { top: 2px; left: 3px; }
.board__file { bottom: 1px; right: 3px; }
.board__sq--l .board__rank, .board__sq--l .board__file { color: var(--sq-dark); }
.board__sq--d .board__rank, .board__sq--d .board__file { color: var(--sq-light); }

/* Flipped: rotate the whole board. Pieces stay upright via inverse rotate. */
.board--flipped { transform: rotate(180deg); }
.board--flipped .board__piece,
.board--flipped .board__rank,
.board--flipped .board__file,
.board--flipped .board__hint { transform: rotate(180deg); }
/* --- 9. Helpers ------------------------------------------ */
.tgc .font-display { font-family: var(--font-display); letter-spacing: -0.02em; }
.tgc .font-mono { font-family: var(--font-mono); }
.tgc .tabular { font-variant-numeric: tabular-nums; }

.tgc .scroller::-webkit-scrollbar { width: 8px; height: 8px; }
.tgc .scroller::-webkit-scrollbar-track { background: transparent; }
.tgc .scroller::-webkit-scrollbar-thumb { background: var(--ink-4); border-radius: 4px; }
.tgc .scroller::-webkit-scrollbar-thumb:hover { background: var(--ink-5); }
.tgc .scroller { scrollbar-width: thin; scrollbar-color: var(--ink-4) transparent; }

.tgc .page__eyebrow {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 11px; font-weight: 700;
  letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--brand-hi);
}
.tgc .page__eyebrow::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%;
  background: var(--brand-hi); box-shadow: 0 0 10px var(--brand-glow);
}
.tgc .section-title {
  display: flex; align-items: center; justify-content: space-between;
  margin: 26px 0 14px;
}
.tgc .section-title h3 {
  font-family: var(--font-display);
  font-size: 18px; font-weight: 700;
  margin: 0; letter-spacing: -0.01em;
}
.tgc .section-title a { color: var(--text-3); font-size: 12px; font-weight: 600; cursor: pointer; }
.tgc .section-title a:hover { color: var(--text-1); }