/* Olivier Vitrac, PhD, HDR - olivier.vitrac@gmail.com — MIT License */

/* ── Theme variables ── */
:root {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  --board-size: 520px;
  --bg: #fafafa;
  --bg-surface: #fff;
  --text: #222;
  --text-muted: rgba(0,0,0,0.55);
  --border: #ddd;
  --border-hover: #888;
  --active-bg: #e8e8e8;
  --active-border: #333;
  --status-ok: #0a6;
  --status-err: #b00;
  --input-bg: #fff;
  --accent: #4a7c59;
  --accent-light: #e8f5e9;
  --lib-hover: #f0f0f0;
}
[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-surface: #252525;
  --text: #ddd;
  --text-muted: rgba(255,255,255,0.5);
  --border: #444;
  --border-hover: #888;
  --active-bg: #3a3a3a;
  --active-border: #bbb;
  --status-ok: #3ddc84;
  --status-err: #f44;
  --input-bg: #2a2a2a;
  --accent: #6aad7a;
  --accent-light: #2a3a2e;
  --lib-hover: #303030;
}

body { margin: 0; background: var(--bg); color: var(--text); transition: background 0.25s, color 0.25s; }

/* ── Top bar ── */
.topbar {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.brand { display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; }
.byline { opacity: 0.65; font-size: 0.85rem; }
.topbar-actions { display: flex; gap: 12px; align-items: center; }

.btn-sm {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.8rem;
}
.btn-sm:hover { border-color: var(--border-hover); }
.btn-sm.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-files { display: inline-flex; align-items: center; justify-content: center; padding: 4px 8px; text-decoration: none; }
.btn-files img { display: block; }

/* ── Theme toggle ── */
.theme-toggle { display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
.theme-toggle input { display: none; }
.toggle-track { position: relative; width: 36px; height: 20px; background: #ccc; border-radius: 10px; transition: background 0.25s; }
[data-theme="dark"] .toggle-track { background: #6a6; }
.toggle-thumb { position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; background: #fff; border-radius: 50%; transition: transform 0.25s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.theme-toggle input:checked ~ .toggle-track .toggle-thumb { transform: translateX(16px); }
.toggle-label { font-size: 0.8rem; min-width: 32px; }

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-columns: var(--board-size) 1fr;
  gap: 16px;
  padding: 16px;
  max-width: 1200px;
}
.col-board { display: grid; gap: 10px; align-content: start; }
.col-sidebar { display: grid; gap: 0; align-content: start; }

/* ── Board ── */
.board { width: var(--board-size); height: var(--board-size); }
.board-wrap { transition: transform 0.4s ease, filter 0.4s ease; transform-origin: center bottom; max-width: 100%; overflow: hidden; }
.board-wrap.perspective-3d {
  transform: perspective(900px) rotateX(22deg) scale(0.95);
}
.board-wrap.perspective-3d piece {
  filter: drop-shadow(2px 5px 4px rgba(0,0,0,0.35));
}

/* ── Controls ── */
.controls {
  display: grid;
  grid-template-columns: auto auto auto auto auto 1fr;
  gap: 6px;
  align-items: center;
}
.controls button {
  padding: 6px 10px;
  cursor: pointer;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  color: var(--text);
  font-size: 0.95rem;
}
.controls button:hover { border-color: var(--border-hover); }
.sliderWrap { display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
#plySlider { width: 100%; max-width: 220px; }

/* ── Panels ── */
.panel { border: 1px solid var(--border); border-radius: 10px; padding: 10px; background: var(--bg-surface); }
.panel h2 { margin: 0 0 6px 0; font-size: 0.95rem; }
.moves-panel { max-height: 280px; overflow: hidden; display: flex; flex-direction: column; }
.moves-panel h2 { flex-shrink: 0; }

/* ── Sidebar tabs ── */
.sidebar-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 12px;
}
.stab {
  flex: 1;
  padding: 10px 8px;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.2s, border-color 0.2s;
}
.stab:hover { color: var(--text); }
.stab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.sidebar-content { min-height: 300px; }
.sidebar-content.hidden { display: none; }

/* ── Library ── */
.lib-search input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.9rem;
  box-sizing: border-box;
}
.lib-filters { display: flex; gap: 6px; margin: 8px 0; flex-wrap: wrap; }
.lib-filter {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-surface);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.8rem;
}
.lib-filter:hover { border-color: var(--border-hover); color: var(--text); }
.lib-filter.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.lib-list { display: grid; gap: 6px; max-height: 520px; overflow-y: auto; }
.lib-item {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.lib-item:hover { background: var(--lib-hover); border-color: var(--border-hover); }
.lib-item.selected { border-color: var(--accent); background: var(--accent-light); }
.lib-item-name { font-weight: 600; font-size: 0.9rem; margin-bottom: 3px; }
.lib-item-meta { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 3px; }
.lib-item-desc { font-size: 0.8rem; color: var(--text-muted); line-height: 1.3; }
.lib-type-badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 8px;
  margin-right: 4px;
  font-weight: 600;
  text-transform: uppercase;
}
.lib-type-badge.opening { background: #e3f2fd; color: #1565c0; }
.lib-type-badge.endgame { background: #fff3e0; color: #e65100; }
.lib-type-badge.game    { background: #f3e5f5; color: #7b1fa2; }
[data-theme="dark"] .lib-type-badge.opening { background: #1e3a5f; color: #90caf9; }
[data-theme="dark"] .lib-type-badge.endgame { background: #4e2a00; color: #ffcc80; }
[data-theme="dark"] .lib-type-badge.game    { background: #3a1a4f; color: #ce93d8; }

/* ── Paste area (inside Paste tab) ── */
.tabs { display: flex; gap: 6px; margin-bottom: 8px; }
.tab {
  padding: 5px 10px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text);
  cursor: pointer;
  border-radius: 8px;
  font-size: 0.85rem;
}
.tab.active { border-color: var(--border-hover); font-weight: 600; }
.paste {
  width: 100%;
  min-height: 120px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  box-sizing: border-box;
  resize: vertical;
  background: var(--input-bg);
  color: var(--text);
}
.row { display: flex; gap: 8px; margin-top: 8px; }
.row button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
}
.row button:hover { border-color: var(--border-hover); }
.status { margin-top: 8px; font-size: 0.85rem; }
.status.ok { color: var(--status-ok); }
.status.err { color: var(--status-err); }

/* ── Move list ── */
.moves {
  display: grid;
  gap: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9rem;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
.moveRow {
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  gap: 6px;
  align-items: baseline;
}
.mn { opacity: 0.55; text-align: right; font-size: 0.85rem; }
.ply {
  padding: 3px 5px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  font-size: 0.88rem;
}
.ply:hover { border-color: var(--border); }
.ply.active { border-color: var(--active-border); font-weight: 700; background: var(--active-bg); }
.ply.empty { opacity: 0.25; cursor: default; }

/* ── Notes panel ── */
.notes-section { margin-bottom: 12px; }
.notes-section:empty { display: none; }
.notes-section h3 {
  font-size: 0.85rem;
  color: var(--accent);
  margin: 0 0 6px 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.notes-section p, .notes-section li { font-size: 0.88rem; line-height: 1.4; margin: 0 0 4px 0; }
.notes-section ul { margin: 0; padding-left: 18px; }
.notes-history-origin { font-style: italic; }
.notes-comment {
  padding: 8px 10px;
  background: var(--accent-light);
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  font-size: 0.88rem;
  line-height: 1.4;
}

/* ── Explain (legacy placeholder) ── */
.explain { min-height: 70px; line-height: 1.35; }
.muted { opacity: 0.6; font-size: 0.88rem; }

/* ── Footer ── */
.footer { padding: 10px 16px; border-top: 1px solid var(--border); opacity: 0.75; font-size: 0.85rem; }

/* ── Responsive: Tablet (≤900px) ── */
@media (max-width: 900px) {
  :root { --board-size: min(520px, calc(100vw - 32px)); }
  .layout {
    grid-template-columns: 1fr;
    max-width: 100%;
    padding: 12px;
  }
  .col-board { justify-items: center; }
  .board { width: var(--board-size); height: var(--board-size); }
  .controls { width: var(--board-size); margin: 0 auto; }
  .moves-panel { width: var(--board-size); margin: 0 auto; }
}

/* ── Responsive: Phone (≤600px) ── */
@media (max-width: 600px) {
  :root { --board-size: calc(100vw - 24px); }
  body { overflow-x: hidden; }
  .layout { padding: 8px; gap: 10px; }

  /* Topbar */
  .topbar { padding: 8px 10px; gap: 6px; }
  .brand strong { font-size: 1rem; }
  .byline { font-size: 0.72rem; }
  .topbar-actions { gap: 8px; }

  /* Board 3D: reduce tilt on small screens */
  .board-wrap.perspective-3d {
    transform: perspective(700px) rotateX(16deg) scale(0.96);
  }

  /* Controls */
  .controls {
    grid-template-columns: auto auto auto auto auto;
    width: 100%;
  }
  .sliderWrap { grid-column: 1 / -1; justify-content: stretch; }
  #plySlider { max-width: none; }

  /* Moves panel */
  .moves-panel { width: 100%; max-height: 180px; }

  /* Sidebar tabs */
  .stab { padding: 8px 4px; font-size: 0.82rem; }

  /* Library */
  .lib-list { max-height: 400px; }
  .lib-item { padding: 8px; }
  .lib-item-name { font-size: 0.85rem; }

  /* Paste */
  .paste { min-height: 90px; font-size: 0.8rem; }

  /* Notes */
  .notes-comment { padding: 6px 8px; font-size: 0.82rem; }
  .notes-section p, .notes-section li { font-size: 0.82rem; }

  /* Footer */
  .footer { padding: 8px 10px; font-size: 0.75rem; }
}

/* ── Responsive: Very small phone (≤380px) ── */
@media (max-width: 380px) {
  :root { --board-size: calc(100vw - 16px); }
  .layout { padding: 4px; gap: 6px; }
  .controls button { padding: 4px 6px; font-size: 0.85rem; }
  .byline { display: none; }
}
