/* ==========================================================================
   ChessCoach Scribe — CSS Mobile-First & PWA Hors-Ligne
   ========================================================================== */

:root {
  color-scheme: dark;
  --bg: #09090b;
  --surface: #141418;
  --surface-2: #1f1f26;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.2);
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --accent: #10b981;
  --accent-strong: #059669;
  --accent-soft: rgba(16, 185, 129, 0.16);
  --warn: #f59e0b;
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --sq-light: #d8e2dc;
  --sq-dark: #4a6b5d;
  --target-size: 24px;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (pointer: coarse) {
  :root {
    --target-size: 44px;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  overflow-wrap: break-word;
  -webkit-tap-highlight-color: transparent;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

h1, h2, h3 {
  text-wrap: balance;
  margin: 0;
}

p, li {
  text-wrap: pretty;
}

:where(
  button:not(.square),
  select,
  textarea,
  input:not([type="checkbox"], [type="radio"]),
  [role="button"],
  label:has(input[type="checkbox"], input[type="radio"])
) {
  min-block-size: var(--target-size);
  min-inline-size: var(--target-size);
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 0.65rem;
  transition: background-color 120ms ease, border-color 120ms ease, transform 80ms ease;
}

button:active:not(:disabled) {
  transform: scale(0.97);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ==========================================================================
   App Shell (100dvh mobile viewport sans défilement superflu)
   ========================================================================== */

.app-shell {
  min-block-size: 100dvh;
  max-inline-size: 520px;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  padding-top: max(0.35rem, env(safe-area-inset-top, 0px));
  padding-bottom: max(0.45rem, env(safe-area-inset-bottom, 0px));
  padding-left: max(0.5rem, env(safe-area-inset-left, 0px));
  padding-right: max(0.5rem, env(safe-area-inset-right, 0px));
  gap: 0.4rem;
}

@media (min-block-size: 540px) {
  .app-shell {
    block-size: 100dvh;
  }
}

/* 1. Top Bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.game-pill {
  flex: 1 1 auto;
  min-inline-size: 0;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.35rem 0.65rem;
  background: var(--surface);
  border-color: var(--border);
  text-align: left;
}

.round-badge {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: clamp(0.75rem, 0.7rem + 0.3vw, 0.85rem);
  font-weight: 700;
  color: #6ee7b7;
  background: var(--accent-soft);
  border: 1px solid rgba(16, 185, 129, 0.35);
  padding: 0.15rem 0.45rem;
  border-radius: 0.45rem;
}

.players-subtitle {
  flex: 1 1 auto;
  min-inline-size: 0;
  font-size: clamp(0.85rem, 0.8rem + 0.35vw, 0.95rem);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pill-chevron {
  color: var(--muted);
  font-size: 0.8rem;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex: 0 0 auto;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0.6rem;
  background: var(--surface);
}

.icon-btn[aria-pressed="true"] {
  background: rgba(245, 158, 11, 0.18);
  border-color: rgba(245, 158, 11, 0.6);
  color: #fcd34d;
}

.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.65rem;
  background: var(--surface);
  font-size: 0.85rem;
  font-weight: 600;
}

.count-badge {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #6ee7b7;
}

/* 2. Status & Move Tape */
.tape-bar {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.turn-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: clamp(0.8rem, 0.75rem + 0.3vw, 0.9rem);
  font-weight: 600;
}

.turn-pill.is-check {
  border-color: rgba(239, 68, 68, 0.55);
  background: var(--danger-soft);
  color: #fecaca;
}

.turn-pill.is-over {
  border-color: rgba(16, 185, 129, 0.55);
  background: var(--accent-soft);
  color: #a7f3d0;
}

.turn-dot {
  width: 0.72rem;
  height: 0.72rem;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.45);
  flex: 0 0 auto;
}

.turn-dot.is-white {
  background: #ffffff;
}

.turn-dot.is-black {
  background: #18181b;
}

.history-banner {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.history-banner[hidden] {
  display: none;
}

.history-label {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: #fcd34d;
}

.mini-btn {
  min-block-size: max(32px, var(--target-size));
  min-inline-size: max(32px, var(--target-size));
  padding: 0.2rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 0.5rem;
}

.mini-btn-accent {
  background: var(--accent-soft);
  border-color: rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

.mini-btn-warn {
  background: rgba(245, 158, 11, 0.16);
  border-color: rgba(245, 158, 11, 0.5);
  color: #fde68a;
}

.move-tape {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  padding: 0.25rem 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.65rem;
  min-block-size: 2.4rem;
}

.tape-empty {
  color: var(--muted);
  font-size: 0.82rem;
  padding-inline: 0.25rem;
  white-space: nowrap;
}

.tape-num {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--muted);
  padding-inline: 0.2rem 0.05rem;
  flex: 0 0 auto;
}

.ply-chip {
  flex: 0 0 auto;
  min-block-size: max(34px, var(--target-size));
  min-inline-size: max(34px, var(--target-size));
  padding: 0.2rem 0.5rem;
  border-radius: 0.45rem;
  border: 1px solid transparent;
  background: transparent;
  font-family: var(--font-mono);
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
}

.ply-chip:hover {
  background: var(--surface-2);
}

.ply-chip.is-active {
  background: var(--accent-soft);
  border-color: rgba(16, 185, 129, 0.6);
  color: #a7f3d0;
}

.ply-star {
  color: #fbbf24;
  font-size: 0.78rem;
}

/* 3. Board Stage */
.board-stage {
  flex: 1 1 0;
  min-block-size: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.gameover-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0.65rem;
  border-radius: 0.65rem;
  background: var(--accent-soft);
  border: 1px solid rgba(16, 185, 129, 0.45);
  flex: 0 0 auto;
}

.gameover-banner[hidden] {
  display: none;
}

.gameover-text {
  font-size: 0.85rem;
  font-weight: 700;
  color: #a7f3d0;
}

.board-wrap {
  flex: 1 1 0;
  min-block-size: 220px;
  inline-size: 100%;
  display: grid;
  place-items: center;
  container-type: size;
}

.chessboard {
  inline-size: min(100cqi, 100cqb);
  block-size: min(100cqi, 100cqb);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 0.6rem;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.square {
  position: relative;
  border: 0;
  border-radius: 0;
  padding: 0;
  margin: 0;
  min-inline-size: 0;
  min-block-size: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: none;
}

.square:active:not(:disabled) {
  transform: none;
}

.square.sq-light {
  background: var(--sq-light);
  color: #2f453c;
}

.square.sq-dark {
  background: var(--sq-dark);
  color: #e6eee9;
}

.square.is-last-move::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(245, 158, 11, 0.34);
  pointer-events: none;
}

.square.is-selected {
  box-shadow: inset 0 0 0 3px #10b981;
}

.square.is-selected::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(16, 185, 129, 0.32);
  pointer-events: none;
}

.square.is-check {
  background: radial-gradient(circle at center, rgba(239, 68, 68, 0.92) 0%, rgba(220, 38, 38, 0.55) 55%, transparent 78%), var(--sq-dark);
}

.square.sq-light.is-check {
  background: radial-gradient(circle at center, rgba(239, 68, 68, 0.92) 0%, rgba(220, 38, 38, 0.55) 55%, transparent 78%), var(--sq-light);
}

.square.is-legal-move::after {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(9, 9, 11, 0.34);
  pointer-events: none;
  z-index: 3;
}

.square.is-legal-capture::after {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: 4px solid rgba(9, 9, 11, 0.38);
  pointer-events: none;
  z-index: 3;
}

.coord-rank,
.coord-file {
  position: absolute;
  font-family: var(--font-mono);
  font-size: clamp(0.55rem, 0.5rem + 0.3vw, 0.7rem);
  font-weight: 700;
  line-height: 1;
  opacity: 0.78;
  pointer-events: none;
  z-index: 1;
}

.coord-rank {
  top: 2px;
  left: 3px;
}

.coord-file {
  bottom: 2px;
  right: 3px;
}

.piece-svg {
  width: 86%;
  height: 86%;
  position: relative;
  z-index: 2;
  pointer-events: none;
  display: block;
}

.square.is-dragging .piece-svg {
  opacity: 0.25;
}

.drag-ghost {
  position: fixed;
  width: 64px;
  height: 64px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 9999;
  filter: drop-shadow(0 8px 14px rgba(0, 0, 0, 0.55));
}

/* 4. Bottom Action Bar */
.action-bar {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  flex: 0 0 auto;
}

.action-row {
  display: grid;
  gap: 0.4rem;
}

.action-row-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.action-row-3 {
  grid-template-columns: 1fr 1.35fr 1fr;
}

.thumb-btn {
  min-block-size: 46px;
  padding: 0.35rem 0.4rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-size: clamp(0.76rem, 0.72rem + 0.25vw, 0.88rem);
  font-weight: 600;
  background: var(--surface);
  border-color: var(--border);
}

.thumb-btn[aria-pressed="true"] {
  background: rgba(245, 158, 11, 0.2);
  border-color: rgba(245, 158, 11, 0.6);
  color: #fde68a;
}

.thumb-primary {
  background: var(--accent-strong);
  border-color: rgba(16, 185, 129, 0.75);
  color: #ffffff;
  font-weight: 700;
}

.thumb-secondary {
  background: var(--surface-2);
}

/* ==========================================================================
   Toast
   ========================================================================== */

.toast {
  position: fixed;
  margin: 0;
  inset: auto;
  left: 50%;
  bottom: calc(7.2rem + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 10px);
  background: #18181b;
  color: #ecfdf5;
  border: 1px solid rgba(16, 185, 129, 0.55);
  border-radius: 999px;
  padding: 0.45rem 0.95rem;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.6);
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease, transform 150ms ease;
  z-index: 60;
  max-inline-size: calc(100vw - 2rem);
  text-align: center;
}

.toast:is(:popover-open, .\:popover-open, .is-visible) {
  opacity: 1;
  transform: translate(-50%, 0);
}

.helper-note {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0;
}

/* ==========================================================================
   Native <dialog> Modals (sans display inconditionnel sur dialog)
   ========================================================================== */

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgba(2, 6, 23, 0.74);
  backdrop-filter: blur(3px);
}

.scribe-dialog {
  margin: auto;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 1rem;
  background: var(--surface);
  color: var(--text);
  inline-size: min(94vw, 480px);
  max-block-size: 88dvh;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.7);
}

.scribe-dialog.dialog-compact {
  inline-size: min(92vw, 380px);
}

.scribe-dialog[open] {
  display: flex;
  flex-direction: column;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.dialog-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.dialog-title {
  font-size: clamp(1rem, 0.95rem + 0.35vw, 1.15rem);
  font-weight: 700;
}

.dialog-close {
  min-inline-size: max(36px, var(--target-size));
  min-block-size: max(36px, var(--target-size));
  padding: 0.2rem 0.55rem;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: 1rem;
}

.dialog-body {
  padding: 0.95rem 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  flex: 1 1 auto;
}

.dialog-footer {
  padding: 0.85rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  flex-wrap: wrap;
  flex: 0 0 auto;
}

.dialog-footer-split {
  justify-content: space-between;
}

.btn-primary,
.btn-secondary,
.btn-danger,
.btn-danger-ghost {
  padding: 0.55rem 0.95rem;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 0.65rem;
}

.btn-primary {
  background: var(--accent-strong);
  border-color: rgba(16, 185, 129, 0.8);
  color: #ffffff;
}

.btn-secondary {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.btn-danger {
  background: #dc2626;
  border-color: #ef4444;
  color: #ffffff;
}

.btn-danger-ghost {
  background: var(--danger-soft);
  border-color: rgba(239, 68, 68, 0.38);
  color: #fca5a5;
}

/* Promotion Grid */
.promo-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem;
  padding: 1rem;
}

.promo-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 0.65rem;
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.promo-btn .piece-svg {
  width: 52px;
  height: 52px;
}

/* Form Elements inside Modals */
.field-group {
  border: 0;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.field-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}

.players-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.45rem;
}

.result-option {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0.45rem 0.25rem;
  border-radius: 0.65rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
}

.result-option input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.result-option span {
  display: flex;
  flex-direction: column;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.9rem;
}

.result-option small {
  font-family: var(--font-sans);
  font-weight: 500;
  font-size: 0.7rem;
  color: var(--muted);
}

.result-option:has(input:checked) {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #a7f3d0;
}

.input-pair {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
}

.input-stack {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.82rem;
  color: var(--muted);
}

.input-stack input,
.input-stack select,
.mono-box,
.mono-line {
  inline-size: 100%;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 0.55rem;
  padding: 0.5rem 0.65rem;
  color: var(--text);
}

.mono-box,
.mono-line {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  overflow-wrap: anywhere;
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--accent);
}

/* Notebook List */
.notebook-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.notebook-toolbar > * {
  flex: 1 1 auto;
}

.notebook-list {
  padding: 0.85rem 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  flex: 1 1 auto;
}

.notebook-empty {
  text-align: center;
  color: var(--muted);
  padding: 1.5rem 0.5rem;
  font-size: 0.9rem;
}

.game-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.8rem;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.game-card.is-active-card {
  border-color: rgba(16, 185, 129, 0.55);
}

.game-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.game-card-title {
  font-weight: 700;
  font-size: 0.92rem;
}

.game-card-meta {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-family: var(--font-mono);
  font-size: 0.76rem;
  color: var(--muted);
}

.tag-pill {
  padding: 0.12rem 0.4rem;
  border-radius: 0.4rem;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
}

.tag-pill-live {
  background: var(--accent-soft);
  color: #6ee7b7;
}

.game-card-preview {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0;
}

.game-card-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.card-btn-import[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #a7f3d0;
}

.advanced-details {
  border: 1px solid var(--border);
  border-radius: 0.65rem;
  padding: 0.55rem 0.75rem;
  background: rgba(255, 255, 255, 0.02);
}

.advanced-details summary {
  cursor: pointer;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
}

.details-content {
  margin-top: 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.details-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.confirm-text {
  padding: 0.9rem 1rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}
