:root {
  --bg: #faf7f2;
  --fg: #2b2b2b;
  --accent: #c0392b;
  --accent-dark: #962d22;
  --accent-learner: #2f6fb5;
  --card-bg: #ffffff;
  --border: #e0d8cc;
  --correct: #2e7d32;
  --incorrect: #c0392b;
  --mastery-new: #b0a999;
  --mastery-learning: #c0392b;
  --mastery-familiar: #d9a441;
  --mastery-mastered: #2e7d32;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a18;
    --fg: #f0ece4;
    --card-bg: #2a2622;
    --border: #443d34;
  }
}

* { box-sizing: border-box; }

/* Restore native `[hidden]` semantics. Several component rules below set
   `display` unconditionally (.rec-indicator, .diagram-row, .mora-feedback),
   and an author-stylesheet rule always wins over the user-agent
   stylesheet's `[hidden] { display: none }` regardless of selector
   specificity -- so without this, toggling the `hidden` attribute/property
   from JS has no visual effect on those elements. One general override
   here keeps every current and future `hidden`-toggled element correctly
   hidden, instead of special-casing `:not([hidden])` onto each component
   rule that happens to set `display`. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: "Hiragino Sans", "Yu Gothic", "Noto Sans JP", system-ui, -apple-system, sans-serif;
  min-height: 100%;
}

.app-header {
  position: relative;
  max-width: 480px;
  margin: 0 auto;
  text-align: center;
  padding: 1.2rem 1rem 0.6rem;
}

.app-header h1 {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: 0.05em;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--fg);
  cursor: pointer;
  padding: 0.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover, .icon-btn:focus-visible {
  color: var(--accent);
}

.icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.settings-btn {
  position: absolute;
  top: 0.7rem;
  right: 0.8rem;
}

.hamburger-icon {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 1.3rem;
  height: 0.9rem;
}

.hamburger-icon span {
  display: block;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
}

/* --- Settings dialog --- */
/* Shown/hidden entirely via the `hidden` attribute (see the [hidden] rule
   above) -- no separate `.hidden` class needed here, unlike the sibling
   apps this panel is ported from. */

.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}

.settings-panel {
  width: 100%;
  max-width: 360px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem 1.2rem;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.4rem;
}

.settings-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.settings-header h2 span {
  display: block;
  font-size: 0.65rem;
  font-weight: normal;
  color: #8a8378;
  margin-top: 0.1rem;
}

.settings-header .icon-btn {
  font-size: 1.4rem;
  line-height: 1;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  padding: 0.8rem 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child { border-bottom: none; }

.settings-row-column {
  flex-direction: column;
  align-items: stretch;
  gap: 0.5rem;
}

.settings-row-label {
  font-size: 0.9rem;
}

.settings-row-label span {
  display: block;
  font-size: 0.7rem;
  font-weight: normal;
  color: #8a8378;
  margin-top: 0.1rem;
}

.switch {
  position: relative;
  display: inline-block;
  flex-shrink: 0;
  width: 2.6rem;
  height: 1.5rem;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.switch-slider::before {
  content: '';
  position: absolute;
  width: 1.1rem;
  height: 1.1rem;
  left: 0.2rem;
  top: 0.2rem;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s ease;
}

.switch input:checked + .switch-slider { background: var(--accent); }
.switch input:checked + .switch-slider::before { transform: translateX(1.1rem); }

.switch input:focus-visible + .switch-slider {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Practice-level picker (js/word-select.js's LEVELS) -- same segmented
   control jlpt/kotoba use for their questions-per-round setting. */
.settings-segmented {
  display: flex;
  gap: 0.4rem;
}

.segmented-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  padding: 0.5rem 0.3rem;
  font-size: 0.9rem;
  font-weight: bold;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}

.segmented-btn span {
  font-size: 0.6rem;
  font-weight: normal;
  color: #8a8378;
}

.segmented-btn:hover, .segmented-btn:focus-visible {
  border-color: var(--accent);
}

.segmented-btn.active {
  border-color: var(--accent);
  color: var(--accent);
}

.settings-install-btn {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  align-self: flex-start;
  padding: 0.6rem 0.9rem;
  font-size: 0.9rem;
  font-weight: bold;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
}

.settings-install-btn span {
  font-size: 0.7rem;
  font-weight: normal;
  color: #8a8378;
}

.settings-install-btn:hover, .settings-install-btn:focus-visible {
  border-color: var(--accent);
}

.settings-install-hint {
  margin: 0;
  font-size: 0.75rem;
  line-height: 1.5;
  color: #8a8378;
}

/* About section in Settings */
.about-app {
  margin: 0;
  font-size: 0.95rem;
  font-weight: bold;
}

.about-link {
  align-self: flex-start;
  margin-top: 0.3rem;
  font-size: 0.85rem;
  font-weight: bold;
  color: var(--accent);
  text-decoration: none;
}

.about-link span {
  display: block;
  margin-top: 0.1rem;
  font-size: 0.7rem;
  font-weight: normal;
  color: #8a8378;
}

.about-link:hover,
.about-link:focus-visible {
  text-decoration: underline;
}

main {
  max-width: 480px;
  margin: 0 auto;
  padding: 0.5rem 1rem 2rem;
}

/* Generic card container -- the word-quiz card, and anything else boxed
   the same way, sits inside one of these. Onchou-specific layout (pitch
   diagrams, record button, mora-match highlighting) is added during
   integration, not here. */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 1.2rem 1.5rem;
  text-align: center;
}

.card + .card {
  margin-top: 1rem;
}

button {
  font-family: inherit;
}

.primary-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.9rem;
  font-size: 1.05rem;
  font-weight: bold;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  width: 100%;
}

.primary-btn:hover { background: var(--accent-dark); }

.primary-btn:disabled {
  background: var(--border);
  color: var(--fg);
  opacity: 0.6;
  cursor: not-allowed;
}

.secondary-btn {
  padding: 0.7rem 1rem;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  color: var(--fg);
  cursor: pointer;
}

.secondary-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.note {
  font-size: 0.8rem;
  color: #8a8378;
  margin-top: 0.4rem;
}

/* "▶ Play" reference-audio button (js/reference-audio.js). Small and
   secondary -- it's a listening aid alongside the pitch diagram, not the
   card's primary action. */
.play-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  font-size: 0.95rem;
  border: 1px solid var(--accent-learner);
  border-radius: var(--radius);
  background: var(--card-bg);
  color: var(--accent-learner);
  cursor: pointer;
}

.play-btn:hover, .play-btn:focus-visible {
  background: var(--accent-learner);
  color: #fff;
}

.play-btn:focus-visible {
  outline: 2px solid var(--accent-learner);
  outline-offset: 2px;
}

/* Visually-disabled state -- no Japanese voice found on this
   device/browser. app.js decides *when* to apply this (via the
   `disabled` attribute and/or `.disabled` class) and supplies the
   accompanying inline note text (see .play-btn-note); this file only
   owns the look. */
.play-btn:disabled,
.play-btn.disabled {
  border-color: var(--border);
  background: var(--card-bg);
  color: var(--fg);
  opacity: 0.55;
  cursor: not-allowed;
}

.play-btn:disabled:hover,
.play-btn.disabled:hover {
  background: var(--card-bg);
  color: var(--fg);
}

.play-btn-note {
  font-size: 0.8rem;
  color: #8a8378;
  margin-top: 0.3rem;
}

/* Pitch-accent plot (js/pitch-diagram.js): filled dots trace a word's own
   morae at high/low pitch, connected by a line; one hollow trailing dot
   stands for whatever follows the word (stays high only for heiban -- see
   pitchLevels() in js/pitch-diagram.js). Ported from jlpt/kotoba's static
   diagram convention, generalized with a 'target'/'learner' variant so the
   known-correct pattern and the learner's detected recording render in
   distinct colors when shown stacked. A third 'unclear' dot state (no
   voiced pitch detected for that mora, from js/mora-segment.js) renders
   smaller and greyed, sitting at a fixed mid-height between the H/L rows,
   with no special-cased line gap. */
.pitch-accent-group {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.pitch-plot {
  overflow: visible;
  vertical-align: middle;
}

.pitch-line-target {
  stroke: var(--accent);
  stroke-width: 1.5;
}

.pitch-line-learner {
  stroke: var(--accent-learner);
  stroke-width: 1.5;
}

.pitch-dot-target {
  fill: var(--accent);
}

.pitch-dot-learner {
  fill: var(--accent-learner);
}

.pitch-dot-trailing {
  fill: var(--card-bg);
  stroke-width: 1.5;
}

.pitch-dot-trailing.pitch-dot-target {
  fill: var(--card-bg);
  stroke: var(--accent);
}

.pitch-dot-trailing.pitch-dot-learner {
  fill: var(--card-bg);
  stroke: var(--accent-learner);
}

/* 'unclear' mora (no voiced pitch detected) -- smaller, greyed, visually
   distinct from both H and L regardless of variant. */
.pitch-dot-unclear {
  fill: var(--border);
  stroke: none;
}

/* Raw pitch-curve graph (js/pitch-contour.js): the learner's actual F0
   trace as a continuous line, alongside a stylized step-line for the
   target H/L pattern -- shown below the existing dot-and-line diagrams
   above, not replacing them. Reuses the same --accent (target)/
   --accent-learner (learner) tokens as the dot diagrams so the two views
   read as the same target/learner color pairing throughout the card. */
.contour-plot {
  overflow: visible;
  vertical-align: middle;
}

.contour-line-target {
  stroke: var(--accent);
  stroke-width: 1.5;
  stroke-dasharray: 3 2;
}

.contour-line-learner {
  stroke: var(--accent-learner);
  stroke-width: 1.5;
}

/* Recording-in-progress indicator: a small pulsing dot, placed next to the
   Record/Stop button while a recording is active. Uses existing tokens
   only (--accent for the dot color). Toggled via [hidden] around
   PitchDetect.startRecording()/stopRecording() calls in js/app.js. */
.rec-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  font-size: 0.85rem;
  color: var(--accent);
  margin-top: 0.6rem;
  justify-content: center;
}
.rec-indicator .rec-dot {
  width: 0.6em;
  height: 0.6em;
  border-radius: 50%;
  background: var(--accent);
  animation: rec-pulse 1.2s ease-in-out infinite;
}
@keyframes rec-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.75); }
}

/* Word card layout (js/app.js). */
.word-card .word-text {
  font-size: 2rem;
  margin: 0 0 0.2rem;
}

.word-card .word-reading {
  font-size: 1.1rem;
  color: #8a8378;
  margin: 0 0 1rem;
}

.diagram-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  margin: 0.8rem 0;
}

.diagram-row .diagram-label {
  font-size: 0.75rem;
  color: #8a8378;
}

.action-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 0.8rem;
}

.shortcut-hint {
  font-size: 0.75rem;
  color: #8a8378;
}

.mora-feedback {
  display: flex;
  justify-content: center;
  gap: 0.4rem;
  margin: 0.6rem 0;
  flex-wrap: wrap;
}

.mora-feedback .mora-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.6rem;
  height: 1.6rem;
  padding: 0 0.3rem;
  border-radius: 999px;
  font-size: 0.85rem;
  border: 1px solid var(--border);
}

.mora-feedback .mora-chip.match {
  background: var(--correct);
  border-color: var(--correct);
  color: #fff;
}

.mora-feedback .mora-chip.mismatch {
  background: var(--incorrect);
  border-color: var(--incorrect);
  color: #fff;
}

.mora-feedback .mora-chip.unclear {
  background: var(--border);
  color: var(--fg);
}

.score-text {
  font-weight: bold;
  margin: 0.4rem 0;
}

.inline-message {
  font-size: 0.9rem;
  color: var(--incorrect);
  margin: 0.6rem 0;
}

.unsupported-notice {
  text-align: center;
  padding: 2rem 1rem;
}

#unsupported-notice-text {
  white-space: pre-line;
}

/* Playback of the learner's own recording (#playback-row), sitting directly
   under the learner diagram so the visual reading and the audio it came from
   are read together. */
.playback-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  margin: 0.6rem 0 0.2rem;
}

.playback-row .secondary-btn {
  font-size: 0.85rem;
}

#compare-btn {
  border-color: var(--accent-learner);
  color: var(--accent-learner);
}

.playback-note {
  flex-basis: 100%;
  margin: 0.1rem 0 0;
  font-size: 0.75rem;
  color: #8a8378;
}

/* ---------------------------------------------------------------------------
   Tutorial page (tutorial/index.html)
   Reuses .card / .app-header / the .pitch-* diagram classes above; only the
   long-form prose layout is new here.
   --------------------------------------------------------------------------- */

/* The app header is capped at 480px for the quiz card; the tutorial's prose
   column is wider, so widen its header to match or the back link and title
   sit inset from the content below them. */
.tutorial-page .app-header {
  max-width: 620px;
  text-align: left;
  padding-left: 1rem;
}

.tutorial-page .app-header h1 {
  font-size: 1.25rem;
}

.tutorial-back {
  position: static;
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
  font-weight: bold;
  color: var(--accent);
  text-decoration: none;
}

.tutorial-back:hover,
.tutorial-back:focus-visible {
  text-decoration: underline;
}

.tutorial-subtitle {
  margin: 0.3rem 0 0;
  font-size: 0.8rem;
  color: #8a8378;
}

main.tutorial {
  max-width: 620px;
  text-align: left;
}

.tutorial-toc {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0.5rem 0 1rem;
  padding: 0.9rem 1.1rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
}

.tutorial-toc a {
  color: var(--accent);
  text-decoration: none;
}

.tutorial-toc a:hover,
.tutorial-toc a:focus-visible {
  text-decoration: underline;
}

.tutorial-section {
  text-align: left;
  padding: 1.2rem 1.2rem 1.4rem;
}

.tutorial-section h2 {
  margin: 0 0 0.8rem;
  font-size: 1.05rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
}

.tutorial-section h2 span {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.75rem;
  font-weight: normal;
  color: #8a8378;
}

.tutorial-section p,
.tutorial-section li {
  font-size: 0.9rem;
  line-height: 1.7;
}

.tutorial-section ul,
.tutorial-section ol {
  padding-left: 1.3rem;
}

.tutorial-section code {
  font-size: 0.85em;
  padding: 0.05em 0.3em;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
}

.tutorial-en {
  color: #8a8378;
  font-size: 0.85em;
}

.tutorial-examples {
  padding: 0.7rem 0.9rem;
  background: var(--bg);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  line-height: 2;
}

.tutorial-pattern {
  margin-top: 1.1rem;
  padding-top: 0.9rem;
  border-top: 1px dashed var(--border);
}

.tutorial-pattern h3 {
  margin: 0 0 0.4rem;
  font-size: 0.95rem;
}

.tutorial-pattern h3 span {
  font-size: 0.75rem;
  font-weight: normal;
  color: #8a8378;
}

.tutorial-diagram-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  margin: 0.8rem 0;
}

.tutorial-diagram {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  transform-origin: left top;
  scale: 1.6;
  width: fit-content;
  /* `scale` doesn't affect layout size, so the extra 0.6x of height has to be
     paid for by margin or the next paragraph rides over the diagram. */
  margin: 0.6rem 0 1.9rem;
}

/* On a phone the 1.6x diagrams plus their scaled captions get close to the
   content width; back off the magnification rather than let them overflow. */
@media (max-width: 420px) {
  .tutorial-diagram {
    scale: 1.3;
    margin-bottom: 1.4rem;
  }
}

.tutorial-diagram-caption {
  font-size: 0.5rem;
  color: #8a8378;
}

/* Inside the minimal-pairs table the diagrams sit in a narrow cell, so they
   stay unscaled and inline rather than blown up like the section examples. */
.tutorial-diagram-inline {
  display: inline-block;
  scale: 1;
  margin: 0;
}

.tutorial-swatch {
  display: inline-block;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  vertical-align: middle;
}

.tutorial-swatch-target { background: var(--accent); }
.tutorial-swatch-learner { background: var(--accent-learner); }

.tutorial-table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tutorial-pairs {
  width: 100%;
  min-width: 320px;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin: 0.6rem 0;
}

.tutorial-pairs th,
.tutorial-pairs td {
  border: 1px solid var(--border);
  padding: 0.4rem 0.5rem;
  text-align: left;
  vertical-align: middle;
}

.tutorial-pairs th {
  font-size: 0.75rem;
  color: #8a8378;
  font-weight: normal;
}

.tutorial-note {
  color: #8a8378;
  font-size: 0.85rem;
}

.tutorial-source {
  margin: 1.2rem 0 0;
  text-align: center;
  font-size: 0.75rem;
  color: #8a8378;
}

.tutorial-source a { color: var(--accent); }
