/* Design aligned with the main site (LaTeX.css academic style):
   serif body and headings, cool near-white background, near-black text,
   underlined dark links, flat and restrained.
   Numbers (sentence index, scores) keep a monospace font as "data". */

:root {
  /* Cool near-white background and near-black text */
  --bg: hsl(210, 20%, 98%);
  --surface: #ffffff;
  --surface-2: hsl(210, 18%, 95.5%);
  --border: hsl(210, 16%, 90%);
  --border-strong: hsl(210, 14%, 80%);

  --text: hsl(210, 15%, 12%);
  --text-muted: hsl(210, 9%, 42%);
  --text-faint: hsl(210, 12%, 62%);

  /* Accent: red, only for key moments — recording, errors, poor pronunciation */
  --accent: #ee5555;
  --accent-soft: hsl(0, 78%, 95%);
  --accent-text: hsl(0, 62%, 45%);

  /* Score level colors (bad reuses the red accent) */
  --good: hsl(145, 32%, 38%);
  --good-soft: hsl(145, 30%, 93%);
  --mid: hsl(38, 58%, 42%);
  --mid-soft: hsl(40, 52%, 90%);

  --radius: 4px;
  /* Serif stack: prefer Latin Modern (if installed locally), fall back to generic serifs and CJK Mincho/Song */
  --serif: "Latin Modern Roman", Georgia, "Times New Roman",
           "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP",
           "Songti SC", "Noto Serif SC", STSong, "SimSun", serif;
  --mono: "Latin Modern Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

/* Ensure the hidden attribute always wins, even when an element sets display (e.g. flex). */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--serif);
  background: var(--bg);
  color: var(--text);
  line-height: 1.72;
  font-size: 16px;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 2px;
}
a:hover { color: var(--accent); }

/* ---- Header (centered masthead, echoing the main site) ---- */
.app-header {
  background: transparent;
}
.header-inner {
  position: relative;
  max-width: 720px;
  margin: 0 auto;
  padding: 40px 24px 30px;
  text-align: center;
}
.title-block h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.subtitle {
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--text-muted);
  font-style: italic;
}
.ghost-btn {
  position: absolute;
  top: 24px;
  right: 24px;
  font-family: var(--serif);
  font-size: 14px;
  padding: 2px 4px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.ghost-btn:hover { color: var(--accent); }

.global-toggle {
  position: absolute;
  top: 24px;
  left: 24px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--serif);
  font-size: 14px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.global-toggle input {
  cursor: pointer;
  accent-color: var(--accent);
  margin: 0;
}

/* ---- Main ---- */
.app-main {
  max-width: 720px;
  margin: 0 auto;
  padding: 34px 24px 72px;
  display: flex;
  flex-direction: column;
  gap: 34px;
}

.section-title {
  display: block;
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  margin: 0 0 14px;
}

/* ---- Credentials panel ---- */
.key-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
}
.notice {
  font-size: 14px;
  color: var(--text-muted);
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 12px 16px;
  margin: 0 0 18px;
  line-height: 1.7;
}
.notice strong { color: var(--text); font-weight: 700; }
.key-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 220px;
  font-size: 13px;
  color: var(--text-muted);
}
.field input {
  font-family: var(--mono);
  font-size: 14px;
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.field input:focus {
  outline: none;
  border-color: var(--text-muted);
}
.key-buttons { display: flex; gap: 10px; }
.key-status {
  margin: 16px 0 0;
  font-size: 13px;
  font-family: var(--mono);
}
.key-status[data-state="ok"] { color: var(--text); }
.key-status[data-state="empty"] { color: var(--text-faint); }

.key-saved {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.key-saved .key-status { margin: 0; }

.voice-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.voice-form select {
  font-family: var(--serif);
  font-size: 14px;
  padding: 8px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.voice-form select:focus {
  outline: none;
  border-color: var(--text-muted);
}

/* ---- Input area ---- */
#input-text {
  width: 100%;
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.75;
  padding: 16px 18px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  resize: vertical;
}
#input-text:focus {
  outline: none;
  border-color: var(--text-muted);
}
/* Hidden mode: cover the whole practice textarea with a solid black block. */
#input-text.input-masked {
  color: transparent;
  background: var(--text);
  caret-color: transparent;
}
#input-text.input-masked::placeholder { color: transparent; }
#input-text.input-masked::selection { color: transparent; background: transparent; }
.input-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}
.sentence-count {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text-faint);
  margin-left: auto;
}

/* ---- Buttons ---- */
.btn {
  font-family: var(--serif);
  font-size: 14px;
  padding: 6px 16px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--text-faint);
}
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary {
  background: var(--text);
  border-color: var(--text);
  color: var(--bg);
}
.btn.primary:hover:not(:disabled) {
  background: #000;
  border-color: #000;
}

/* ---- Sentence list ---- */
.sentence-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.empty-hint {
  color: var(--text-faint);
  font-size: 15px;
  font-style: italic;
  padding: 8px 2px;
  margin: 0;
}
.sentence-row {
  display: flex;
  gap: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  transition: border-color 0.15s;
}
.sentence-row.is-recording {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}
.row-index {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-faint);
  padding-top: 4px;
  min-width: 26px;
  user-select: none;
}
.row-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.row-text {
  font-size: 18px;
  line-height: 1.7;
  color: var(--text);
  word-break: break-word;
}
.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.lang-toggle {
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 5px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
  min-width: 40px;
}
.lang-toggle:hover { border-color: var(--text-faint); color: var(--text); }

.hide-toggle {
  font-family: var(--mono);
  font-size: 12px;
  padding: 5px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
}
.hide-toggle:hover { border-color: var(--text-faint); color: var(--text); }
.hide-toggle[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
}

/* Hidden text: render one continuous black bar per line (for from-memory shadowing).
   The inline background breaks per line, and box-decoration-break: clone rounds each line fragment. */
.sentence-row.is-hidden .row-text-inner {
  color: transparent;
  background: var(--text);
  border-radius: 3px;
  padding: 0.05em 0.1em;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

/* ---- Row status ---- */
.row-status {
  font-size: 14px;
  font-family: var(--serif);
  padding: 4px 0 0;
}
.row-status[data-kind="info"] { color: var(--text-muted); }
.row-status[data-kind="recording"] {
  color: var(--accent);
  font-weight: 600;
}
.row-status[data-kind="error"] {
  color: var(--accent-text);
  background: var(--accent-soft);
  padding: 8px 12px;
  border-radius: var(--radius);
}

/* ---- Score results ---- */
.row-result {
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
/* display:flex would override the hidden attribute's default display:none; restore it explicitly */
.row-result[hidden] { display: none; }
.score-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.score-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 72px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}
.score-value {
  font-family: var(--mono);
  font-size: 24px;
  font-weight: 600;
  line-height: 1;
  color: var(--text);
}
.score-chip[data-level="good"] .score-value { color: var(--good); }
.score-chip[data-level="mid"] .score-value { color: var(--mid); }
.score-chip[data-level="bad"] .score-value { color: var(--accent); }
.score-label {
  font-family: var(--serif);
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 7px;
}

.words-line {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 6px;
  font-size: 19px;
  line-height: 1.95;
}
.word {
  position: relative;
  padding: 1px 4px;
  border-radius: var(--radius);
  cursor: default;
}
.word[data-level="good"] { color: var(--good); }
.word[data-level="mid"] { color: var(--mid); background: var(--mid-soft); }
.word[data-level="bad"] { color: var(--accent); background: var(--accent-soft); }
.word[data-error="omission"] {
  color: var(--text-faint);
  text-decoration: line-through;
}
.word[data-error="insertion"] {
  color: var(--accent);
  border-bottom: 2px dashed var(--accent);
}

/* Hidden mode: each scored word (morpheme) becomes a solid color block in its
   level color; the hover tooltip still shows normally. */
.sentence-row.is-hidden .word {
  color: transparent;
  border-radius: 3px;
  text-decoration: none;
  border-bottom: none;
}
.sentence-row.is-hidden .word[data-level="good"] { background: var(--good); }
.sentence-row.is-hidden .word[data-level="mid"] { background: var(--mid); }
.sentence-row.is-hidden .word[data-level="bad"] { background: var(--accent); }
.sentence-row.is-hidden .word[data-error="omission"] { background: var(--text-faint); }
.sentence-row.is-hidden .word[data-error="insertion"] { background: var(--accent); }

/* ---- Word hover tooltip (paper card, shown instantly) ---- */
.word-tip {
  position: absolute;
  bottom: calc(100% + 9px);
  left: 50%;
  transform: translateX(-50%) translateY(3px);
  z-index: 30;
  width: max-content;
  max-width: 320px;
  padding: 11px 13px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(20, 30, 50, 0.14);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  /* No enter delay: appears immediately on hover, with only a very short fade */
  transition: opacity 0.09s ease, transform 0.09s ease;
}
.word:hover .word-tip {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}
.word-tip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: var(--surface);
}
.tip-head {
  display: block;
  font-family: var(--serif);
  font-size: 14px;
  line-height: 1.4;
  color: var(--text);
}
.tip-head[data-level="good"] { border-left: 3px solid var(--good); padding-left: 8px; }
.tip-head[data-level="mid"] { border-left: 3px solid var(--mid); padding-left: 8px; }
.tip-head[data-level="bad"] { border-left: 3px solid var(--accent); padding-left: 8px; }
.tip-phonemes {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 10px;
}
.ph {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 28px;
  padding: 4px 6px 3px;
  border-radius: var(--radius);
  background: var(--surface-2);
}
.ph b {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  color: var(--text);
}
.ph i {
  font-family: var(--mono);
  font-style: normal;
  font-size: 11px;
  line-height: 1;
}
.ph[data-level="good"] { box-shadow: inset 0 0 0 1px rgba(79, 122, 92, 0.35); }
.ph[data-level="good"] i { color: var(--good); }
.ph[data-level="mid"] { box-shadow: inset 0 0 0 1px rgba(156, 119, 51, 0.4); }
.ph[data-level="mid"] i { color: var(--mid); }
.ph[data-level="bad"] { box-shadow: inset 0 0 0 1px rgba(238, 85, 85, 0.45); }
.ph[data-level="bad"] i { color: var(--accent); }

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--text-muted);
}
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.legend-item i {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  display: inline-block;
}
.legend-item i[data-level="good"] { background: var(--good); }
.legend-item i[data-level="mid"] { background: var(--mid); }
.legend-item i[data-level="bad"] { background: var(--accent); }
.legend-item i[data-error="omission"] {
  background: transparent;
  border-bottom: 2px solid var(--text-faint);
  border-radius: 0;
  height: 8px;
}
.legend-item i[data-error="insertion"] {
  background: transparent;
  border-bottom: 2px dashed var(--accent);
  border-radius: 0;
  height: 8px;
}
.legend-hint {
  color: var(--text-faint);
  font-style: italic;
  margin-left: auto;
}

/* ---- Footer ---- */
.app-footer {
  padding: 22px 24px;
  text-align: center;
}
.app-footer p {
  margin: 0;
  font-size: 13px;
  color: var(--text-faint);
  font-style: italic;
}

/* ---- Responsive ---- */
@media (max-width: 560px) {
  .header-inner {
    padding: 20px 20px 24px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
  }
  .global-toggle, .ghost-btn { position: static; order: 3; }
  .title-block { order: 1; margin-top: 8px; }
  .title-block h1 { font-size: 27px; }
  .sentence-row { padding: 16px; gap: 12px; }
  .row-index { min-width: 22px; }
}
