:root {
  --accent: #ff8c42;
  --accent-dim: #ffd9b3;
  --ink: #222;
  --gray: #ccc;
  --good: #4caf50;
  --bad: #e05555;
  --circle-size: clamp(16px, 5.5vmin, 32px);
  --circle-gap: clamp(4px, 1vmin, 7px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html, body {
  height: 100%;
  margin: 0;
  background: #ffffff;
  color: var(--ink);
  font-family: "M PLUS Rounded 1c", "Hiragino Maru Gothic ProN", "Hiragino Sans", "Yu Gothic", sans-serif;
  overscroll-behavior: none;
  overflow-x: hidden;
}

.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(24px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  gap: 24px;
}

.screen.active {
  display: flex;
}

/* ---------- start screen ---------- */

.title {
  font-size: clamp(2rem, 8vw, 3.5rem);
  text-align: center;
  line-height: 1.3;
  margin: 0;
}

.big-button {
  font-size: clamp(1.5rem, 6vw, 2.2rem);
  padding: 0.8em 2em;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-family: inherit;
  font-weight: bold;
}

.big-button:active {
  transform: scale(0.96);
}

.set-size-picker {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 16px;
}

.picker-label {
  font-size: 1rem;
  color: #888;
}

.picker-options {
  display: flex;
  gap: 10px;
}

.picker-option {
  font-size: 1.2rem;
  font-family: inherit;
  padding: 0.5em 1.1em;
  border-radius: 999px;
  border: 2px solid var(--gray);
  background: white;
  color: var(--ink);
  cursor: pointer;
}

.picker-option.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.streak-badge {
  font-size: clamp(1rem, 2.5vmin, 1.3rem);
  color: var(--accent);
  font-weight: bold;
  min-height: 1.4em;
}

.calendar-strip {
  display: flex;
  gap: 8px;
  margin-top: -8px;
}

.cal-dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2.5px solid var(--gray);
  box-sizing: border-box;
}

.cal-dot.played {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.cal-dot.completed {
  background: var(--accent);
  border-color: var(--accent);
}

.cal-dot.today {
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.link-button {
  margin-top: 2px;
  background: #fff4ea;
  border: 2px solid var(--accent-dim);
  color: #b56a1f;
  font-weight: bold;
  font-size: 0.9rem;
  padding: 0.55em 1.1em;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.1s ease;
}

.link-button:active {
  transform: scale(0.94);
  background: var(--accent-dim);
}

.points-badge {
  font-size: clamp(1.1rem, 2.8vmin, 1.4rem);
  font-weight: bold;
  color: #d6960f;
}

.app-version {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  font-size: 0.75rem;
  color: #ccc;
  font-variant-numeric: tabular-nums;
}

.module-label {
  font-size: 0.95rem;
  color: #999;
  font-weight: bold;
  margin-top: 4px;
}

.start-links {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 4px;
}

/* ---------- pin screen ---------- */

.pin-title {
  font-size: clamp(1.2rem, 3vmin, 1.6rem);
  font-weight: bold;
  color: #555;
  text-align: center;
}

.pin-display {
  font-size: 2.4rem;
  font-weight: bold;
  letter-spacing: 0.3em;
  color: var(--ink);
  min-height: 1.4em;
}

.pin-display.wrong {
  animation: shake 0.35s;
  color: var(--bad);
}

.pin-feedback {
  font-size: 1.1rem;
  font-weight: bold;
  color: var(--bad);
  min-height: 1.4em;
}

/* ---------- parent screen ---------- */

.parent-title {
  font-size: 1.8rem;
  font-weight: bold;
}

.parent-balance {
  font-size: 1.4rem;
  font-weight: bold;
  color: #d6960f;
}

.parent-section {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.parent-section-title {
  font-size: 1.05rem;
  color: #999;
  font-weight: bold;
}

.activity-grid, .reward-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  width: 100%;
}

.activity-btn, .reward-btn {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 12px 10px;
  border-radius: 14px;
  border: 2px solid var(--gray);
  background: white;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  line-height: 1.4;
}

.activity-btn:active, .reward-btn:active {
  transform: scale(0.96);
}

.activity-btn .pt {
  color: var(--good);
  font-weight: bold;
  font-size: 0.9rem;
}

.reward-btn .pt {
  color: var(--bad);
  font-weight: bold;
  font-size: 0.9rem;
}

.activity-btn:disabled, .reward-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.activity-btn.done-flash, .reward-btn.done-flash {
  animation: popIn 0.35s ease;
}

.reward-btn.affordable {
  border-color: var(--good);
  background: #f2fbf2;
}

.shop-hint {
  font-size: 1rem;
  color: #999;
  max-width: 420px;
  text-align: center;
}

/* ---------- records screen ---------- */

.records-title {
  font-size: 2rem;
  font-weight: bold;
}

.records-tabs {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.records-tab {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: bold;
  padding: 0.5em 1.1em;
  border-radius: 999px;
  border: 2px solid var(--gray);
  background: white;
  color: #999;
  cursor: pointer;
}

.records-tab.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
  color: #b56a1f;
}

.records-panel {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
}

.records-panel.active {
  display: flex;
}

.tier-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 420px;
}

.tier-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  border-radius: 12px;
  background: #f7f7f7;
  font-weight: bold;
}

.tier-label {
  color: #555;
}

.tier-value {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.tier-value.none {
  color: #bbb;
  font-weight: normal;
}

.kana-heatmap {
  grid-template-columns: repeat(8, minmax(0, 1fr));
  max-width: 460px;
}

.kana-heatmap .heat-cell {
  font-size: clamp(0.75rem, 2vmin, 1rem);
}

.heatmap {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 4px;
  width: 100%;
  max-width: 520px;
}

.heat-cell {
  aspect-ratio: 1;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.55rem, 1.6vmin, 0.8rem);
  color: white;
  font-weight: bold;
}

.heat-label {
  background: none;
  color: #999;
  font-weight: bold;
}

.heat-none { background: #eee; color: #bbb; }
.heat-weak { background: var(--bad); }
.heat-ok { background: #ffb84d; }
.heat-good { background: var(--good); }

.heatmap-legend {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  justify-content: center;
  font-size: 0.85rem;
  color: #888;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.legend-none { background: #eee; }
.legend-weak { background: var(--bad); }
.legend-ok { background: #ffb84d; }
.legend-good { background: var(--good); }

/* ---------- offline overlay ---------- */

.offline-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: white;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
  text-align: center;
  z-index: 1000;
}

.offline-overlay.shown {
  display: flex;
}

.offline-emoji {
  font-size: 4rem;
}

.offline-message {
  font-size: clamp(1rem, 2.6vmin, 1.3rem);
  line-height: 1.9;
  color: #555;
  max-width: 480px;
  white-space: pre-line;
}

/* ---------- play screen (landscape-first) ---------- */

#screen-play {
  justify-content: flex-start;
  padding: max(14px, env(safe-area-inset-top)) max(28px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom)) max(28px, env(safe-area-inset-left));
  gap: 10px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 1000px;
  align-self: center;
}

.progress, .timer {
  color: #aaa;
  font-size: clamp(0.9rem, 2.2vmin, 1.2rem);
  font-variant-numeric: tabular-nums;
}

.progress {
  flex: 1;
  text-align: center;
}

.quit-button {
  background: none;
  border: none;
  color: #ccc;
  font-size: clamp(0.8rem, 2vmin, 1rem);
  font-family: inherit;
  cursor: pointer;
  padding: 4px 8px;
}

.quit-button:active {
  color: #999;
}

.topbar-actions {
  display: flex;
  gap: 4px;
}

/* Icon-only, larger than the old text button so it's an easy one-tap target
   on a moving train — pause especially needs to be quick and unambiguous
   since it's penalty-free by design (see pauseSession()). */
.icon-button {
  background: none;
  border: 2px solid var(--gray);
  border-radius: 12px;
  color: #999;
  font-size: clamp(1rem, 2.6vmin, 1.3rem);
  font-family: inherit;
  cursor: pointer;
  min-width: 40px;
  min-height: 40px;
  line-height: 1;
}

.icon-button:active {
  transform: scale(0.92);
  background: #f5f5f5;
}

#btn-pause-quick {
  color: var(--accent);
  border-color: var(--accent-dim);
}

#btn-pause-quick:active {
  background: var(--accent-dim);
}

.session-time-track {
  width: 100%;
  max-width: 900px;
  align-self: center;
  height: 8px;
  border-radius: 999px;
  background: #f0f0f0;
  overflow: hidden;
}

.session-time-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--good), var(--accent));
  border-radius: 999px;
  transition: width 0.5s linear;
}

.qtimer-track {
  width: 100%;
  max-width: 900px;
  align-self: center;
  height: 5px;
  border-radius: 999px;
  background: #eee;
  overflow: hidden;
  margin-top: 2px;
}

.qtimer-fill {
  height: 100%;
  width: 100%;
  background: var(--accent);
  border-radius: 999px;
}

.qtimer-fill.running {
  width: 0%;
}

.qtimer-fill.urgent {
  background: var(--bad);
}

.live-points {
  display: flex;
  align-items: baseline;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0 10px;
  width: 100%;
  max-width: 900px;
  align-self: center;
  min-height: 1.3em;
}

.live-points-count {
  font-size: clamp(0.95rem, 2.4vmin, 1.25rem);
  font-weight: bold;
  color: #d6960f;
}

.live-points-goal {
  font-size: clamp(0.75rem, 1.9vmin, 0.95rem);
  color: #b56a1f;
  font-weight: bold;
}

.session-bar {
  display: flex;
  flex: 1;
  gap: 10px;
  max-width: 640px;
  margin: 0 12px;
}

.session-seg {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.session-seg-label {
  font-size: clamp(0.6rem, 1.5vmin, 0.8rem);
  color: #ccc;
  font-weight: bold;
  white-space: nowrap;
  transition: color 0.3s ease;
}

.session-seg.current .session-seg-label {
  color: var(--accent);
}

.session-seg.done .session-seg-label {
  color: #bbb;
}

.session-seg-track {
  width: 100%;
  height: 8px;
  border-radius: 999px;
  background: #eee;
  overflow: hidden;
}

.session-seg-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.subject-block {
  display: none;
  flex: 1;
  width: 100%;
  flex-direction: column;
}

.subject-block.active {
  display: flex;
}

.resume-button {
  display: none;
  background: var(--good);
}

.resume-button.shown {
  display: inline-block;
}

.quit-confirm-sub {
  font-size: 1.2rem;
  color: #888;
}

.play-main {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: clamp(20px, 5vw, 60px);
  width: 100%;
  max-width: 1100px;
  flex: 1;
  min-height: 0;
}

.left-pane {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2vmin, 22px);
  flex: 0 0 auto;
  min-width: 0;
}

.right-pane {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.6vmin, 16px);
  flex: 0 0 auto;
  width: min(400px, 100%);
}

.difficulty-tag {
  font-size: clamp(0.85rem, 2vmin, 1.15rem);
  font-weight: bold;
  color: var(--accent);
  min-height: 1.4em;
  opacity: 0;
  transform: translateY(-6px);
}

.difficulty-tag.shown {
  animation: popIn 0.4s ease forwards;
}

.frames {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vw, 28px);
  width: 100%;
}

.frames.enter {
  animation: slideFadeIn 0.4s ease;
}

.operand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.6vmin, 16px);
}

.operand-num {
  font-size: clamp(2rem, 8vmin, 3.6rem);
  font-weight: bold;
  line-height: 1;
}

.frame-plus {
  font-size: clamp(1.4rem, 4vmin, 2rem);
  color: #bbb;
  font-weight: bold;
}

.frame-group {
  display: grid;
  grid-template-columns: repeat(5, var(--circle-size));
  gap: var(--circle-gap);
  padding: 8px;
  position: relative;
  min-width: calc(var(--circle-size) * 5 + var(--circle-gap) * 4);
  min-height: calc(var(--circle-size) * 2 + var(--circle-gap));
}

.circle {
  width: var(--circle-size);
  height: var(--circle-size);
  border-radius: 50%;
  border: 2.5px solid var(--gray);
  background: white;
  transition: background 0.35s ease, border-color 0.35s ease, transform 0.4s cubic-bezier(.34,1.56,.64,1);
}

.circle.filled {
  background: var(--accent);
  border-color: var(--accent);
}

.circle.pending {
  background: #e8f4ff;
  border-color: #4dabf7;
}

/* Subtraction "take away" hint: circles crossed out rather than filled in. */
.circle.removed {
  background: #f5f5f5;
  border-color: #ddd;
  opacity: 0.5;
  position: relative;
}

.circle.removed::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(45deg, transparent 45%, var(--bad) 45%, var(--bad) 55%, transparent 55%),
    linear-gradient(-45deg, transparent 45%, var(--bad) 45%, var(--bad) 55%, transparent 55%);
  border-radius: 50%;
}

.group-caption {
  position: absolute;
  bottom: -22px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 1.1rem;
  color: var(--accent);
  font-weight: bold;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.group-caption.shown {
  opacity: 1;
}

.answer-display {
  min-width: 3.2em;
  min-height: 1.3em;
  font-size: clamp(2.6rem, 8vmin, 4rem);
  font-weight: bold;
  text-align: center;
  border-bottom: 5px solid var(--gray);
  padding: 0 0.2em;
  color: var(--ink);
}

.answer-display.correct {
  border-color: var(--good);
  color: var(--good);
}

.answer-display.wrong {
  border-color: var(--bad);
  animation: shake 0.35s;
}

.answer-display.pulse {
  animation: pulse 0.15s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.18); }
  100% { transform: scale(1); }
}

@keyframes popIn {
  0% { transform: scale(0.5) translateY(-6px); opacity: 0; }
  70% { transform: scale(1.15) translateY(0); opacity: 1; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes slideFadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.feedback {
  font-size: clamp(1.1rem, 3vmin, 1.6rem);
  font-weight: bold;
  min-height: 1.6em;
  color: var(--good);
}

.feedback.show {
  animation: popIn 0.35s ease;
}

.feedback.bad {
  color: var(--bad);
}

.keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(8px, 1.8vmin, 14px);
  width: 100%;
}

.key {
  font-size: clamp(1.6rem, 4.6vmin, 2.3rem);
  font-family: inherit;
  padding: clamp(0.5em, 2.2vmin, 0.75em) 0;
  border-radius: 16px;
  border: 2.5px solid var(--gray);
  background: white;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.1s ease;
}

.key:active {
  background: #f0f0f0;
  transform: scale(0.92);
}

.key-enter {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.key-del {
  color: #999;
}

/* ---------- summary screen ---------- */

.summary-title {
  font-size: 2rem;
  font-weight: bold;
}

.summary-score {
  font-size: 3rem;
  font-weight: bold;
  color: var(--accent);
}

.summary-time {
  font-size: 1.3rem;
  color: #aaa;
  font-variant-numeric: tabular-nums;
}

.summary-sub {
  font-size: 1.2rem;
  color: #888;
}

@media (max-height: 500px) {
  /* Short viewports (e.g. iPhone in landscape) don't have room for the
     iPad-tuned vertical rhythm — tighten every screen's chrome so nothing
     (start screen title, keypad's bottom row, etc.) gets pushed below the
     fold and forces a page scroll on what's meant to be a fixed screen. */
  .screen {
    padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    gap: 10px;
  }

  .title { font-size: clamp(1.4rem, 7vh, 3.5rem); }
  .big-button { padding: 0.5em 1.8em; }
  .set-size-picker { margin-top: 4px; gap: 4px; }

  #screen-play { padding: 6px 20px; gap: 4px; }

  .live-points { min-height: 0; }
  .live-points-count { font-size: clamp(0.8rem, 3vh, 1.1rem); }
  .live-points-goal { font-size: clamp(0.65rem, 2.2vh, 0.85rem); }

  .session-time-track, .qtimer-track { height: 4px; }

  .play-main { gap: clamp(10px, 3vw, 24px); }
  .right-pane { gap: 6px; }

  .difficulty-tag { min-height: 0; font-size: clamp(0.7rem, 1.8vh, 1rem); }

  .answer-display {
    min-height: 0;
    font-size: clamp(1.6rem, 5.5vh, 3rem);
    border-bottom-width: 3px;
  }

  .keypad { gap: clamp(4px, 1vh, 10px); }

  .key {
    font-size: clamp(1.1rem, 4.2vh, 2rem);
    padding: clamp(0.25em, 1vh, 0.6em) 0;
    border-radius: 10px;
  }
}

/* ---------- phone-width layout (e.g. iPhone portrait/landscape) ---------- */

@media (max-width: 700px) {
  html, body { overflow-x: hidden; }

  .play-main {
    flex-direction: column;
    gap: clamp(10px, 2.5vh, 22px);
  }

  .right-pane {
    width: 100%;
    max-width: 400px;
  }

  .topbar {
    gap: 6px;
  }

  .session-bar {
    margin: 0 6px;
    gap: 6px;
  }
}
