:root {
  --primary-color: #4e342e;   /* 木の駒のような焦げ茶 */
  --accent-color: #8d6e63;    /* 明るいウォールナット */
  --bg-color: #f8f5f2;        /* アイボリー */
  --card-bg: #ffffff;
  --input-bg: #fafafa;
  --border-color: #e0d8d5;
  --success-color: #1b8a5a;
  --success-bg: #e3f6ec;
  --fail-color: #c0392b;
  --fail-bg: #fdecea;
  --sand-color: #fffbf0;
  --sand-border: #ffe0b2;
}

* { box-sizing: border-box; }

body {
  font-family: 'Hiragino Kaku Gothic ProN', 'Meiryo', -apple-system, sans-serif;
  background: var(--bg-color);
  color: var(--primary-color);
  margin: 0;
  padding: 24px 16px 60px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

.app {
  max-width: 980px;
  margin: 0 auto;
}

.app-header {
  text-align: center;
  margin-bottom: 24px;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 6px;
}

.home-link {
  display: block;
  flex-shrink: 0;
  line-height: 0;
  border-radius: 10px;
  overflow: hidden;
  transition: transform 0.15s, box-shadow 0.15s;
}
.home-link:hover {
  transform: scale(1.05);
  box-shadow: 0 3px 10px rgba(74, 144, 226, 0.25);
}
.home-link:focus-visible {
  outline: 3px solid #4a90e2;
  outline-offset: 2px;
}
.home-icon {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: cover;
}

.brand-name {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', sans-serif;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: #1f2937;
  margin: 0;
}
.tagline {
  font-size: 0.9rem;
  color: #6d4c41;
  margin: 0;
}
.tagline a {
  color: var(--accent-color);
  font-weight: 700;
  text-decoration: underline;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 24px;
  align-items: start;
}

@media (max-width: 800px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* ---------- board column ---------- */
.board-column {
  text-align: center;
  min-width: 0;
}

.player-bar {
  max-width: 480px;
  margin: 0 auto;
  padding: 6px 4px;
  text-align: left;
}
.player-name {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--primary-color);
}

.app-loading {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.04em;
  text-shadow: 0 1px 3px rgba(20, 15, 12, 0.9);
  pointer-events: none;
}
.app-loading.hidden { display: none; }
.app-loading .loading-dot {
  opacity: 0;
  animation: engineLoadingDot 1.4s infinite;
}
.app-loading .loading-dot:nth-child(2) { animation-delay: 0.2s; }
.app-loading .loading-dot:nth-child(3) { animation-delay: 0.4s; }
.app-loading .loading-dot:nth-child(4) { animation-delay: 0.6s; }

.board-wrapper {
  position: relative;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
  /* Empty squares should let a vertical swipe scroll the page normally;
     pieces need the opposite (see .piece-417db below) so a drag isn't
     hijacked by the browser's own scroll handling. The JS touchstart/
     touchmove/touchend listeners in app.js are what actually enforce
     this per-gesture (touch-action alone can't be conditional), but
     this default still matters as the browser's own first-pass hint. */
  touch-action: pan-y;
}

/* Keep the loading text legible over the board while preserving the existing
   loading animation and the normal board appearance after initialization. */
.board-wrapper.is-loading::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(20, 15, 12, 0.28);
  pointer-events: none;
}

/* chessboard.js's own piece images use this class. Touching one should
   never scroll the page — it should always start a drag. */
.piece-417db {
  touch-action: none;
}

.board {
  width: 100%;
}

.annotation-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}
.annot-circle {
  fill: none;
  stroke: rgba(46, 160, 67, 0.9);
}
.annot-arrow {
  stroke: rgba(46, 160, 67, 0.9);
  stroke-linecap: round;
}
.annot-arrowhead {
  fill: rgba(46, 160, 67, 0.9);
}

.status-bar {
  margin-top: 18px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  font-weight: 600;
  font-size: 0.95rem;
  min-height: 1.2em;
}
.status-bar.hidden { display: none; }

.btn-tiny {
  padding: 6px 12px;
  font-size: 11px;
  font-weight: 700;
  background: #ffe0b2;
  color: #e65100;
  flex: none;
}
.btn-tiny:hover { background: #ffcc80; }

/* chessboard.js square highlight hooks for tap-to-move / premove */
#board .square-55d63.selected-square::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(141, 110, 99, 0.65);
  pointer-events: none;
}
#board .square-55d63.premove-square::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(230, 81, 0, 0.55);
  pointer-events: none;
}

/* legal-move hint markers, lichess-style: a filled dot on empty squares,
   a ring around the edge on squares with a capturable piece */
#board .square-55d63 .move-hint-dot {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 30%; height: 30%;
  border-radius: 50%;
  background: rgba(78, 52, 46, 0.45);
  pointer-events: none;
}
#board .square-55d63 .move-hint-ring {
  position: absolute;
  top: 4%; left: 4%; right: 4%; bottom: 4%;
  border-radius: 50%;
  box-shadow: inset 0 0 0 5px rgba(78, 52, 46, 0.45);
  pointer-events: none;
}

.result-banner {
  margin-top: 14px;
  padding: 16px;
  border-radius: 14px;
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0.03em;
}
.result-banner.success {
  background: var(--success-bg);
  color: var(--success-color);
  border: 2px solid var(--success-color);
}
.result-banner.fail {
  background: var(--fail-bg);
  color: var(--fail-color);
  border: 2px solid var(--fail-color);
}
.result-banner.hidden { display: none; }

/* Compact result banner overlaid on the bottom edge of the board itself
   — used on both mobile and desktop now, so the below-buttons banner is
   never shown (see the unconditional override just below). */
.result-banner-board {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px;
  text-align: center;
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.03em;
  z-index: 5;
}
.result-banner-board.success {
  background: var(--success-bg);
  color: var(--success-color);
  border-top: 2px solid var(--success-color);
}
.result-banner-board.fail {
  background: var(--fail-bg);
  color: var(--fail-color);
  border-top: 2px solid var(--fail-color);
}
.result-banner-board.hidden { display: none; }

#resultBanner { display: none !important; }

.promotion-dialog {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(20, 15, 12, 0.52);
}
.promotion-dialog.hidden { display: none; }
.promotion-card {
  width: min(440px, 100%);
  padding: 24px;
  border-radius: 16px;
  background: var(--card-bg);
  box-shadow: 0 12px 40px rgba(20, 15, 12, 0.25);
}
.promotion-card h2 { margin: 0 0 18px; color: var(--primary-color); }
.promotion-options { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.promotion-option { min-height: 48px; font-weight: 700; }

/* "Thinking..." overlay — shown only while White has already moved but
   the engine hasn't finished loading yet (see requestEngineMove() in
   app.js). It's really just a loading placeholder, not a genuine
   per-move "engine is thinking" indicator, so it only ever appears once
   per session, before the engine's very first move. */
.engine-loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 15, 12, 0.55);
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.03em;
  z-index: 6;
  pointer-events: none;
}
.engine-loading-overlay.hidden { display: none; }
.engine-loading-overlay .dot {
  opacity: 0;
  animation: engineLoadingDot 1.4s infinite;
}
.engine-loading-overlay .dot:nth-child(2) { animation-delay: 0.2s; }
.engine-loading-overlay .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes engineLoadingDot {
  0%, 20% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 0; }
}

.board-controls {
  margin-top: 20px;
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}
.board-controls .btn {
  flex: 1 1 150px;
}

/* ---------- practice history ---------- */
.history-layout { max-width: 980px; margin: 0 auto; padding: 24px; }
.history-toolbar { margin-bottom: 18px; }
.history-card { background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 12px; padding: 20px; margin-bottom: 18px; box-shadow: 0 4px 14px rgba(78, 52, 46, 0.06); }
.history-chart-card { position: relative; }
.history-card h2 { margin: 0 0 16px; color: var(--primary-color); }
.history-chart-shell { position: relative; background: var(--card-bg); }
.chart-fullscreen-btn { position: absolute; top: 12px; right: 12px; z-index: 1; width: 40px; min-width: 40px; height: 40px; min-height: 40px; padding: 8px; display: inline-flex; flex: none; align-items: center; justify-content: center; background: transparent; color: var(--primary-color); }
.chart-fullscreen-btn:hover { background: var(--input-bg); }
.fullscreen-icon { display: none; width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart-fullscreen-btn[data-active="true"] .fullscreen-icon-exit,
.chart-fullscreen-btn[data-active="false"] .fullscreen-icon-open,
.chart-fullscreen-btn:not([data-active]) .fullscreen-icon-open { display: block; }
.history-chart { overflow-x: auto; }
.history-chart-svg { display: block; width: 920px; max-width: none; height: auto; }
.chart-axis { stroke: var(--border-color); stroke-width: 1; }
.chart-gridline { stroke: var(--border-color); stroke-width: 1; stroke-dasharray: 3 4; opacity: 0.7; }
.chart-y-label { fill: #6d4c41; font-size: 11px; }
.chart-day { cursor: pointer; outline: none; }
.chart-day:focus rect,
.chart-day:hover rect,
.chart-day.is-selected rect { filter: brightness(0.88); }
.chart-detail { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-top: 8px; padding: 9px 12px; border: 1px solid var(--border-color); border-radius: 8px; background: var(--input-bg); color: #6d4c41; font-size: 0.9rem; }
.chart-detail strong { color: var(--primary-color); }
.chart-success { fill: var(--success-color); }
.chart-failure { fill: #bdb3af; }
.chart-label { fill: #6d4c41; font-size: 11px; }
.chart-legend { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 10px; color: #6d4c41; font-size: 0.9rem; }
.chart-legend i { display: inline-block; width: 12px; height: 12px; margin-right: 5px; vertical-align: -1px; border-radius: 2px; }
.legend-success { background: var(--success-color); }
.legend-failure { background: #bdb3af; }
.history-message, .empty-history { color: #6d4c41; }
.summary-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.summary-item { background: var(--input-bg); border: 1px solid var(--border-color); border-radius: 8px; padding: 14px; }
.summary-item span { display: block; color: #6d4c41; }
.summary-item strong { display: block; font-size: 2rem; color: var(--primary-color); margin-top: 5px; }
.attempt-list { display: grid; gap: 8px; }
.attempt-row { display: grid; grid-template-columns: minmax(110px, 1fr) minmax(160px, 1.5fr) 34px 82px; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border-color); }
.attempt-row:last-child { border-bottom: 0; }
.attempt-game { font-weight: 600; color: var(--primary-color); }
.attempt-position { margin-left: 6px; font-weight: 500; color: var(--accent-color); white-space: nowrap; }
.attempt-date { color: #6d4c41; font-size: 0.9rem; }
.attempt-result { font-size: 1.25rem; text-align: center; font-weight: 700; }
.attempt-result.success { color: var(--success-color); }
.attempt-result.failure { color: #8a7d78; }
.attempt-retry { text-decoration: none; text-align: center; }

.history-chart-shell:fullscreen,
.history-chart-shell.is-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  padding: 20px;
  background: var(--bg-color);
  overflow: auto;
}
.history-chart-shell:fullscreen .history-chart,
.history-chart-shell.is-fullscreen .history-chart { max-width: 1200px; margin: 0 auto; }
.history-chart-shell:fullscreen .history-chart-svg,
.history-chart-shell.is-fullscreen .history-chart-svg { width: 100%; max-width: 1200px; margin: 0 auto; }
.history-chart-shell:fullscreen h2,
.history-chart-shell.is-fullscreen h2 { padding-right: 56px; }

@media (max-width: 600px) {
  .history-layout { padding: 16px; }
  .summary-grid { grid-template-columns: 1fr; }
  .attempt-row { grid-template-columns: 1fr 28px 76px; }
  .attempt-date { grid-column: 1; font-size: 0.8rem; }
  .history-chart-shell:fullscreen,
  .history-chart-shell.is-fullscreen { padding: 12px; }
}

/* ---------- buttons ---------- */
.btn {
  padding: 14px 22px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  transition: 0.25s;
  letter-spacing: 0.03em;
  min-height: 44px;
  touch-action: manipulation;
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}
.btn-primary {
  background: linear-gradient(135deg, #8d6e63 0%, #4e342e 100%);
  color: #fff;
  box-shadow: 0 6px 15px rgba(78, 52, 46, 0.25);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(78,52,46,0.35); }
.btn-secondary {
  background: transparent;
  color: var(--primary-color);
  border: 1px solid var(--primary-color);
}
.btn-secondary:hover { background: var(--input-bg); }
.btn-small {
  padding: 10px 14px;
  font-size: 12px;
  font-weight: 600;
  flex: 1;
  background: #efebe9;
  color: var(--primary-color);
}
.btn-small:hover { background: #e0d8d5; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; }

/* ---------- side panel ---------- */
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel-card {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 10px 10px 30px #d9d6d3, -10px -10px 30px #ffffff;
}

.panel-card h2 {
  font-size: 1rem;
  margin: 0 0 10px;
  color: var(--primary-color);
}

.panel-desc {
  font-size: 0.78rem;
  color: #6d4c41;
  margin: 0 0 14px;
  line-height: 1.5;
}

.option-group {
  padding: 16px;
  background: var(--sand-color);
  border: 1px dashed var(--sand-border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}
.option-group label {
  cursor: pointer;
  font-size: 12.5px;
  color: #5d4037;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
input[type="checkbox"] {
  width: 18px; height: 18px;
  accent-color: var(--primary-color);
  cursor: pointer;
  flex: none;
}

.game-info {
  font-size: 0.85rem;
  color: #6d4c41;
  line-height: 1.5;
}

.option-group select {
  width: 100%;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--primary-color);
  background: #fff;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  cursor: pointer;
}

#fenDisplay {
  width: 100%;
  padding: 12px;
  font-size: 12px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  outline: none;
  text-align: center;
  background: var(--input-bg);
  font-family: 'Courier New', monospace;
  color: var(--primary-color);
  margin-bottom: 12px;
}

.btn-row { display: flex; gap: 8px; }
.btn-row + .btn-row { margin-top: 10px; }
.copy-msg {
  color: var(--accent-color);
  font-size: 11px;
  height: 16px;
  margin-top: 6px;
  font-weight: 700;
  opacity: 0;
  transition: 0.3s;
}
.copy-msg.show { opacity: 1; }

.pgn-box {
  margin-top: 16px;
  padding: 12px 14px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
}
.pgn-box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--primary-color);
}
.pgn-content {
  margin: 0;
  max-height: 140px;
  overflow-y: auto;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.6;
  color: #6d4c41;
  white-space: pre-wrap;
  word-break: break-word;
}

.rules-list {
  margin: 0;
  padding-left: 18px;
  font-size: 0.82rem;
  line-height: 1.7;
  color: #6d4c41;
}

.engine-status {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent-color);
}

.app-footer {
  text-align: center;
  margin-top: 30px;
  font-size: 0.72rem;
  color: #8d6e63;
}
.app-footer code {
  background: #fff;
  padding: 2px 6px;
  border-radius: 6px;
}

/* ---------- mobile responsive pass ---------- */
@media (max-width: 600px) {
  body {
    padding: 12px 8px 40px;
    font-size: 19px;
  }

  .brand-name {
    font-size: 1.5rem;
  }
  .app-header {
    margin-bottom: 16px;
  }
  .tagline {
    font-size: 1.15rem;
  }

  .layout {
    gap: 14px;
  }

  .board-wrapper {
    max-width: 100%;
  }

  .player-bar {
    max-width: 100%;
  }
  .player-name {
    font-size: 1.05rem;
  }
  .game-info {
    font-size: 1rem;
  }

  .status-bar {
    font-size: 1.2rem;
    padding: 14px 16px;
  }
  .result-banner-board {
    font-size: 1.1rem;
    padding: 12px;
  }

  .board-controls {
    gap: 8px;
  }
  .board-controls .btn {
    flex: 1 1 46%;
    padding: 15px 10px;
    font-size: 17px;
  }

  .side-panel {
    gap: 12px;
  }

  .panel-card {
    padding: 14px;
    border-radius: 16px;
  }
  .panel-card h2 {
    font-size: 1.4rem;
  }

  .option-group {
    padding: 12px;
  }
  .option-group label {
    font-size: 17px;
  }
  .option-group select {
    font-size: 16px;
    padding: 12px;
  }

  /* iOS Safari auto-zooms on focusing any input with font-size under
     16px — keep this one at 16px on phones to avoid that. */
  #fenDisplay {
    font-size: 17px;
    padding: 12px;
  }

  .btn-small {
    padding: 15px 10px;
    font-size: 17px;
  }
  .btn-tiny {
    padding: 11px 15px;
    font-size: 15px;
  }

  .copy-msg {
    font-size: 15px;
  }

  .pgn-box-header {
    font-size: 16px;
  }
  .pgn-content {
    max-height: 140px;
    font-size: 15px;
  }

  .rules-list {
    font-size: 1.05rem;
  }

  .engine-status {
    font-size: 1.1rem;
  }
}
