:root {
  color: #172122;
  background: #f6f1e8;
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, sans-serif;
  font-synthesis: none;
  --ink: #172122;
  --muted: #66706d;
  --paper: #fffdf8;
  --line: #ded8cc;
  --green: #2f6b57;
  --green-dark: #184a3a;
  --orange: #e9753d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 8% 10%, rgba(233, 117, 61, 0.12), transparent 28rem),
    #f6f1e8;
  color: var(--ink);
}

button, input, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.app-shell { min-height: 100vh; }
.loading { padding: 3rem; color: var(--muted); }

.topbar {
  min-height: 58px;
  padding: 0 2vw;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  border-bottom: 1px solid rgba(23, 33, 34, 0.12);
  background: rgba(255, 253, 248, 0.82);
  backdrop-filter: blur(18px);
}

.brand {
  color: var(--green-dark);
  font-family: Georgia, serif;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.06em;
  text-decoration: none;
}

.theme-nav { display: flex; gap: 0.25rem; }
.theme-picker { display: none; }
.theme-nav a, .quiet-button {
  padding: 0.5rem 0.7rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  text-decoration: none;
  cursor: pointer;
}
.theme-nav a[aria-current="page"] { background: #dcebe4; color: var(--green-dark); font-weight: 700; }
.theme-nav a:hover, .quiet-button:hover { color: var(--ink); background: rgba(23, 33, 34, 0.06); }

.routebar {
  min-width: 0;
  width: min(410px, 31vw);
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.65rem;
}
.route-picker { min-width: 0; flex: 1; color: var(--muted); font-size: 0.75rem; font-weight: 700; }
.route-picker > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.route-picker select {
  width: 100%;
  padding: 0.55rem 2rem 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.route-progress { color: var(--muted); font-size: 0.72rem; white-space: nowrap; }

.player-layout {
  width: min(1500px, 94vw);
  margin: 0 auto;
  min-height: 0;
  height: calc(100svh - 58px);
  padding: 0.75rem 0 0.9rem;
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(320px, 0.7fr);
  gap: clamp(1.5rem, 3vw, 3.5rem);
  align-items: stretch;
}

.visual-panel {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 0.78fr);
  grid-template-rows: auto minmax(0, 1fr);
  column-gap: 1rem;
  align-content: start;
}
.episode-heading { min-width: 0; display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin-bottom: 0.65rem; }
.eyebrow { margin: 0 0 0.35rem; color: var(--orange); font-size: 0.75rem; font-weight: 800; letter-spacing: 0.13em; text-transform: uppercase; }
h1 { margin: 0; font-family: Georgia, serif; font-size: clamp(1.65rem, 2.5vw, 2.65rem); font-weight: 500; letter-spacing: -0.04em; line-height: 0.96; }
.episode-code { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.8rem; }

.episode-navigation {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 0.6rem;
  align-items: end;
  align-self: end;
  margin-bottom: 0.65rem;
}
.episode-navigation[hidden] { display: none; }
.episode-picker { display: grid; gap: 0.3rem; color: var(--muted); font-size: 0.75rem; font-weight: 700; }
.episode-picker select {
  width: 100%;
  min-width: 0;
  padding: 0.55rem 2rem 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.episode-arrow {
  display: grid;
  place-items: center;
  width: 2.45rem;
  height: 2.45rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--paper);
  color: var(--green-dark);
  font-size: 1.2rem;
  font-weight: 800;
  text-decoration: none;
}
.episode-arrow:hover { background: #dcebe4; }
.episode-arrow.is-disabled { opacity: 0.32; pointer-events: none; }

.canvas-frame {
  grid-column: 1 / -1;
  justify-self: center;
  width: min(100%, calc((100svh - 155px) * 4 / 3));
  padding: clamp(0.45rem, 1vw, 0.8rem);
  border: 1px solid rgba(23, 33, 34, 0.15);
  border-radius: 1.1rem;
  background: #e9e2d7;
  box-shadow: 0 28px 60px rgba(39, 44, 39, 0.15);
}
.master-canvas { position: relative; overflow: hidden; width: 100%; aspect-ratio: 4 / 3; background: #fff; border-radius: 0.65rem; }
.stage-layer { position: absolute; overflow: visible; user-select: none; pointer-events: none; }
.stage-image { position: absolute; left: 0; top: 0; width: 100%; height: auto; max-width: none; transform-origin: 0 0; }

.lesson-panel {
  min-height: 0;
  max-height: calc(100svh - 82px);
  margin-top: 0;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(23, 33, 34, 0.12);
  border-radius: 1.25rem;
  background: rgba(255, 253, 248, 0.92);
  box-shadow: 0 18px 50px rgba(39, 44, 39, 0.08);
}
.lesson-tabs { display: grid; grid-template-columns: repeat(3, 1fr); padding: 0.25rem; border-radius: 0.8rem; background: #eee8dd; }
.lesson-tabs button { padding: 0.65rem 0.4rem; border: 0; border-radius: 0.6rem; background: transparent; color: var(--muted); cursor: pointer; }
.lesson-tabs button[aria-selected="true"] { background: var(--paper); color: var(--green-dark); font-weight: 750; box-shadow: 0 2px 8px rgba(23, 33, 34, 0.1); }

audio { width: 100%; height: 42px; margin: 0.8rem 0 0.45rem; accent-color: var(--green); }
.login-form button {
  padding: 0.8rem 1rem;
  border: 0;
  border-radius: 0.75rem;
  background: var(--green);
  color: white;
  font-weight: 750;
  cursor: pointer;
}
.login-form button:hover { background: var(--green-dark); }

.transcript { min-height: 0; flex: 1; overflow-y: auto; margin: 0.75rem -0.3rem -0.3rem; padding: 0 0.3rem; list-style: none; }
.transcript li + li { border-top: 1px solid var(--line); }
.transcript button { width: 100%; padding: 0.48rem 0.5rem; border: 0; border-radius: 0.45rem; background: transparent; color: var(--muted); font-size: 0.82rem; line-height: 1.25; text-align: left; cursor: pointer; }
.transcript button[aria-current="true"] { color: var(--ink); font-weight: 700; }
.transcript button[aria-current="true"] .subtitle-text { background: #f5eadf; box-shadow: inset 3px 0 var(--orange); }
.subtitle-line { display: block; }
.subtitle-line + .subtitle-line { margin-top: 0.24rem; }
.subtitle-line.starts-speaker:not(:first-child) { margin-top: 0.6rem; padding-top: 0.45rem; border-top: 1px solid rgba(23, 33, 34, 0.1); }
.subtitle-role { display: block; margin-bottom: 0.2rem; color: var(--orange); font-weight: 800; }
.subtitle-text { display: block; width: fit-content; max-width: calc(100% - 1rem); margin-left: 1rem; padding: 0.12rem 0.45rem; border-radius: 0.35rem; }
.subtitle-line:not(.has-speaker) > .subtitle-text { max-width: 100%; margin-left: 0; }

.login-body { display: grid; place-items: center; padding: 1.5rem; }
.login-card, .error-card {
  width: min(440px, 100%);
  padding: clamp(2rem, 5vw, 3.5rem);
  border: 1px solid rgba(23, 33, 34, 0.12);
  border-radius: 1.5rem;
  background: rgba(255, 253, 248, 0.95);
  box-shadow: 0 28px 80px rgba(39, 44, 39, 0.14);
}
.brand-mark { display: grid; place-items: center; width: 3rem; height: 3rem; margin-bottom: 1.5rem; border-radius: 50%; background: var(--green); color: white; font: 700 1.5rem Georgia, serif; }
.login-card h1, .error-card h1 { font-size: 2.5rem; line-height: 1; }
.login-copy { margin: 1rem 0 1.5rem; color: var(--muted); line-height: 1.5; }
.login-error { padding: 0.75rem; border-radius: 0.7rem; background: #fce5dc; color: #8b351c; }
.login-form { display: grid; gap: 1rem; }
.login-form label { display: grid; gap: 0.4rem; color: var(--muted); font-size: 0.85rem; font-weight: 650; }
.login-form input { width: 100%; padding: 0.8rem; border: 1px solid var(--line); border-radius: 0.65rem; background: white; color: var(--ink); outline: none; }
.login-form input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(47, 107, 87, 0.15); }
.error-card { margin: 10vh auto; }

@media (max-width: 1100px) {
  .route-progress { display: none; }
}

@media (max-width: 960px) {
  .topbar { flex-wrap: wrap; padding-block: 0.55rem; }
  .theme-nav { order: 3; width: 100%; overflow-x: auto; }
  .routebar { width: min(420px, 46vw); }
  .player-layout { height: auto; grid-template-columns: 1fr; padding-top: 1rem; }
  .visual-panel { display: block; }
  .lesson-panel { margin-top: 0; }
  .transcript { max-height: none; }
}

@media (max-width: 560px) {
  .topbar { min-height: auto; padding: 0.65rem 1rem; gap: 0.5rem; flex-wrap: wrap; }
  .brand { font-size: 1.65rem; }
  .theme-nav a, .quiet-button { padding: 0.5rem 0.6rem; font-size: 0.85rem; white-space: nowrap; }
  .routebar { order: 4; width: 100%; }
  .route-progress { display: none; }
  .player-layout { width: min(100% - 1rem, 1500px); }
  .episode-heading { align-items: start; }
  .episode-code { display: none; }
  .lesson-panel { padding: 1rem; }
}

@media (orientation: landscape) and (max-width: 1000px) and (max-height: 600px) {
  html, body:not(.login-body) {
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }

  body:not(.login-body) {
    background: #fff;
  }

  .app-shell {
    width: 100%;
    height: var(--keba-visible-height, 100dvh);
    min-height: 0;
    padding-left: env(safe-area-inset-left);
    display: grid;
    grid-template-columns: calc(var(--keba-visible-height, 100dvh) * 4 / 3) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    overflow: hidden;
    background: var(--paper);
  }

  .topbar {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    min-height: 0;
    padding: 0.3rem max(0.4rem, env(safe-area-inset-right)) 0.3rem 0.4rem;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 0.25rem;
    border-bottom: 1px solid var(--line);
    background: var(--paper);
    backdrop-filter: none;
  }

  .brand {
    align-self: center;
    font-size: 1.15rem;
  }

  .theme-nav { display: none; }
  .theme-picker {
    min-width: 0;
    display: block;
  }
  .theme-picker > span,
  .route-picker > span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }
  .theme-picker select,
  .route-picker select {
    width: 100%;
    min-width: 0;
    height: 1.8rem;
    padding: 0.2rem 1.35rem 0.2rem 0.4rem;
    border: 1px solid var(--line);
    border-radius: 0.45rem;
    background: var(--paper);
    color: var(--ink);
    font-size: 0.68rem;
    font-weight: 700;
  }

  .routebar {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    min-width: 0;
    margin: 0;
    display: block;
  }
  .route-progress { display: none; }
  .quiet-button {
    width: 1.8rem;
    height: 1.8rem;
    padding: 0;
    overflow: hidden;
    color: transparent;
    font-size: 0;
  }
  .quiet-button::before {
    content: "×";
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    color: var(--muted);
    font-size: 1rem;
  }

  .player-layout,
  .visual-panel {
    display: contents;
  }

  .episode-heading { display: none; }

  .episode-navigation {
    grid-column: 2;
    grid-row: 2;
    min-width: 0;
    margin: 0;
    padding: 0.28rem max(0.4rem, env(safe-area-inset-right)) 0.28rem 0.4rem;
    align-self: stretch;
    align-items: end;
    gap: 0.25rem;
    border-bottom: 1px solid var(--line);
    background: var(--paper);
  }
  .episode-picker { gap: 0.1rem; font-size: 0.6rem; }
  .episode-picker select {
    height: 1.8rem;
    padding: 0.2rem 1.35rem 0.2rem 0.4rem;
    border-radius: 0.45rem;
    font-size: 0.68rem;
  }
  .episode-arrow {
    width: 1.8rem;
    height: 1.8rem;
    border-radius: 0.45rem;
    font-size: 0.85rem;
  }

  .canvas-frame {
    grid-column: 1;
    grid-row: 1 / -1;
    align-self: stretch;
    justify-self: stretch;
    width: calc(var(--keba-visible-height, 100dvh) * 4 / 3);
    height: var(--keba-visible-height, 100dvh);
    padding: 0;
    border: 0;
    border-radius: 0;
    background: #fff;
    box-shadow: none;
  }
  .master-canvas {
    width: 100%;
    height: 100%;
    aspect-ratio: 4 / 3;
    border-radius: 0;
  }

  .lesson-panel {
    grid-column: 2;
    grid-row: 3;
    min-width: 0;
    min-height: 0;
    max-height: none;
    margin: 0;
    padding: 0.35rem max(0.4rem, env(safe-area-inset-right)) 0.25rem 0.4rem;
    overflow: hidden;
    border: 0;
    border-radius: 0;
    background: var(--paper);
    box-shadow: none;
  }
  .lesson-tabs { padding: 0.14rem; border-radius: 0.45rem; }
  .lesson-tabs button {
    min-width: 0;
    padding: 0.35rem 0.15rem;
    overflow: hidden;
    font-size: 0.65rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  audio {
    height: 30px;
    margin: 0.28rem 0 0.12rem;
  }
  .transcript {
    margin: 0.18rem -0.15rem 0;
    padding: 0 0.15rem;
    scrollbar-width: thin;
  }
  .transcript button {
    padding: 0.26rem 0.25rem;
    font-size: 0.68rem;
    line-height: 1.14;
  }
  .subtitle-line + .subtitle-line { margin-top: 0.12rem; }
  .subtitle-line.starts-speaker:not(:first-child) { margin-top: 0.25rem; padding-top: 0.2rem; }
  .subtitle-role { margin-bottom: 0.06rem; }
  .subtitle-text {
    max-width: calc(100% - 0.55rem);
    margin-left: 0.55rem;
    padding: 0.08rem 0.22rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
