:root {
  --bg: #f5f4f2;
  --card: #ffffff;
  --accent: #8b6914;
  --accent-light: #c9a84c;
  --text: #1a1a1a;
  --text-muted: #666;
  --border: #e0ddd8;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); min-height: 100dvh; }

/* ── App shell ── */
.app-header {
  background: var(--accent); color: white; padding: 0.875rem 1rem;
  display: flex; align-items: center; gap: 0.75rem;
  position: sticky; top: 0; z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.app-header h1 { font-size: 1.05rem; font-weight: 600; flex: 1; }
.btn-icon { background: none; border: none; color: white; font-size: 1.3rem; cursor: pointer; padding: 0.25rem; line-height: 1; }
.user-badge { font-size: 0.75rem; opacity: .8; white-space: nowrap; }

/* ── Step indicator ── */
.steps {
  display: flex; padding: 0.75rem 1rem 0; background: white;
  border-bottom: 1px solid var(--border); gap: 0;
}
.step { flex: 1; text-align: center; font-size: 0.68rem; color: var(--text-muted); padding-bottom: 0.6rem; position: relative; }
.step::after { content: ''; position: absolute; top: 9px; left: 50%; width: 100%; height: 2px; background: var(--border); }
.step:last-child::after { display: none; }
.step-dot {
  width: 20px; height: 20px; border-radius: 50%; background: var(--border);
  margin: 0 auto 5px; display: flex; align-items: center; justify-content: center;
  font-size: 0.65rem; font-weight: 700; position: relative; z-index: 1;
}
.step.active .step-dot { background: var(--accent); color: white; }
.step.done .step-dot { background: var(--success); color: white; content: '✓'; }
.step.active { color: var(--accent); font-weight: 600; }
.step.done { color: var(--success); }

/* ── Content ── */
.app-content { padding: 1rem; max-width: 560px; margin: 0 auto; padding-bottom: calc(80px + var(--safe-bottom)); }

/* ── Cards ── */
.card { background: var(--card); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1rem; box-shadow: 0 1px 4px rgba(0,0,0,.06); }
.card-title { font-weight: 600; margin-bottom: 1rem; font-size: 0.95rem; color: var(--accent); display: flex; align-items: center; gap: 0.5rem; }

/* ── Form ── */
.field { margin-bottom: 0.875rem; }
.field label { display: block; font-size: 0.82rem; font-weight: 500; color: var(--text-muted); margin-bottom: 0.35rem; }
.field input, .field select, .field textarea {
  width: 100%; padding: 0.75rem; border: 1.5px solid var(--border); border-radius: 8px;
  font-size: 1rem; background: white; color: var(--text); -webkit-appearance: none; appearance: none;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 90px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.field-small { margin-bottom: 0; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.75rem 1.25rem; border-radius: 8px; font-size: 0.95rem; font-weight: 500;
  border: none; cursor: pointer; transition: opacity .15s; text-decoration: none; white-space: nowrap;
}
.btn:active { opacity: .7; }
.btn-primary { background: var(--accent); color: white; }
.btn-outline { background: white; color: var(--accent); border: 1.5px solid var(--accent); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1.5px solid var(--border); }
.btn-danger { background: var(--danger); color: white; }
.btn-success { background: var(--success); color: white; }
.btn-full { width: 100%; }
.btn-lg { padding: 1rem 1.5rem; font-size: 1rem; border-radius: 10px; }
.btn:disabled { opacity: .45; pointer-events: none; }

/* ── Action bar (sticky bottom) ── */
.action-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
  background: white; border-top: 1px solid var(--border);
  display: flex; gap: 0.75rem; z-index: 50;
}

/* ── Login ── */
.login-wrap { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 1.5rem; background: var(--bg); }
.login-card { background: white; border-radius: var(--radius); padding: 2rem; width: 100%; max-width: 360px; text-align: center; box-shadow: 0 4px 24px rgba(0,0,0,.1); }
.login-icon { font-size: 3rem; margin-bottom: 0.5rem; }
.login-card h2 { font-size: 1.4rem; margin-bottom: 0.25rem; }
.login-card p { color: var(--text-muted); margin-bottom: 1.75rem; font-size: 0.9rem; }

/* ── Select wrapper with arrow ── */
.select-wrap { position: relative; }
.select-wrap select { padding-right: 2.5rem; }
.select-wrap::after { content: '▾'; position: absolute; right: 0.875rem; top: 50%; transform: translateY(-50%); pointer-events: none; color: var(--text-muted); }

/* ── Inline new-item form ── */
.new-item-form { background: var(--bg); border-radius: 8px; padding: 1rem; margin-top: 0.5rem; border: 1px dashed var(--border); }

/* ── Game list ── */
.game-item {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.875rem; background: var(--bg); border-radius: 8px; margin-bottom: 0.5rem;
}
.board-num {
  background: var(--accent); color: white; width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 0.85rem; font-weight: 700; flex-shrink: 0;
}
.game-names { flex: 1; }
.game-names .white-p { font-weight: 500; font-size: 0.9rem; }
.game-names .black-p { color: var(--text-muted); font-size: 0.82rem; margin-top: 1px; }
.result-badge { font-weight: 700; font-size: 0.9rem; padding: 0.2rem 0.6rem; border-radius: 5px; background: white; flex-shrink: 0; }
.result-badge.win { color: var(--success); }
.result-badge.draw { color: var(--text-muted); }
.result-badge.loss { color: var(--danger); }
.btn-del { background: none; border: none; color: var(--border); cursor: pointer; font-size: 1.1rem; padding: 0.25rem; flex-shrink: 0; }
.btn-del:active { color: var(--danger); }

/* ── Result selector ── */
.result-group { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.5rem; }
.result-opt {
  padding: 0.875rem 0.5rem; border-radius: 8px; border: 2px solid var(--border);
  background: white; font-size: 0.95rem; font-weight: 600; cursor: pointer; text-align: center;
  transition: border-color .1s, background .1s;
}
.result-opt.selected { border-color: var(--accent); background: var(--accent); color: white; }

/* ── OCR section ── */
.ocr-box {
  border: 1.5px dashed var(--border); border-radius: 8px; padding: 0.875rem;
  text-align: center; cursor: pointer; color: var(--text-muted); font-size: 0.9rem;
  transition: border-color .15s;
}
.ocr-box:active { border-color: var(--accent); }
.ocr-preview img { max-width: 100%; max-height: 200px; border-radius: 6px; margin-top: 0.5rem; }
.ocr-status { font-size: 0.85rem; margin-top: 0.5rem; color: var(--text-muted); }

/* ── Voice ── */
.voice-row { display: flex; gap: 0.75rem; align-items: flex-start; }
.voice-row textarea { flex: 1; }
.voice-btn {
  width: 48px; height: 48px; border-radius: 50%; border: none;
  background: var(--accent); color: white; font-size: 1.3rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0; margin-top: 0;
}
.voice-btn.recording { background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100%{box-shadow:0 0 0 0 rgba(220,38,38,.4)} 50%{box-shadow:0 0 0 8px rgba(220,38,38,0)} }

/* ── AI preview ── */
.ai-draft {
  background: var(--bg); border-radius: 8px; padding: 1rem;
  font-size: 0.9rem; line-height: 1.6; max-height: 280px; overflow-y: auto;
  border: 1px solid var(--border);
}
.ai-draft h1, .ai-draft h2, .ai-draft h3 { font-size: 1rem; margin: 0.75rem 0 0.25rem; }
.ai-draft p { margin-bottom: 0.5rem; }

/* ── Spinner ── */
.spinner { display: inline-block; width: 20px; height: 20px; border: 2.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Success ── */
.success-screen { text-align: center; padding: 2rem 1rem; }
.success-icon { font-size: 4.5rem; margin-bottom: 1rem; }
.success-screen h2 { font-size: 1.5rem; margin-bottom: 0.5rem; }
.success-screen p { color: var(--text-muted); margin-bottom: 1.5rem; line-height: 1.5; }
.success-detail { background: var(--bg); border-radius: 8px; padding: 1rem; text-align: left; margin-bottom: 1.5rem; font-size: 0.9rem; }
.success-detail .row { display: flex; justify-content: space-between; padding: 0.3rem 0; border-bottom: 1px solid var(--border); }
.success-detail .row:last-child { border: none; }

/* ── Misc ── */
.error-msg { color: var(--danger); font-size: 0.85rem; margin-top: 0.4rem; }
.hint { color: var(--text-muted); font-size: 0.82rem; margin-top: 0.3rem; }
.divider { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.score-display { font-size: 1.75rem; font-weight: 700; text-align: center; color: var(--accent); margin: 0.5rem 0; }
.tag { display: inline-block; padding: 0.2rem 0.6rem; border-radius: 4px; font-size: 0.75rem; font-weight: 500; }
.tag-draft { background: #fef9c3; color: #854d0e; }

/* ── Article blocks ── */
.block-label-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.35rem; }
.block-label-row label { font-size: 0.82rem; font-weight: 500; color: var(--text-muted); }
.voice-btn-sm {
  background: none; border: 1px solid var(--border); border-radius: 20px;
  padding: 0.2rem 0.55rem; cursor: pointer; font-size: 0.82rem;
  color: var(--text-muted); transition: all .2s; line-height: 1;
}
.voice-btn-sm:hover { border-color: var(--accent); color: var(--accent); }
.voice-btn-sm.recording { background: var(--danger); border-color: var(--danger); color: #fff; animation: pulse 1.2s infinite; }

/* ── Photo upload ── */
.photo-drop {
  border: 2px dashed var(--border); border-radius: 10px;
  padding: 1.25rem; text-align: center; cursor: pointer;
  transition: all .2s; margin-bottom: 0.75rem;
}
.photo-drop:hover, .photo-drop.dragover { border-color: var(--accent); background: rgba(139,105,20,0.04); }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(80px,1fr)); gap: 0.5rem; }
.photo-thumb { position: relative; border-radius: 8px; overflow: hidden; aspect-ratio: 1; }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-remove {
  position: absolute; top: 3px; right: 3px; background: rgba(0,0,0,.6);
  color: #fff; border: none; border-radius: 50%; width: 20px; height: 20px;
  font-size: 0.65rem; cursor: pointer; display: flex; align-items: center; justify-content: center;
}

/* ── Home menu ── */
.home-greeting { font-size: 1rem; color: var(--text-muted); margin-bottom: 1rem; }
.home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.home-card {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.35rem; padding: 1.25rem 0.75rem;
  background: var(--card); border-radius: var(--radius); box-shadow: 0 1px 4px rgba(0,0,0,.07);
  border: 2px solid transparent; cursor: pointer; text-align: center;
  transition: border-color .15s, box-shadow .15s; color: var(--text);
}
.home-card:hover { border-color: var(--accent); box-shadow: 0 4px 12px rgba(0,0,0,.1); }
.home-card-primary { border-color: var(--accent); background: linear-gradient(135deg, #fff9ec 0%, #fff 100%); }
.home-card-icon { font-size: 2rem; line-height: 1; }
.home-card-title { font-weight: 600; font-size: 0.95rem; color: var(--text); }
.home-card-desc { font-size: 0.76rem; color: var(--text-muted); }

/* ── Player home ── */
.tab-bar { display: flex; background: white; border-bottom: 1px solid var(--border); }
.tab-btn { flex: 1; padding: 0.75rem 0.5rem; border: none; background: none; font-size: 0.82rem; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; transition: all .15s; }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ── Game checkboxes ── */
.game-check-row {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.5rem 0; border-bottom: 1px solid var(--border);
  cursor: pointer; font-size: 0.875rem;
}
.game-check-row:last-child { border: none; }
.game-check-row input[type=checkbox] { width: 1rem; height: 1rem; accent-color: var(--accent); flex-shrink: 0; }
.board-num-sm { font-weight: 700; color: var(--accent); min-width: 1.2rem; text-align: center; font-size: 0.85rem; }
.game-check-names { flex: 1; color: var(--text); }
.result-badge-sm { font-size: 0.75rem; font-weight: 600; padding: 0.1rem 0.4rem; border-radius: 4px; }
.result-badge-sm.win { background: rgba(22,163,74,.12); color: #15803d; }
.result-badge-sm.draw { background: rgba(234,179,8,.12); color: #a16207; }
.result-badge-sm.loss { background: rgba(220,38,38,.12); color: #b91c1c; }

/* ── Training ── */
.training-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.65rem;
  margin-bottom: 1rem;
}
.training-stats.compact { margin-bottom: 0.75rem; }
.training-stat {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem 0.7rem;
  text-align: center;
}
.training-stat strong {
  display: block;
  font-size: 1.2rem;
  color: var(--accent);
  margin-bottom: 0.2rem;
}
.training-stat span {
  font-size: 0.74rem;
  color: var(--text-muted);
}
.training-section-label {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 0.95rem 0 0.55rem;
}
.training-recommended-grid,
.training-track-list {
  display: grid;
  gap: 0.65rem;
}
.training-stage-btn,
.training-stage-chip,
.training-pill {
  cursor: pointer;
}
.training-stage-btn {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(135deg, #fffef8 0%, #ffffff 100%);
  padding: 0.85rem 0.95rem;
  text-align: left;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.training-stage-title {
  font-weight: 700;
  color: var(--text);
}
.training-stage-meta {
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}
.training-pill-row,
.training-stage-list,
.training-tag-row,
.training-action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.training-pill,
.training-tag {
  border: 1px solid rgba(139,105,20,0.15);
  background: rgba(139,105,20,0.08);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.42rem 0.75rem;
  font-size: 0.78rem;
}
.training-tag-soft {
  background: rgba(0,0,0,0.04);
  color: var(--text-muted);
  border-color: rgba(0,0,0,0.06);
}
.training-track {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem;
  background: #fff;
}
.training-track-head {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.65rem;
}
.training-track-title {
  font-weight: 700;
  color: var(--text);
}
.training-track-summary {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}
.training-track-progress {
  font-weight: 700;
  color: var(--accent);
  font-size: 1.05rem;
}
.training-stage-chip {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 999px;
  padding: 0.4rem 0.7rem;
  font-size: 0.78rem;
}
.training-stage-chip small {
  color: var(--text-muted);
  margin-left: 0.3rem;
}
.training-hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.training-hero-meta span {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.32rem 0.6rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.training-board {
  display: grid;
  gap: 0;
  border: 10px solid #5b4630;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.14);
}
.training-board-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
}
.training-square {
  position: relative;
  aspect-ratio: 1;
  border: none;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.training-square.light { background: #f0d9b5; }
.training-square.dark { background: #b58863; }
.training-square.selected {
  box-shadow: inset 0 0 0 4px rgba(22, 163, 74, 0.7);
}
.training-piece {
  font-size: clamp(1.55rem, 5vw, 2.35rem);
  line-height: 1;
}
.training-square-label {
  position: absolute;
  left: 5px;
  bottom: 4px;
  font-size: 0.58rem;
  color: rgba(0, 0, 0, 0.48);
  font-weight: 700;
}
.training-board-footer {
  margin-top: 0.9rem;
  display: grid;
  gap: 0.45rem;
}
.training-selected {
  font-size: 0.82rem;
  color: var(--text-muted);
}
.training-status {
  padding: 0.7rem 0.8rem;
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 0.88rem;
  line-height: 1.45;
}
.training-status.success {
  background: rgba(22, 163, 74, 0.1);
  border-color: rgba(22, 163, 74, 0.25);
  color: #166534;
}

@media (max-width: 480px) {
  .training-stats {
    grid-template-columns: 1fr 1fr 1fr;
  }
  .training-stage-btn {
    flex-direction: column;
    align-items: flex-start;
  }
}
