/* Carnet d'echecs - feuille de style unique, mobile first. */
:root {
  color-scheme: light dark;
  --bg: #f4f4f6;
  --surface: #ffffff;
  --surface-2: #ebebf0;
  --text: #17171b;
  --muted: #6a6a75;
  --line: #dcdce3;
  --accent: #6c4ee0;
  --accent-soft: #ece7fd;
  --ok: #1f8a55;
  --warn: #c2651a;
  /* Le plateau garde la meme palette en theme clair et sombre. */
  --light-sq: #ebecd0;
  --dark-sq: #739552;
  --mark: rgba(245, 246, 130, .55);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --bg: #131316;
    --surface: #1d1d22;
    --surface-2: #26262d;
    --text: #f0f0f4;
    --muted: #9b9baa;
    --line: #32323b;
    --accent: #a78bfa;
    --accent-soft: #2b2545;
    --ok: #4ade80;
    --warn: #fbbf24;
  }
}

html[data-theme="dark"] {
  --bg: #131316;
  --surface: #1d1d22;
  --surface-2: #26262d;
  --text: #f0f0f4;
  --muted: #9b9baa;
  --line: #32323b;
  --accent: #a78bfa;
  --accent-soft: #2b2545;
  --ok: #4ade80;
  --warn: #fbbf24;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-top: calc(52px + var(--safe-top));
  padding-bottom: calc(64px + var(--safe-bottom));
  overscroll-behavior-y: contain;
}

/* --- Chrome de l'application --------------------------------------------- */

.app-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  height: calc(52px + var(--safe-top));
  padding: var(--safe-top) 8px 0;
  display: flex; align-items: center; gap: 4px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
}
.app-bar h1 { flex: 1; font-size: 17px; font-weight: 650; margin: 0; text-align: center; letter-spacing: -.01em; }
.app-bar button {
  width: 40px; height: 40px; border: 0; background: none; color: var(--accent);
  font-size: 20px; border-radius: 10px; cursor: pointer;
}
.app-bar .back { font-size: 30px; line-height: 1; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; font-size: 11px; text-decoration: none; color: var(--muted);
}
.tabbar a span { font-size: 20px; line-height: 1; }
.tabbar a.active { color: var(--accent); }
.tabbar b {
  position: absolute; top: 4px; right: 50%; margin-right: -22px;
  min-width: 18px; padding: 1px 5px; border-radius: 9px;
  background: var(--accent); color: #fff; font-size: 10px; font-weight: 700;
}

.view { padding: 14px 14px 24px; max-width: 720px; margin: 0 auto; }

/* --- Blocs generiques ----------------------------------------------------- */

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
}
.card h2 { margin: 0 0 6px; font-size: 16px; }
.card p { margin: 0 0 8px; color: var(--muted); font-size: 14px; }
.card p.lead { color: var(--text); }

.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

button.primary, a.primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--accent); color: #fff; border: 0; border-radius: 12px;
  padding: 12px 16px; font-size: 15px; font-weight: 600; cursor: pointer;
  text-decoration: none; width: 100%;
}
button.secondary {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 14px; font-size: 14px; cursor: pointer;
}
button.link { background: none; border: 0; color: var(--accent); font-size: 14px; padding: 6px 0; cursor: pointer; }
button:disabled { opacity: .45; }

.tag {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
}
.tag.grey { background: var(--surface-2); color: var(--muted); }
.tag.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }

.list { display: grid; gap: 10px; }
.list a {
  display: block; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
}
.list a strong { display: block; font-size: 15px; margin-bottom: 2px; }
.list a small { color: var(--muted); font-size: 13px; }
.list .meta { margin-top: 6px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

.chapter-tile { display: flex; align-items: center; gap: 12px; }
.chapter-tile .glyph {
  width: 42px; height: 42px; flex: none; display: grid; place-items: center;
  background: var(--surface-2); border-radius: 12px; font-size: 22px;
}
.progress { height: 5px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.progress i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

/* --- Echiquier ------------------------------------------------------------ */

.board {
  position: relative; width: 100%; aspect-ratio: 1;
  border-radius: 6px; overflow: hidden; touch-action: pan-y;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .28);
}
.board-grid { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(8, 1fr); }
.square { position: relative; }
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }
.square.marked::after {
  content: ''; position: absolute; inset: 0; background: var(--mark);
}
.square.show-file::before, .square.show-rank::after {
  position: absolute; font-size: clamp(8px, 2.1vw, 11px); font-weight: 700;
  font-family: ui-sans-serif, system-ui, sans-serif;
}
/* Coordonnees dans la couleur de la case opposee, comme sur les plateaux classiques. */
.square.show-file::before { content: attr(data-file); bottom: 1px; right: 3px; }
.square.show-rank::after { content: attr(data-rank); top: 1px; left: 3px; }
.square.light.show-file::before, .square.light.show-rank::after { color: var(--dark-sq); }
.square.dark.show-file::before, .square.dark.show-rank::after { color: var(--light-sq); }

.board-pieces { position: absolute; inset: 0; }
.piece {
  position: absolute; top: 0; left: 0; width: 12.5%; height: 12.5%;
  transition: transform .22s cubic-bezier(.3, .8, .4, 1), opacity .2s;
  will-change: transform; pointer-events: none; user-select: none;
}
.piece-svg { display: block; width: 100%; height: 100%; overflow: visible; }
.piece-svg .body {
  stroke: #22201d; stroke-width: 1.4; stroke-linejoin: round; stroke-linecap: round;
}
.piece-svg.w .body { fill: #fafafa; }
.piece-svg.b .body { fill: #3b3936; }
.piece-svg .detail { fill: none; stroke-width: 1.2; stroke-linecap: round; }
.piece-svg.w .detail { stroke: #22201d; }
.piece-svg.b .detail { stroke: #d9d7d2; }
.piece-svg .detail .detail-solid { fill: #22201d; stroke: none; }
.piece-svg.b .detail .detail-solid { fill: #d9d7d2; }
.piece-svg { filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .35)); }

.piece.captured { opacity: 0; }
.piece.moving { z-index: 3; }

/* --- Lecteur de fiche ----------------------------------------------------- */

.player .controls {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 10px;
}
.player .controls button {
  background: var(--surface-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 11px 0; font-size: 16px; cursor: pointer;
}
.player .controls button.play { background: var(--accent); color: #fff; border-color: transparent; }

.movelist {
  display: flex; flex-wrap: wrap; gap: 4px; margin-top: 10px;
  max-height: 132px; overflow-y: auto; padding: 2px;
}
.movelist .num { color: var(--muted); font-size: 13px; padding: 4px 2px 4px 6px; }
.movelist button {
  border: 1px solid transparent; background: var(--surface-2); color: var(--text);
  border-radius: 8px; padding: 4px 8px; font-size: 14px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; cursor: pointer;
}
.movelist button.current { background: var(--accent); color: #fff; }

.comment {
  margin-top: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--accent-soft); font-size: 14px; min-height: 42px;
}
.comment:empty { display: none; }

ul.keys { margin: 8px 0 0; padding-left: 18px; font-size: 14px; }
ul.keys li { margin-bottom: 6px; }

textarea, input[type="text"], input[type="password"], select {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 15px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
}
textarea { min-height: 90px; resize: vertical; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 10px 0 4px; }

.error { color: #d33; font-size: 13px; margin-top: 6px; }
.ok-msg { color: var(--ok); font-size: 13px; margin-top: 6px; }

/* --- Revision ------------------------------------------------------------- */

.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; }
.rate button {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
  border-radius: 12px; padding: 10px 4px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.rate button small { display: block; font-weight: 400; font-size: 10px; color: var(--muted); margin-top: 2px; }
.rate button[data-rating="0"] { border-color: color-mix(in srgb, #d33 40%, var(--line)); }
.rate button[data-rating="3"] { border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 10px; text-align: center;
}
.stat b { display: block; font-size: 22px; line-height: 1.2; }
.stat span { font-size: 11px; color: var(--muted); }

.empty { text-align: center; padding: 32px 12px; color: var(--muted); }
.empty .big { font-size: 40px; display: block; margin-bottom: 8px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(78px + var(--safe-bottom)); z-index: 40;
  background: #17171b; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.3);
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
