:root {
  --bg: #f3efe8;
  --surface: #fffdf9;
  --surface-2: #f8f3ea;
  --ink: #27211c;
  --muted: #746b62;
  --line: #d9cec0;
  --primary: #7b4029;
  --primary-dark: #5f2f1e;
  --accent: #c48742;
  --success: #226a45;
  --success-bg: #e8f5ed;
  --error: #9f2f2f;
  --error-bg: #fae9e7;
  --warning: #8a5a12;
  --board: #e8b96d;
  --board-dark: #b67d33;
  --selected: #fff3a7;
  --target: rgba(45, 111, 72, 0.28);
  --last: rgba(75, 102, 181, 0.23);
  --shadow: 0 8px 30px rgba(72, 48, 26, 0.09);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 0%, rgba(196, 135, 66, 0.12), transparent 28rem),
    var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, select, input { font: inherit; }
button { touch-action: manipulation; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: calc(0.8rem + var(--safe-top)) max(1rem, env(safe-area-inset-left)) 0.8rem max(1rem, env(safe-area-inset-right));
  color: #fff;
  background: rgba(45, 36, 29, 0.96);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(12px);
}

.app-header h1 { margin: 0; font-size: clamp(1.15rem, 4vw, 1.55rem); letter-spacing: 0.04em; }
.subtitle { margin: 0.15rem 0 0; font-size: 0.75rem; color: rgba(255,255,255,0.72); }

.network-badge {
  flex: 0 0 auto;
  padding: 0.35rem 0.65rem;
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 999px;
  font-size: 0.72rem;
  color: rgba(255,255,255,0.88);
}
.network-badge.offline { background: rgba(43, 122, 77, 0.55); }

main {
  width: min(1050px, 100%);
  margin: 0 auto;
  padding: 1rem max(0.8rem, env(safe-area-inset-left)) calc(2rem + var(--safe-bottom)) max(0.8rem, env(safe-area-inset-right));
}

.hidden { display: none !important; }

.card, .question-card, .result-panel {
  background: var(--surface);
  border: 1px solid rgba(155, 125, 92, 0.22);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.card { padding: 1.1rem; margin-bottom: 1rem; }
.card h2 { margin: 0 0 0.45rem; font-size: 1.05rem; }
.card p { margin: 0.25rem 0 0.85rem; color: var(--muted); line-height: 1.65; }
.card code { color: var(--primary); }

.notice-card {
  margin-bottom: 1rem;
  padding: 0.9rem 1rem;
  border-radius: 14px;
  color: #5d431b;
  background: #fff4d6;
  border: 1px solid #efd494;
}
.notice-card p { margin: 0.35rem 0 0; line-height: 1.5; }

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 1.3rem 0 0.7rem;
}
.section-heading h2 { margin: 0; font-size: 1.2rem; }
.section-heading p { margin: 0.2rem 0 0; color: var(--muted); font-size: 0.85rem; }

.button-row { display: flex; align-items: center; gap: 0.65rem; }
.button-row.wrap { flex-wrap: wrap; }
.button-row.centered { justify-content: center; }

.primary-button, .secondary-button, .danger-button, .text-button, .icon-button {
  border: 0;
  border-radius: 11px;
  min-height: 44px;
  padding: 0.7rem 1rem;
  cursor: pointer;
  font-weight: 700;
}
.primary-button { color: white; background: var(--primary); box-shadow: 0 4px 10px rgba(123,64,41,0.22); }
.primary-button:active { background: var(--primary-dark); transform: translateY(1px); }
.secondary-button { color: var(--ink); background: #eee6db; border: 1px solid #d9ccbd; }
.secondary-button:active { background: #e4d8c9; }
.danger-button { color: white; background: #9a3732; }
.text-button { color: var(--primary); background: transparent; }
.compact { min-height: 38px; padding: 0.5rem 0.75rem; font-size: 0.84rem; }
.icon-button { width: 42px; height: 42px; padding: 0; font-size: 1.4rem; color: var(--primary); background: var(--surface); border: 1px solid var(--line); }
button:disabled { opacity: 0.45; cursor: default; }

.status-message { min-height: 1.2em; margin: 0.7rem 0 0 !important; font-size: 0.85rem; }
.status-message.success { color: var(--success); }
.status-message.error { color: var(--error); }

.quiz-set-list { display: grid; gap: 0.85rem; }
.quiz-set-card { padding: 1rem; }
.quiz-set-card h3 { margin: 0; font-size: 1.03rem; }
.quiz-set-card .description { margin: 0.35rem 0 0.75rem; color: var(--muted); font-size: 0.87rem; line-height: 1.5; }
.quiz-set-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 0.8rem; }
.quiz-count { flex: 0 0 auto; padding: 0.3rem 0.55rem; border-radius: 999px; background: #eee5da; color: #5e5146; font-size: 0.75rem; font-weight: 700; }
.progress-line { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 0.7rem; margin: 0.7rem 0; }
.progress-track { height: 8px; overflow: hidden; border-radius: 999px; background: #e8dfd4; }
.progress-fill { height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, var(--primary), var(--accent)); }
.progress-label { color: var(--muted); font-size: 0.78rem; }
.set-stats { display: flex; gap: 0.8rem; flex-wrap: wrap; color: var(--muted); font-size: 0.8rem; margin-bottom: 0.8rem; }
.set-actions { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 0.55rem; }
.set-actions button { padding-inline: 0.45rem; font-size: 0.8rem; }
.set-footer { display: flex; justify-content: space-between; align-items: center; gap: 0.7rem; margin-top: 0.75rem; padding-top: 0.7rem; border-top: 1px solid #eee7de; }
.set-footer select { min-height: 38px; max-width: 72%; border-radius: 9px; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 0.4rem 0.55rem; }
.delete-set-button { color: var(--error); background: transparent; border: 0; padding: 0.4rem; cursor: pointer; }

.empty-state { text-align: center; padding: 2rem 1rem; }
.empty-icon {
  display: grid; place-items: center; width: 64px; height: 72px; margin: 0 auto 0.7rem;
  color: #3b2419; font-family: serif; font-size: 2rem; font-weight: 700;
  background: #e6b669; border: 2px solid #9e6a2d;
  clip-path: polygon(50% 0, 100% 18%, 90% 100%, 10% 100%, 0 18%);
}

.help-card summary { cursor: pointer; font-weight: 700; }
.help-card ol { padding-left: 1.4rem; line-height: 1.7; color: var(--muted); }

.quiz-toolbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.8rem;
}
.quiz-progress-wrap { min-width: 0; text-align: center; }
.quiz-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; font-size: 0.9rem; }
.quiz-progress-text { margin-top: 0.15rem; color: var(--muted); font-size: 0.75rem; }
.elapsed-time { min-width: 3.2rem; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.8rem; }

.quiz-layout { display: grid; gap: 1rem; }
.board-column { min-width: 0; }
.question-card { padding: 0.85rem 1rem; margin-bottom: 0.75rem; }
.question-meta-row { display: flex; align-items: center; gap: 0.55rem; min-height: 1.7rem; }
.pill { display: inline-flex; align-items: center; padding: 0.25rem 0.55rem; border-radius: 999px; color: #fff; background: var(--primary); font-size: 0.72rem; font-weight: 700; }
.question-tags { color: var(--muted); font-size: 0.72rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.question-prompt { margin: 0.55rem 0 0.2rem; font-weight: 700; }
.side-to-move { margin: 0; color: var(--muted); font-size: 0.82rem; }

.board-shell {
  width: min(100%, 520px);
  margin: 0 auto;
  padding: 5px;
  border-radius: 4px;
  background: linear-gradient(145deg, #7c4b1f, #b67a34);
  box-shadow: 0 7px 18px rgba(76, 45, 19, 0.2);
}

.shogi-board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1 / 1;
  border-top: 1px solid rgba(77, 45, 19, 0.84);
  border-left: 1px solid rgba(77, 45, 19, 0.84);
  background: var(--board);
  user-select: none;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border-right: 1px solid rgba(77, 45, 19, 0.78);
  border-bottom: 1px solid rgba(77, 45, 19, 0.78);
  background: linear-gradient(135deg, rgba(255,255,255,0.05), rgba(115,69,24,0.03));
  cursor: pointer;
}
.square.selected { background: var(--selected); }
.square.target::after {
  content: "";
  position: absolute;
  width: 36%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--target);
  pointer-events: none;
}
.square.capture-target::after { width: 72%; border-radius: 8px; background: transparent; border: 3px solid rgba(128, 42, 30, 0.4); }
.square.answer-highlight { box-shadow: inset 0 0 0 4px var(--last); }
.square-coordinate { position: absolute; right: 2px; bottom: 0; color: rgba(80,48,21,0.48); font-size: clamp(0.42rem, 1.5vw, 0.58rem); pointer-events: none; }

.piece {
  position: relative;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 78%;
  height: 83%;
  color: #24160d;
  font-family: "Yu Mincho", "Hiragino Mincho ProN", serif;
  font-size: clamp(1rem, 5.6vw, 2.1rem);
  font-weight: 800;
  line-height: 1;
  text-shadow: 0 1px rgba(255,255,255,0.35);
  background: linear-gradient(145deg, #f2cc83, #d99d4b);
  border: 1px solid rgba(91,52,18,0.56);
  clip-path: polygon(50% 0, 96% 18%, 88% 100%, 12% 100%, 4% 18%);
  filter: drop-shadow(0 1px 1px rgba(67,39,16,0.25));
  pointer-events: none;
}
.piece.opponent { transform: rotate(180deg); }
.piece.promoted { color: #9b211d; }

.hand-area {
  width: min(100%, 520px);
  min-height: 48px;
  margin: 0.45rem auto;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  padding: 0.4rem 0.5rem;
  border: 1px solid #d4c4b1;
  border-radius: 10px;
  background: rgba(255,253,249,0.78);
}
.opponent-hand { justify-content: flex-end; }
.player-hand { justify-content: flex-start; }
.hand-label { color: var(--muted); font-size: 0.72rem; margin-right: 0.25rem; }
.hand-piece {
  position: relative;
  display: grid;
  place-items: center;
  width: 38px;
  height: 42px;
  border: 1px solid #9e6c32;
  clip-path: polygon(50% 0, 98% 18%, 88% 100%, 12% 100%, 2% 18%);
  background: #e3af62;
  font-family: "Yu Mincho", "Hiragino Mincho ProN", serif;
  font-weight: 800;
  font-size: 1.2rem;
  cursor: pointer;
}
.hand-piece.opponent { transform: rotate(180deg); cursor: default; }
.hand-piece.selected { outline: 4px solid rgba(255, 208, 64, 0.75); outline-offset: 1px; }
.hand-count { position: absolute; right: -4px; bottom: -5px; display: grid; place-items: center; min-width: 19px; height: 19px; padding: 0 4px; border-radius: 999px; color: white; background: #463126; font: 700 0.68rem -apple-system, sans-serif; transform: none; }
.opponent-hand .hand-count { transform: rotate(180deg); }
.empty-hand-text { color: var(--muted); font-size: 0.78rem; }

.selection-message { min-height: 1.5em; margin: 0.6rem auto; text-align: center; color: var(--muted); font-size: 0.86rem; }

.result-panel { padding: 1rem; }
.result-header { padding: 0.8rem 1rem; border-radius: 12px; font-size: 1.15rem; font-weight: 800; text-align: center; }
.result-header.correct { color: var(--success); background: var(--success-bg); }
.result-header.incorrect { color: var(--error); background: var(--error-bg); }
.result-header.revealed { color: var(--warning); background: #fff3d7; }
.answer-grid { display: grid; gap: 0; margin: 0.85rem 0; border: 1px solid #e4dbd1; border-radius: 12px; overflow: hidden; }
.answer-grid > div { display: grid; grid-template-columns: 7.2rem 1fr; gap: 0.5rem; padding: 0.65rem 0.75rem; border-bottom: 1px solid #eee6dd; }
.answer-grid > div:last-child { border-bottom: 0; }
.answer-grid dt { color: var(--muted); font-size: 0.78rem; }
.answer-grid dd { margin: 0; font-weight: 700; }
.evaluation-box { padding: 0.75rem; border-radius: 10px; color: #5d4a39; background: var(--surface-2); line-height: 1.6; font-size: 0.86rem; }
.explanation-block, .pv-block { margin-top: 1rem; }
.explanation-block h3, .pv-block h3 { margin: 0 0 0.4rem; font-size: 0.93rem; }
.explanation-block p { margin: 0; color: #4f463e; line-height: 1.75; }
.pv-block ol { margin: 0; padding-left: 1.6rem; display: grid; gap: 0.25rem; color: #4f463e; }
.result-actions { justify-content: flex-end; margin-top: 1.1rem; }

.promotion-dialog, .confirm-dialog {
  max-width: min(92vw, 420px);
  border: 0;
  border-radius: 16px;
  padding: 1.2rem;
  color: var(--ink);
  background: var(--surface);
  box-shadow: 0 18px 60px rgba(0,0,0,0.26);
}
dialog::backdrop { background: rgba(31, 23, 17, 0.55); backdrop-filter: blur(3px); }
dialog h2 { margin-top: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.2rem + var(--safe-bottom));
  z-index: 100;
  transform: translateX(-50%);
  max-width: min(90vw, 560px);
  padding: 0.75rem 1rem;
  border-radius: 999px;
  color: white;
  background: rgba(42,34,28,0.93);
  box-shadow: 0 8px 30px rgba(0,0,0,0.25);
  font-size: 0.84rem;
  text-align: center;
}

@media (min-width: 820px) {
  .quiz-layout { grid-template-columns: minmax(430px, 520px) minmax(300px, 1fr); align-items: start; }
  .result-panel { position: sticky; top: calc(82px + var(--safe-top)); }
  .piece { font-size: clamp(1.4rem, 3.5vw, 2.1rem); }
}

@media (max-width: 480px) {
  main { padding-inline: 0.55rem; }
  .app-header { padding-inline: 0.8rem; }
  .subtitle { display: none; }
  .set-actions { grid-template-columns: 1fr; }
  .answer-grid > div { grid-template-columns: 6.4rem 1fr; }
  .result-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .primary-button, .secondary-button, .danger-button { padding-inline: 0.75rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
