:root {
  --bg: #120a04;
  --surface: #20140b;
  --surface-soft: #2b1b0f;
  --wood-dark: #2c1a0e;
  --wood-mid: #5c3317;
  --wood-light: #8b5e3c;
  --text-main: #fff4d5;
  --text-gold: #f5d78a;
  --text-dim: #c89850;
  --accent: #f0a820;
  --blue: #2d9bf0;
  --bead-cream: #f5e6c8;
  --bead-edge: #c8a96e;
  --line: rgba(232, 168, 50, 0.22);
  --bead-transition-ms: 240ms;
}

* { box-sizing: border-box; }

.hidden {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top, rgba(240, 168, 32, 0.12), transparent 34rem),
    var(--bg);
  color: var(--text-main);
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
}

button {
  font: inherit;
}

#appContainer {
  width: min(100%, 960px);
  min-height: 100vh;
  margin: 0 auto;
  position: relative;
}

.page {
  display: none;
  min-height: 100vh;
  padding: 18px;
}

.page.active {
  display: flex;
  flex-direction: column;
}

.title-wrap {
  min-height: calc(100vh - 36px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.title-logo {
  max-width: 760px;
  color: var(--text-gold);
  font-family: Georgia, serif;
  font-size: clamp(38px, 8vw, 76px);
  font-weight: 700;
  line-height: 1.02;
  letter-spacing: 0;
  text-shadow: 0 0 30px rgba(232, 200, 120, 0.35);
}

.title-logo span,
.app-title span {
  color: var(--accent);
}

.title-sub {
  margin: 16px 0 34px;
  color: var(--text-dim);
  font-size: 16px;
  line-height: 1.7;
}

.title-abacus-deco {
  width: min(320px, 86vw);
  height: 62px;
  margin-bottom: 36px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border: 2px solid var(--wood-light);
  border-radius: 8px;
  background: linear-gradient(160deg, var(--wood-mid), var(--wood-dark));
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.deco-bead {
  width: 38px;
  height: 22px;
  border: 1px solid var(--bead-edge);
  border-radius: 12px;
  background: linear-gradient(160deg, #fff8e8, var(--bead-cream), #d4b07a);
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.title-version {
  margin-top: 18px;
  color: rgba(200, 152, 80, 0.72);
  font-size: 12px;
}

.title-copyright {
  margin-top: 6px;
  color: rgba(200, 152, 80, 0.62);
  font-size: 12px;
}

.page-header,
.trainer-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0 16px;
  border-bottom: 1px solid var(--line);
}

.page-header h1 {
  margin: 0;
  color: var(--text-gold);
  font-size: 20px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(232, 168, 50, 0.08);
  color: var(--text-gold);
  cursor: pointer;
}

.select-list {
  width: min(100%, 560px);
  margin: 24px auto 0;
  display: grid;
  gap: 12px;
}

.select-card {
  min-height: 84px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-main);
  cursor: pointer;
}

.select-card:not(.disabled):hover {
  border-color: rgba(240, 168, 32, 0.55);
  background: rgba(240, 168, 32, 0.1);
}

.select-card.disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.select-name {
  color: var(--text-gold);
  font-size: 18px;
  font-weight: 700;
}

.select-desc {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 13px;
}

.screen {
  width: 100%;
}

.trainer-header {
  justify-content: flex-start;
}

.app-title {
  color: var(--text-gold);
  font-family: Georgia, serif;
  font-size: 20px;
  font-weight: 700;
}

.app-subtitle {
  margin-top: 2px;
  color: var(--text-dim);
  font-size: 12px;
}

.trainer-layout {
  width: 100%;
  margin: 12px auto 0;
  display: grid;
  grid-template-columns: minmax(280px, 330px) minmax(420px, 1fr);
  gap: 16px;
  align-items: start;
}

.problem-panel,
.abacus-panel,
.controls-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
}

.problem-panel {
  padding: 12px 14px;
}

.problem-panel.test-mode {
  display: grid;
  gap: 7px;
}

.abacus-panel {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-title,
.control-label {
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
}

.problem-settings {
  margin-top: 8px;
  padding: 10px;
  display: grid;
  gap: 8px;
  border: 1px solid rgba(232, 168, 50, 0.15);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.16);
}

.settings-title {
  color: var(--text-gold);
  font-size: 13px;
  font-weight: 700;
}

.setting-field {
  display: grid;
  grid-template-columns: 54px 1fr;
  gap: 8px;
  align-items: center;
  color: var(--text-dim);
  font-size: 13px;
}

.setting-field select {
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #1a1009;
  color: var(--text-main);
  font: inherit;
}

.problem-list {
  margin: 10px 0 6px;
  padding: 0;
  list-style: none;
  border-top: 1px solid rgba(232, 168, 50, 0.15);
}

.problem-row {
  display: grid;
  grid-template-columns: 34px 1fr;
  align-items: center;
  min-height: 34px;
  border-bottom: 1px solid rgba(232, 168, 50, 0.15);
  color: var(--text-main);
}

.problem-index {
  color: var(--text-dim);
  font-size: 13px;
}

.problem-number {
  text-align: right;
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.problem-row.active {
  margin: 0 -8px;
  padding: 0 8px;
  border-radius: 6px;
  background: linear-gradient(90deg, rgba(45, 155, 240, 0.28), rgba(240, 168, 32, 0.18));
  box-shadow: inset 3px 0 0 var(--blue);
}

.problem-row.done {
  color: rgba(255, 244, 213, 0.58);
}

.answer-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding-top: 6px;
  color: var(--text-dim);
  font-size: 14px;
}

.answer-row strong {
  color: var(--accent);
  font-size: 24px;
  font-variant-numeric: tabular-nums;
}

.status-line {
  min-height: 34px;
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.6;
}

.training-answer {
  margin-top: 8px;
  padding-top: 8px;
  display: grid;
  gap: 8px;
  border-top: 1px solid rgba(232, 168, 50, 0.15);
}

.test-meta {
  margin-top: 10px;
  padding: 8px 10px;
  display: grid;
  gap: 4px;
  border: 1px solid rgba(232, 168, 50, 0.18);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.16);
  color: var(--text-dim);
  font-size: 13px;
}

.test-meta-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.test-meta strong {
  color: var(--accent);
  font-size: 22px;
  font-variant-numeric: tabular-nums;
}

.test-condition {
  color: var(--text-gold);
  font-size: 12px;
  line-height: 1.3;
}

.test-answer {
  margin-top: 8px;
  padding-top: 8px;
  display: grid;
  gap: 8px;
  border-top: 1px solid rgba(232, 168, 50, 0.15);
}

.problem-panel.test-mode .panel-title {
  display: none;
}

.problem-panel.test-mode .test-meta,
.problem-panel.test-mode .problem-list,
.problem-panel.test-mode .test-answer,
.problem-panel.test-mode .status-line {
  margin-top: 0;
}

.problem-panel.test-mode .test-meta {
  order: 1;
}

.problem-panel.test-mode .test-answer {
  order: 3;
  grid-template-columns: minmax(0, 1fr) 92px;
  align-items: end;
  padding-top: 7px;
  border-top: 2px solid rgba(232, 168, 50, 0.35);
}

.problem-panel.test-mode .problem-list {
  order: 2;
}

.problem-panel.test-mode .status-line {
  order: 4;
  min-height: 24px;
  font-size: 12px;
}

.problem-panel.test-mode .problem-row {
  min-height: 29px;
}

.problem-panel.test-mode .problem-number {
  font-size: 21px;
  line-height: 1.05;
}

.problem-panel.test-mode .answer-input-field {
  gap: 3px;
}

.problem-panel.test-mode .answer-input-field input {
  min-height: 36px;
  font-size: 19px;
}

.test-result {
  margin-top: 10px;
  padding-top: 10px;
  display: grid;
  gap: 10px;
  border-top: 1px solid rgba(232, 168, 50, 0.15);
}

.result-title {
  color: var(--text-gold);
  font-size: 18px;
  font-weight: 700;
}

.result-summary {
  display: grid;
  gap: 4px;
  color: var(--text-main);
  font-size: 14px;
  line-height: 1.6;
}

.result-list {
  max-height: 220px;
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: auto;
  border-top: 1px solid rgba(232, 168, 50, 0.15);
}

.result-list li {
  padding: 7px 0;
  border-bottom: 1px solid rgba(232, 168, 50, 0.12);
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.45;
}

.result-list .correct {
  color: #89e08f;
}

.result-list .incorrect {
  color: #ff9b83;
}

.result-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.answer-input-field {
  display: grid;
  gap: 6px;
  color: var(--text-dim);
  font-size: 13px;
}

.answer-input-field input {
  width: 100%;
  min-height: 38px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #1a1009;
  color: var(--text-main);
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.answer-result {
  min-height: 24px;
  color: var(--text-gold);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.5;
}

.answer-result.correct {
  color: #89e08f;
}

.answer-result.incorrect {
  color: #ff9b83;
}

.controls-panel {
  margin-top: 2px;
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
}

.control-group {
  display: grid;
  gap: 6px;
  min-width: 150px;
  flex: 1 1 170px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.control-label span {
  color: var(--text-gold);
  font-variant-numeric: tabular-nums;
}

.speed-slider {
  width: 100%;
  height: 4px;
  appearance: none;
  -webkit-appearance: none;
  border-radius: 999px;
  background: rgba(232, 168, 50, 0.22);
  outline: none;
}

.speed-slider::-webkit-slider-thumb {
  width: 18px;
  height: 18px;
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.speed-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.speed-scale {
  display: flex;
  justify-content: space-between;
  color: var(--text-dim);
  font-size: 11px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.segmented.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.segmented button,
.secondary-btn,
.primary-btn {
  min-height: 36px;
  border-radius: 8px;
  cursor: pointer;
}

.segmented button,
.secondary-btn {
  border: 1px solid var(--line);
  background: rgba(232, 168, 50, 0.08);
  color: var(--text-dim);
}

.segmented button.active,
.secondary-btn:hover {
  border-color: var(--accent);
  background: rgba(232, 168, 50, 0.2);
  color: var(--text-gold);
}

.primary-btn:disabled,
.secondary-btn:disabled,
.segmented button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  box-shadow: none;
}

.primary-btn {
  min-width: 0;
  padding: 0 22px;
  border: 0;
  background: linear-gradient(135deg, #e8a020, #c87010);
  color: #1c1008;
  font-weight: 700;
  box-shadow: 0 6px 20px rgba(232, 160, 32, 0.32);
}

.compact,
.wide {
  width: 100%;
  min-width: 0;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  flex: 1 1 220px;
}

.controls-panel > .wide {
  width: auto;
  flex: 1 1 160px;
}

.value-display {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.digits-row {
  display: flex;
  gap: 4px;
  align-items: flex-end;
}

.digit-box {
  width: 48px;
  text-align: center;
}

.digit-label {
  margin-bottom: 2px;
  color: var(--text-dim);
  font-size: 9px;
}

.digit-value {
  color: var(--text-gold);
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  transition: color 0.15s, transform 0.15s;
}

.digit-value.pop {
  color: #ffd700;
  transform: scale(1.15);
}

.total-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.total-label {
  color: var(--text-dim);
  font-size: 12px;
}

.total-value {
  min-width: 72px;
  color: var(--accent);
  font-size: 26px;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.total-hidden .total-value {
  color: var(--text-dim);
}

.abacus-wrap {
  width: min(100%, 500px);
  margin: 8px auto 0;
}

.abacus-frame {
  width: 100%;
  padding: 8px 6px;
  border: 2px solid var(--wood-light);
  border-radius: 8px;
  background: linear-gradient(160deg, var(--wood-mid), var(--wood-dark));
  box-shadow: inset 0 2px 6px rgba(255,255,255,0.08), 0 8px 24px rgba(0,0,0,0.46);
}

.abacus-bar {
  width: 100%;
  height: 12px;
  border-radius: 4px;
  background: linear-gradient(180deg, var(--wood-light), var(--wood-mid));
}

.abacus-columns {
  position: relative;
  display: flex;
  justify-content: space-around;
  padding: 0 2px;
}

.beam {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 10;
  pointer-events: none;
  background: linear-gradient(180deg, var(--wood-light), var(--wood-mid), var(--wood-dark));
  border-top: 1px solid rgba(255,255,255,0.15);
  border-bottom: 1px solid rgba(0,0,0,0.4);
}

.col-wrap {
  width: 46px;
  position: relative;
}

.col-wrap::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  z-index: 1;
  width: 5px;
  transform: translateX(-50%);
  border-radius: 2px;
  background: linear-gradient(90deg, #0a0604, #2a1a0e, #0a0604);
}

.bead {
  position: absolute;
  left: 50%;
  z-index: 3;
  width: 46px;
  height: 26px;
  transform: translateX(-50%);
  border: 1px solid var(--bead-edge);
  border-radius: 14px;
  background: linear-gradient(160deg, #fff8e8 0%, var(--bead-cream) 45%, #d4b07a 100%);
  box-shadow: 0 2px 4px rgba(0,0,0,0.35), inset 0 1px 3px rgba(255,255,255,0.55);
  cursor: pointer;
  user-select: none;
  transition: top var(--bead-transition-ms, 180ms) cubic-bezier(0.25,0.46,0.45,0.94), background 0.15s;
}

.bead::after {
  content: "";
  position: absolute;
  top: 5px;
  left: 8px;
  right: 8px;
  height: 6px;
  border-radius: 3px;
  background: rgba(255,255,255,0.38);
  pointer-events: none;
}

.bead.hl-add {
  border-color: #2196f3;
  background: linear-gradient(160deg, #80cfff 0%, #2196f3 45%, #0d6ebd 100%);
  animation: hl-add 0.7s ease-in-out infinite alternate;
}

.bead.hl-sub {
  border-color: #ff4444;
  background: linear-gradient(160deg, #ff9080 0%, #ff4444 45%, #cc1100 100%);
  animation: hl-sub 0.7s ease-in-out infinite alternate;
}

@keyframes hl-add {
  from { box-shadow: 0 0 8px rgba(33,150,243,0.5); }
  to { box-shadow: 0 0 22px rgba(33,150,243,1); }
}

@keyframes hl-sub {
  from { box-shadow: 0 0 8px rgba(255,68,68,0.5); }
  to { box-shadow: 0 0 22px rgba(255,68,68,1); }
}

@media (max-width: 760px) {
  .page {
    padding: 12px;
  }

  .trainer-layout {
    grid-template-columns: 1fr;
  }

  .problem-panel {
    order: 1;
  }

  .abacus-panel {
    order: 2;
  }

  .controls-panel {
    flex-direction: column;
    align-items: stretch;
  }
}
