:root {
  --primary: #0e9aab;
  --primary-strong: #0a7f8f;
  --primary-weak: #e6f7f9;
  --primary-line: #a8e0e7;
  --ink: #1b2430;
  --muted: #667282;
  --line: #e2e8ee;
  --bg: #f3f6f8;
  --card: #fff;
  --danger: #cf3b4a;
  --radius: 16px;
  --text-scale: 1;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans JP", "Hiragino Sans", "Yu Gothic UI", Meiryo, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

[hidden] { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: default; }

:focus-visible { outline: 3px solid var(--primary-line); outline-offset: 2px; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 24px; height: 24px; fill: currentColor; flex: none; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg);
  position: relative;
}

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---------- 共通パーツ ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: calc(6px + var(--safe-top)) 8px 6px 12px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.topbar-title {
  flex: 1;
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
.icon-btn:active { background: rgba(0, 0, 0, 0.06); }
.icon-btn-small { width: 36px; height: 36px; }
.icon-btn-small .icon { width: 20px; height: 20px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 48px;
  padding: 0 20px;
  border: 1.5px solid transparent;
  border-radius: 12px;
  font-weight: 700;
  font-size: 1rem;
}
.btn-compact { min-height: 40px; padding: 0 14px 0 10px; font-size: 0.9rem; }
.btn-compact .icon { width: 20px; height: 20px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary-strong); color: #fff; }
.btn-primary:active { background: #086978; }
.btn-secondary { background: var(--primary-weak); color: var(--primary-strong); border-color: var(--primary-line); }
.btn-ghost { background: #fff; color: var(--ink); border-color: var(--line); }
.btn-danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: 0.45; }

/* ---------- 選ぶ（レッスン一覧） ---------- */

.brand-mark { border-radius: 9px; }
.brand-name { flex: 1; margin: 0; font-size: 1.15rem; font-weight: 800; letter-spacing: 0.01em; color: #1d2a4a; }

.home-body { flex: 1; overflow-y: auto; padding: 8px 16px calc(24px + var(--safe-bottom)); }

.section-title { margin: 20px 2px 10px; font-size: 0.9rem; font-weight: 700; color: var(--muted); }

.lesson-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; }

.lesson {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.1);
}
.lesson-link { display: block; color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
.lesson-link:active .lesson-thumb { filter: brightness(0.92); }
.lesson-thumb {
  display: grid;
  place-items: center;
  height: 88px;
  color: #fff;
  background: linear-gradient(135deg, #38cfdc 0%, var(--primary) 55%, #0a7f8f 100%);
}
.lesson-thumb .icon { width: 44px; height: 44px; opacity: 0.95; }
.lesson-body { padding: 12px 16px 14px; }
.lesson-tag {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--primary-weak);
  color: var(--primary-strong);
  font-size: 0.72rem;
  font-weight: 700;
}
.lesson-title { margin: 6px 0 2px; font-size: 1.05rem; line-height: 1.4; overflow-wrap: anywhere; }
.lesson-meta { margin: 0; color: var(--muted); font-size: 0.85rem; }
.lesson-resume { display: block; margin-top: 2px; color: var(--primary-strong); font-weight: 600; }
.lesson-delete { position: absolute; top: 8px; right: 8px; color: #fff; background: rgba(0, 0, 0, 0.22); }
.lesson-delete:active { background: rgba(0, 0, 0, 0.4); }

.empty {
  padding: 22px 18px;
  border: 2px dashed var(--primary-line);
  border-radius: var(--radius);
  background: #fff;
  text-align: center;
}
.empty p { margin: 0 0 14px; color: var(--muted); line-height: 1.6; text-wrap: balance; word-break: auto-phrase; }

/* ---------- 練習 ---------- */

.hint {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 12px 0;
  padding: 8px 6px 8px 12px;
  border: 1px solid #f1dc9c;
  border-radius: 12px;
  background: #fff8e2;
  color: #6a5000;
  font-size: 0.82rem;
  line-height: 1.5;
}
.hint p { flex: 1; margin: 0; }

.cards-wrap { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }

.cards {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 12px 45vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.card {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  column-gap: 8px;
  align-items: start;
}

.card-play {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--primary-strong);
}
.card-play .icon {
  width: 40px;
  height: 40px;
  padding: 8px;
  border-radius: 50%;
  background: var(--primary-weak);
}
.card-time { font-size: 0.68rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.card.is-active .card-play .icon { background: var(--primary); color: #fff; }

.card-flip { cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.card-flip:focus-visible { outline-offset: 3px; border-radius: var(--radius); }

/* 534 枚ぶんの 3D 変形を常時かけると重いので、静止時は表裏を visibility で切り替え、
   めくる瞬間（.is-flipping）だけ回転アニメーションで裏返して見せる。 */
.card-inner { display: grid; }
.card:not(.is-flipped) .face-back,
.card.is-flipped .face-front { visibility: hidden; }

.card.is-flipping .card-inner { animation: card-flip 0.4s linear; }
.card.is-flipping .face { transition: visibility 0s linear 0.2s; } /* 真横を向いた瞬間に面を入れ替える */
@keyframes card-flip {
  0% { transform: perspective(900px) rotateY(0deg); animation-timing-function: ease-in; }
  50% { transform: perspective(900px) rotateY(90deg); }
  50.01% { transform: perspective(900px) rotateY(-90deg); animation-timing-function: ease-out; }
  100% { transform: perspective(900px) rotateY(0deg); }
}

.face {
  grid-area: 1 / 1;
  padding: 14px 16px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
}
.face-back {
  border-color: var(--primary-line);
  background: var(--primary-weak);
}
.card.is-active .face { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(14, 154, 171, 0.16); }

.face-tag {
  display: inline-block;
  margin-bottom: 6px;
  padding: 1px 9px;
  border: 1px solid var(--primary-line);
  border-radius: 999px;
  background: #fff;
  color: var(--primary-strong);
  font-size: 0.72rem;
  font-weight: 700;
}

.face-text {
  margin: 0;
  font-size: calc(1.2rem * var(--text-scale));
  font-weight: 500;
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.face-back .face-text { font-size: calc(1.08rem * var(--text-scale)); line-height: 1.7; }
.face-text.is-empty { color: var(--muted); font-size: 0.9rem; }

/* 画面外に出た今の文へ戻るボタン */
.locate {
  position: absolute;
  left: 50%;
  bottom: 14px;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 9px 18px 9px 10px;
  border: 0;
  border-radius: 999px;
  background: var(--primary-strong);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 700;
  box-shadow: 0 4px 14px rgba(10, 127, 143, 0.4);
  transform: translateX(-50%);
}
.locate:active { background: var(--primary); }

/* 下のコントロール */

.panel {
  padding: 10px 16px calc(10px + var(--safe-bottom));
  background: #fff;
  border-top: 1px solid var(--line);
  box-shadow: 0 -6px 18px rgba(16, 24, 40, 0.06);
}

.seek { display: flex; align-items: center; gap: 10px; }
.time { min-width: 2.6em; color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.time:last-child { text-align: right; }

.seek-range {
  --progress: 0%;
  flex: 1;
  height: 32px;
  margin: 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
}
.seek-range::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, var(--primary) var(--progress), #dbe2e9 var(--progress));
}
.seek-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  margin-top: -8px;
  border: 3px solid var(--primary);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}
.seek-range::-moz-range-track { height: 6px; border-radius: 3px; background: #dbe2e9; }
.seek-range::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--primary); }
.seek-range::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 3px solid var(--primary);
  border-radius: 50%;
  background: #fff;
}

.speed { display: flex; gap: 6px; margin: 6px 0 8px; }
.speed button {
  flex: 1;
  min-height: 38px;
  padding: 0;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}
.speed button[aria-checked="true"] { border-color: var(--primary-strong); background: var(--primary-strong); color: #fff; }

.transport { display: flex; align-items: center; justify-content: space-between; }
.ctl {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
}
.ctl:active { background: rgba(0, 0, 0, 0.06); }
.ctl .icon { width: 28px; height: 28px; }
.ctl[aria-pressed="true"] { background: var(--primary-weak); color: var(--primary-strong); }
.ctl-main {
  width: 68px;
  height: 68px;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 6px 16px rgba(14, 154, 171, 0.35);
}
.ctl-main:active { background: var(--primary-strong); }
.ctl-main .icon { width: 38px; height: 38px; }

/* ---------- ダイアログ・トースト ---------- */

dialog { color: var(--ink); }

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  max-height: 92dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 22px 22px 0 0;
  background: #fff;
  box-shadow: 0 -8px 32px rgba(16, 24, 40, 0.25);
}
.sheet[open] { animation: sheet-in 0.22s ease-out; }
.sheet::backdrop, .modal::backdrop { background: rgba(16, 24, 40, 0.45); }
.sheet-body { display: grid; gap: 14px; padding: 20px 20px calc(20px + var(--safe-bottom)); }
.sheet-title { margin: 0; font-size: 1.15rem; }
.sheet-help { margin: 0; color: var(--muted); font-size: 0.9rem; line-height: 1.6; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; }
.sheet-actions .btn { flex: 1; }

@keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }

.modal {
  width: min(88vw, 360px);
  padding: 22px 20px 18px;
  border: 0;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 12px 40px rgba(16, 24, 40, 0.3);
}
.dlg-title { margin: 0 0 8px; font-size: 1.1rem; overflow-wrap: anywhere; }
.dlg-message { margin: 0 0 18px; color: var(--muted); line-height: 1.6; font-size: 0.92rem; }

.field { display: grid; gap: 8px; }
.field-label { font-size: 0.85rem; font-weight: 700; color: var(--muted); }
.row { display: flex; gap: 10px; }
.row .btn { flex: 1; min-height: 44px; font-size: 0.92rem; padding: 0 8px; }

.text-input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  font: inherit;
  font-size: 1rem;
}
.text-input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(14, 154, 171, 0.18); }

.segmented { display: flex; border: 1.5px solid var(--line); border-radius: 12px; overflow: hidden; }
.segmented button {
  flex: 1;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-right: 1.5px solid var(--line);
  background: #fff;
  font-weight: 700;
  color: var(--muted);
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-checked="true"] { background: var(--primary-strong); color: #fff; }

.file-status { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.file-status li {
  display: grid;
  grid-template-columns: 22px auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: #f6f8fa;
  font-size: 0.88rem;
}
.file-mark { position: relative; width: 20px; height: 20px; border: 2px solid #c3ccd6; border-radius: 50%; }
.file-kind { font-weight: 700; white-space: nowrap; }
.file-name { color: var(--muted); text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-status li.is-ready { background: var(--primary-weak); }
.file-status li.is-ready .file-mark { border-color: var(--primary); background: var(--primary); }
.file-status li.is-ready .file-mark::after {
  content: "";
  position: absolute;
  left: 4.5px;
  top: 1px;
  width: 5px;
  height: 9px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.file-status li.is-ready .file-name { color: var(--primary-strong); }

.note { margin: 0; min-height: 1.4em; color: var(--muted); font-size: 0.85rem; line-height: 1.6; }
.note.is-warn { color: #8a5a00; }

.toast {
  position: fixed;
  top: calc(64px + var(--safe-top));
  left: 50%;
  z-index: 10;
  width: max-content;
  max-width: min(92vw, 520px);
  padding: 11px 18px;
  border-radius: 999px;
  background: rgba(27, 36, 48, 0.94);
  color: #fff;
  font-size: 0.9rem;
  line-height: 1.4;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

.noscript { padding: 24px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  .card.is-flipping .card-inner { animation: none; }
  .card.is-flipping .face { transition: none; }
  .sheet[open] { animation: none; }
  .toast { transition: none; }
}
