:root {
  color-scheme: light;
  --surface:        #fcfcfb;
  --page:           #f9f9f7;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --accent:         #2a78d6;
  --accent-strong:  #184f95;
  --border:         #e1e0d9;
  --card-border:    rgba(11,11,11,0.10);
  --gold:           #a6790a;
  --gold-bg:        #f7ecd6;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface:        #1a1a19;
    --page:           #0d0d0d;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --accent:         #3987e5;
    --accent-strong:  #86b6ef;
    --border:         #2c2c2a;
    --card-border:    rgba(255,255,255,0.10);
    --gold:           #e0ae3f;
    --gold-bg:        #332a17;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface:        #1a1a19;
  --page:           #0d0d0d;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --accent:         #3987e5;
  --accent-strong:  #86b6ef;
  --border:         #2c2c2a;
  --card-border:    rgba(255,255,255,0.10);
  --gold:           #e0ae3f;
  --gold-bg:        #332a17;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: "Hiragino Sans", "Yu Gothic", "Noto Sans JP", sans-serif;
  font-size: 20px;
  line-height: 1.7;
  height: 100%;
}

/* ---------- app shell ---------- */
.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.view-root {
  flex: 1;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px calc(90px + env(safe-area-inset-bottom));
}
.view-root[aria-busy="true"] { opacity: 0.6; }

.bottom-nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.bottom-nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 0 8px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.75em;
}
.bottom-nav a.active { color: var(--accent); font-weight: bold; }
.nav-icon { font-size: 1.4em; line-height: 1; }

.install-hint {
  text-align: center;
  color: var(--accent);
  font-size: 0.8em;
  padding: 0 20px;
}
.disclaimer-note {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.75em;
  padding: 0 20px 8px;
}

.app-header {
  text-align: center;
  margin-bottom: 24px;
}
.app-header h1 {
  font-size: 1.8em;
  margin: 0 0 4px;
}
.subtitle {
  color: var(--text-secondary);
  margin: 0;
  font-size: 0.9em;
}

.back-link {
  display: inline-block;
  color: var(--accent);
  text-decoration: none;
  margin-bottom: 16px;
  font-size: 0.9em;
}

.loading-note, .empty-note {
  color: var(--text-muted);
  text-align: center;
  padding: 20px;
}

.card, .story-card, .test-card {
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
}

.story-card h2, .test-card h2 {
  margin: 0 0 4px;
  font-size: 1.4em;
}
.test-card h3 { margin: 0 0 12px; font-size: 1.1em; }
.story-note {
  color: var(--text-secondary);
  margin: 0 0 16px;
  font-size: 0.85em;
}

/* ---------- story library grid ---------- */
.library h2 { font-size: 1.2em; margin: 0 0 14px; }
.story-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.story-tile {
  display: block;
  text-decoration: none;
  color: var(--text-primary);
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  overflow: hidden;
}
.story-tile-img {
  aspect-ratio: 16 / 9;
  background-size: cover;
  background-position: center;
  background-color: var(--border);
  position: relative;
}
.story-done-badge {
  position: absolute;
  top: 8px; right: 8px;
  background: var(--accent);
  color: #fff;
  width: 24px; height: 24px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8em;
}
.story-tile-title {
  padding: 10px 12px;
  font-weight: bold;
  font-size: 0.95em;
}

/* ---------- story player ---------- */
.story-hero {
  aspect-ratio: 16 / 9;
  background-size: cover;
  background-position: center;
  border-radius: 16px;
  margin-bottom: 16px;
  background-color: var(--border);
}

.speed-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.speed-row-label {
  color: var(--text-secondary);
  font-size: 0.85em;
}
.speed-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.speed-btn {
  min-width: 44px;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-primary);
  font-size: 0.9em;
  cursor: pointer;
}
.speed-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: bold;
}

.audio-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
}
.current-speed {
  color: var(--text-secondary);
  font-size: 0.9em;
}
.audio-scrub-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
  font-size: 0.75em;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.audio-scrub-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}
.bg-hint {
  color: var(--text-muted);
  font-size: 0.75em;
  margin: 0 0 16px;
}

.big-button {
  min-height: 64px;
  padding: 0 28px;
  font-size: 1.1em;
  font-weight: bold;
  border-radius: 14px;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.big-button.primary {
  background: var(--accent);
  color: #ffffff;
}
.big-button.primary:active {
  background: var(--accent-strong);
}
.big-button.secondary {
  width: 100%;
  justify-content: center;
  background: var(--page);
  color: var(--text-primary);
  border: 2px solid var(--accent);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.big-button.secondary.held {
  background: var(--accent);
  color: #ffffff;
}

.story-text {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  font-size: 1.05em;
  white-space: pre-wrap;
}

.complete-note {
  margin-top: 14px;
  padding: 12px 14px;
  background: var(--gold-bg);
  color: var(--gold);
  border-radius: 10px;
  font-weight: bold;
  text-align: center;
}

.test-instructions {
  color: var(--text-secondary);
  font-size: 0.9em;
  margin-top: 0;
}

.test-readout {
  text-align: center;
  margin-top: 14px;
  font-size: 1.2em;
  min-height: 1.6em;
}

.chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 120px;
  border-bottom: 1px solid var(--border);
  padding: 0 4px;
}
.chart-bar-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  flex: 1;
  height: 100%;
  gap: 4px;
}
.chart-bar {
  width: 100%;
  max-width: 28px;
  background: var(--accent);
  border-radius: 4px 4px 0 0;
}
.chart-bar-label {
  font-size: 0.7em;
  color: var(--text-muted);
}

/* ---------- rank / streak header ---------- */
.rank-card {
  background: var(--surface);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 16px 20px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.rank-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.rank-badge {
  min-width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--gold-bg);
  color: var(--gold);
  font-weight: bold;
  font-size: 0.95em;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--gold);
}
.rank-name {
  font-weight: bold;
  font-size: 1.05em;
}
.rank-points {
  color: var(--text-secondary);
  font-size: 0.85em;
}
.rank-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  min-width: 160px;
}
.streak {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.9em;
  color: var(--text-secondary);
}
.streak-flame {
  color: var(--gold);
  font-size: 0.7em;
}
.rank-progress-wrap { width: 100%; }
.rank-progress-bar {
  width: 100%;
  height: 8px;
  border-radius: 4px;
  background: var(--border);
  overflow: hidden;
}
.rank-progress-fill {
  height: 100%;
  background: var(--gold);
  border-radius: 4px;
  width: 0%;
  transition: width 0.3s ease;
}
.rank-progress-label {
  font-size: 0.7em;
  color: var(--text-muted);
  text-align: right;
  margin-top: 3px;
}

/* ---------- points tag ---------- */
.points-tag {
  display: inline-block;
  font-size: 0.55em;
  font-weight: bold;
  color: var(--gold);
  background: var(--gold-bg);
  border-radius: 999px;
  padding: 3px 10px;
  vertical-align: middle;
  margin-left: 6px;
}

/* ---------- read-aloud test ---------- */
.readaloud-text {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 14px;
  font-size: 1.15em;
  text-align: center;
  color: var(--text-secondary);
  min-height: 2.4em;
}
.readaloud-fallback {
  color: var(--text-muted);
  font-size: 0.8em;
  text-align: center;
  margin-top: 10px;
}

/* ---------- rewards / family share card ---------- */
.rewards-card {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
}
.rewards-card h2 {
  margin: 0 0 8px;
  font-size: 1.15em;
}
.rewards-card p {
  color: var(--text-secondary);
  font-size: 0.9em;
  margin: 0 0 14px;
}
.share-result {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.share-result input {
  flex: 1;
  min-width: 160px;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-secondary);
  padding: 0 10px;
  font-size: 0.85em;
}
.share-result .big-button { min-height: 44px; padding: 0 16px; font-size: 0.9em; width: auto; }

.family-stat {
  font-size: 2em;
  font-weight: bold;
  color: var(--accent);
  margin: 0;
}

@media (max-width: 400px) {
  html, body { font-size: 18px; }
  .big-button { padding: 0 18px; }
  .rank-card { flex-direction: column; align-items: stretch; }
  .rank-right { align-items: stretch; }
  .story-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
}
