/* ==========================================================================
   ChessHeart v3 — iOS Dark Mode Mobile-First Design System
   ========================================================================== */
:root {
  --bg: #000000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --surface-3: #3a3a3c;
  --separator: rgba(84, 84, 88, 0.55);
  --text: #ffffff;
  --text-secondary: rgba(235, 235, 245, 0.65);
  --text-tertiary: rgba(235, 235, 245, 0.38);

  --heart: #ff2d55;
  --heart-soft: rgba(255, 45, 85, 0.16);
  --green: #30d158;
  --green-soft: rgba(48, 209, 88, 0.16);
  --cyan: #32d2f2;
  --cyan-soft: rgba(50, 210, 242, 0.18);
  --amber: #ff9f0a;
  --amber-soft: rgba(255, 159, 10, 0.16);
  --orange: #ff6b2c;
  --blue: #0a84ff;
  --blue-soft: rgba(10, 132, 255, 0.16);

  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --sq-select: rgba(255, 214, 10, 0.62);
  --sq-last: rgba(255, 214, 10, 0.36);

  --radius-card: 18px;
  --tabbar-h: 64px;
}

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

html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

/* ---------- iOS Lock Screen ---------- */
.lock-screen {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: radial-gradient(circle at top, #1f1018 0%, #000000 62%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.lock-screen.hidden { display: none; }

.lock-card {
  width: 100%;
  max-width: 340px;
  background: rgba(28, 28, 30, 0.92);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-radius: 24px;
  padding: 28px 22px;
  border: 0.5px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.lock-logo {
  width: 60px;
  height: 60px;
  border-radius: 16px;
  background: linear-gradient(145deg, #ff2d55, #b8143b);
  display: grid;
  place-items: center;
  margin: 0 auto 14px;
  box-shadow: 0 8px 24px rgba(255, 45, 85, 0.38);
}

.lock-logo svg { width: 32px; height: 32px; fill: #fff; }
.lock-title { font-size: 1.4rem; font-weight: 800; letter-spacing: -0.02em; }
.lock-sub { font-size: 0.84rem; color: var(--text-secondary); margin: 4px 0 18px; }
.lock-err { color: var(--heart); font-size: 0.82rem; font-weight: 600; min-height: 20px; margin-top: 8px; }

/* ---------- iOS Header ---------- */
.ios-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(18, 18, 20, 0.84);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
}

.ios-header-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand-icon {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(145deg, #ff2d55, #c8103c);
  display: grid; place-items: center; flex-shrink: 0;
}
.brand-icon svg { width: 18px; height: 18px; fill: #fff; }
.brand-title { font-size: 1rem; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.brand-sub { font-size: 0.7rem; color: var(--text-secondary); }

.desktop-nav { display: none; gap: 6px; }
.desktop-pill {
  background: transparent; border: none; color: var(--text-secondary);
  padding: 7px 13px; border-radius: 10px; font-weight: 600; font-size: 0.85rem; cursor: pointer;
}
.desktop-pill.active { background: var(--heart); color: #fff; }

/* ---------- Main Container & Bottom Tab Bar ---------- */
.app-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 22px);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: iosFade 0.2s ease; }

@keyframes iosFade {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}

.ios-page-title { font-size: 1.65rem; font-weight: 800; letter-spacing: -0.03em; margin-bottom: 12px; }
.section-label {
  font-size: 0.75rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--text-secondary); margin: 14px 4px 6px;
}

.ios-tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(22, 22, 24, 0.88);
  backdrop-filter: saturate(180%) blur(22px);
  -webkit-backdrop-filter: saturate(180%) blur(22px);
  border-top: 0.5px solid var(--separator);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

.tabbar-btn {
  background: transparent; border: none; color: var(--text-secondary);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; font-size: 0.66rem; font-weight: 600; cursor: pointer; min-height: 48px;
}
.tabbar-btn svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.85; stroke-linecap: round; stroke-linejoin: round; }
.tabbar-btn.active { color: var(--heart); }

/* ---------- Responsive Grids ---------- */
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid-board { display: grid; grid-template-columns: 1fr; gap: 14px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

@media (min-width: 900px) {
  .desktop-nav { display: flex; }
  .ios-tabbar { display: none; }
  .app-main { padding: 20px 24px 40px; }
  .grid-board { grid-template-columns: minmax(340px, 450px) 1fr; gap: 22px; align-items: start; }
}

/* ---------- iOS Cards & Grouped Lists ---------- */
.card {
  background: var(--surface);
  border-radius: var(--radius-card);
  padding: 15px;
  border: 0.5px solid rgba(255, 255, 255, 0.06);
}
.card h2 { font-size: 1rem; font-weight: 700; margin-bottom: 6px; }
.muted { color: var(--text-secondary); font-size: 0.84rem; }

.ios-group {
  background: var(--surface);
  border-radius: var(--radius-card);
  overflow: hidden;
  border: 0.5px solid rgba(255, 255, 255, 0.07);
}

.ios-row {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 15px; background: transparent; border: none;
  border-bottom: 0.5px solid var(--separator);
  cursor: pointer; text-align: left; width: 100%; color: var(--text); font-family: inherit;
}
.ios-row:last-child { border-bottom: none; }
.ios-row:active { background: var(--surface-2); }

.ios-row-num {
  width: 30px; height: 30px; border-radius: 50%;
  border: 1.5px solid var(--separator); color: var(--text-secondary);
  font-weight: 700; font-size: 0.82rem; display: grid; place-items: center; flex-shrink: 0;
}
.ios-row.done .ios-row-num { background: var(--green); border-color: var(--green); color: #00240d; }
.ios-row.current .ios-row-num { background: var(--heart); border-color: var(--heart); color: #fff; }
.ios-row-body { flex: 1; min-width: 0; }
.ios-row-title { font-size: 0.94rem; font-weight: 600; line-height: 1.25; }
.ios-row-sub { font-size: 0.78rem; color: var(--text-secondary); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ios-row-meta { font-size: 0.8rem; font-weight: 600; color: var(--text-secondary); flex-shrink: 0; }
.ios-row.done .ios-row-meta { color: var(--green); }
.ios-row-chevron { color: var(--text-tertiary); font-size: 1.2rem; flex-shrink: 0; }

/* ---------- Segmented Control, Buttons, Inputs ---------- */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--surface-2); padding: 3px; border-radius: 11px; gap: 3px;
}
.seg-btn {
  background: transparent; border: none; color: var(--text-secondary);
  font-family: inherit; font-size: 0.78rem; font-weight: 600;
  padding: 7px 6px; border-radius: 8px; min-height: 34px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.seg-btn.active { background: #48484a; color: #fff; box-shadow: 0 2px 6px rgba(0,0,0,0.35); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 15px; border-radius: 12px; border: none;
  background: var(--surface-2); color: var(--text); font-family: inherit;
  font-weight: 600; font-size: 0.9rem; cursor: pointer; touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--heart); color: #fff; }
.btn-green { background: var(--green); color: #00240d; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 0.82rem; border-radius: 10px; }
.btn-icon { min-width: 40px; padding: 0 10px; }

input, select, textarea {
  background: var(--surface-2); border: 0.5px solid var(--separator);
  color: var(--text); border-radius: 12px; padding: 10px 12px;
  font-family: inherit; font-size: 16px; min-height: 44px;
}
textarea { width: 100%; min-height: 78px; resize: vertical; }

/* ---------- Progress Bar & Badges ---------- */
.progress-bar { height: 7px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--heart); border-radius: 99px; transition: width 0.3s ease; }
.progress-fill.green { background: var(--green); }

.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 99px; font-size: 0.72rem; font-weight: 700; }
.badge-red { background: var(--heart-soft); color: #ff6984; }
.badge-green { background: var(--green-soft); color: #4ade80; }
.badge-amber { background: var(--amber-soft); color: #ffb84d; }
.badge-blue { background: var(--blue-soft); color: #5ac8fa; }
.badge-cyan { background: var(--cyan-soft); color: #32d2f2; }

/* ---------- Chessboard ---------- */
.board-shell { width: 100%; max-width: 440px; margin: 0 auto; }
.chessboard {
  position: relative; width: 100%; aspect-ratio: 1 / 1;
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  border-radius: 14px; overflow: hidden; user-select: none; touch-action: manipulation;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
}
.sq { position: relative; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq.selected { background: var(--sq-select) !important; }
.sq.last-move::after { content: ''; position: absolute; inset: 0; background: var(--sq-last); pointer-events: none; }
.sq.danger { box-shadow: inset 0 0 0 3.5px var(--heart); animation: pulseDanger 1.2s infinite; }
.sq.hint-from { box-shadow: inset 0 0 0 3.5px var(--green); }
.sq.hint-to { box-shadow: inset 0 0 0 3.5px var(--green); background: rgba(48, 209, 88, 0.35) !important; }
.sq.flash-ok { background: rgba(48, 209, 88, 0.65) !important; }
.sq.flash-err { background: rgba(255, 45, 85, 0.65) !important; }
.sq.guide-lane { box-shadow: inset 0 0 0 2.5px rgba(10, 132, 255, 0.9); filter: brightness(1.1); }
.sq.guide-lane::before { content: ''; position: absolute; inset: 0; background: rgba(10, 132, 255, 0.24); pointer-events: none; }

@keyframes pulseDanger {
  0%, 100% { box-shadow: inset 0 0 0 3.5px rgba(255, 45, 85, 0.95); }
  50% { box-shadow: inset 0 0 0 7px rgba(255, 45, 85, 0.45); }
}

.sq-coord { position: absolute; font-size: 10px; font-weight: 800; pointer-events: none; z-index: 1; }
.sq.light .sq-coord { color: #8c6239; }
.sq.dark .sq-coord { color: #f4e1c3; }
.coord-rank { top: 2px; left: 3px; }
.coord-file { bottom: 1px; right: 3px; }
.piece-img { width: 88%; height: 88%; object-fit: contain; z-index: 2; pointer-events: none; display: block; }
.legal-dot { width: 28%; height: 28%; border-radius: 50%; background: rgba(0, 0, 0, 0.25); pointer-events: none; z-index: 3; }
.legal-ring { position: absolute; inset: 5%; border-radius: 50%; border: 3.5px solid rgba(0, 0, 0, 0.26); pointer-events: none; z-index: 3; }
.board-arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

/* ---------- Eval Bar & Analysis ---------- */
.eval-bar-wrap {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border-radius: 12px; padding: 8px 12px;
}
.eval-track { flex: 1; height: 10px; background: #222; border-radius: 99px; overflow: hidden; position: relative; }
.eval-white { height: 100%; background: #f2f2f7; transition: width 0.25s ease; }
.eval-score { font-size: 0.82rem; font-weight: 800; min-width: 46px; text-align: right; }

.analysis-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px;
}
.an-pill {
  background: var(--surface-2); border-radius: 10px; padding: 8px; text-align: center; font-size: 0.76rem;
}
.an-pill strong { display: block; font-size: 1.05rem; }

/* ---------- Coach Callout, Moves, Lists ---------- */
.coach-box {
  background: var(--surface-2); border-left: 4px solid var(--heart);
  padding: 11px 13px; border-radius: 12px; font-size: 0.9rem;
}
.coach-box.ok { border-left-color: var(--green); background: var(--green-soft); }
.coach-box.warn { border-left-color: var(--amber); background: var(--amber-soft); }

.move-list {
  display: flex; flex-wrap: wrap; gap: 5px; max-height: 150px; overflow-y: auto;
  padding: 8px; background: #121214; border-radius: 12px;
}
.move-chip { padding: 4px 8px; border-radius: 8px; font-size: 0.8rem; background: var(--surface-2); color: var(--text); cursor: pointer; }
.move-chip.active { background: var(--heart); color: #fff; font-weight: 700; }
.move-chip.tag-brilliant { border: 1px solid var(--cyan); color: var(--cyan); }
.move-chip.tag-great { border: 1px solid var(--blue); color: #60a5fa; }
.move-chip.tag-inaccuracy { border: 1px solid var(--amber); }
.move-chip.tag-mistake { border: 1px solid var(--orange); color: var(--orange); }
.move-chip.tag-blunder { border: 1px solid var(--heart); color: #ff6984; }

.game-row {
  padding: 11px 13px; border-radius: 13px; background: var(--surface-2);
  cursor: pointer; margin-bottom: 7px; border: 1.5px solid transparent;
}
.game-row.active { border-color: var(--heart); background: var(--heart-soft); }

.scrollable { max-height: 420px; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 14px);
  background: rgba(36, 36, 40, 0.96); border: 1px solid var(--heart);
  color: #fff; padding: 9px 15px; border-radius: 99px; font-size: 0.84rem; font-weight: 600;
  z-index: 99; display: none; max-width: 92vw; text-align: center;
}
.toast.show { display: block; animation: iosFade 0.18s ease; }

@media (min-width: 900px) { .toast { bottom: 24px; } }
