/* ---------------------------------------------------------------
   Say It — English speaking practice
   Sizes are deliberately large: the primary user is 65 and will be
   holding the phone at arm's length. Keep contrast high and avoid
   thin grey text anywhere.
   --------------------------------------------------------------- */

:root {
  --ink: #16202c;
  --ink-soft: #4a5a6b;
  --paper: #f4f6fa;
  --card: #ffffff;
  --blue: #1b4f9c;
  --blue-dark: #143c78;
  --green: #1a7a46;
  --green-dark: #146038;
  --red: #b3261e;
  --red-dark: #8c1d17;
  --amber: #8a5a00;
  --line: #d3dae5;

  /* Scaled by the text-size setting via [data-size] below. */
  --base: 20px;
  --phrase: 42px;

  --tap: 66px; /* minimum comfortable tap target */
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

[data-size="1"] { --base: 18px; --phrase: 34px; }
[data-size="2"] { --base: 20px; --phrase: 42px; }
[data-size="3"] { --base: 23px; --phrase: 52px; }
[data-size="4"] { --base: 26px; --phrase: 62px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Must win over the `display` values set by .sheet and friends below,
   otherwise the HTML `hidden` attribute is silently ignored and the
   panels sit invisibly on top of the page swallowing every tap. */
[hidden] { display: none !important; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: var(--base);
  line-height: 1.45;
  color: var(--ink);
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
}

/* ---------------- Screens ---------------- */

.screen {
  display: none;
  flex-direction: column;
  height: 100%;
}
.screen.is-active { display: flex; }

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 10px) 12px 10px;
  background: var(--blue);
  color: #fff;
  box-shadow: 0 2px 10px rgba(16, 32, 56, 0.18);
  flex: none;
}

.bar__title {
  font-size: 1.15em;
  font-weight: 700;
  margin: 0;
  flex: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.iconbtn {
  flex: none;
  width: 48px;
  height: 48px;
  font-size: 26px;
  line-height: 1;
  border: 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  cursor: pointer;
}
.iconbtn:active { background: rgba(255, 255, 255, 0.32); }

.counter {
  flex: none;
  min-width: 48px;
  text-align: right;
  font-size: 0.9em;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  padding-right: 4px;
}

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--safe-b) + 24px);
}

.lede {
  margin: 4px 2px 18px;
  font-size: 1.02em;
  color: var(--ink-soft);
}

.grouphead {
  margin: 30px 2px 0;
  padding-top: 20px;
  border-top: 2px solid var(--line);
  font-size: 1.1em;
  font-weight: 700;
}

.footnote {
  margin: 24px 2px 0;
  font-size: 0.88em;
  color: var(--ink-soft);
  border-top: 2px solid var(--line);
  padding-top: 14px;
}

/* ---------------- Lesson list ---------------- */

.lessons {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.lesson {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: var(--tap);
  padding: 16px;
  text-align: left;
  font-size: 1em;
  font-family: inherit;
  color: var(--ink);
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 18px;
  cursor: pointer;
}
.lesson:active { background: #e9eff9; border-color: var(--blue); }

.lesson__icon { font-size: 1.9em; flex: none; line-height: 1; }
.lesson__body { flex: 1; min-width: 0; }
.lesson__title { display: block; font-weight: 700; font-size: 1.06em; }
.lesson__meta { display: block; color: var(--ink-soft); font-size: 0.88em; margin-top: 2px; }
.lesson__chev { flex: none; font-size: 1.8em; color: var(--blue); line-height: 1; }

/* ---------------- Practice ---------------- */

.practice {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 14px 16px calc(var(--safe-b) + 16px);
  min-height: 0;
  /* The transcript panel appears and disappears, so this column has to be
     able to scroll rather than squeeze the buttons off the bottom. */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.progress {
  flex: none;
  height: 8px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
}
.progress__fill {
  height: 100%;
  width: 0;
  background: var(--green);
  border-radius: 99px;
  transition: width 0.25s ease;
}

.card {
  flex: none;
  min-height: 30vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  padding: 20px 18px;
  font-family: inherit;
  color: var(--ink);
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 3px 14px rgba(16, 32, 56, 0.07);
  cursor: pointer;
  overflow-y: auto;
  text-align: center;
}
.card:active { background: #f0f5ff; }
.card.is-speaking { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(27, 79, 156, 0.16); }

.card__text {
  font-size: var(--phrase);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.01em;
}

.card__hint {
  font-size: 1.02em;
  color: var(--ink-soft);
  max-width: 24ch;
}

.card__note {
  font-size: 1em;
  font-weight: 600;
  color: var(--green-dark);
  max-width: 24ch;
}
.card__note:empty { display: none; }

.card__tap {
  font-size: 0.8em;
  color: var(--ink-soft);
  opacity: 0.75;
  margin-top: 8px;
}

/* ---------------- Conversation ---------------- */

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

.chat__log {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 14px 8px;
}

.msg {
  max-width: 88%;
  padding: 12px 15px;
  border-radius: 20px;
  /* The learner reads these, so they track the text-size setting rather
     than sitting at a fixed chat-app size. */
  font-size: 1.08em;
  line-height: 1.35;
}

.msg--bot {
  align-self: flex-start;
  background: var(--card);
  border: 2px solid var(--line);
  border-bottom-left-radius: 6px;
}

.msg--you {
  align-self: flex-end;
  background: var(--blue);
  border: 2px solid var(--blue);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.msg__text { margin: 0; font-weight: 600; }

.msg__vi {
  margin: 8px 0 0;
  font-size: 0.92em;
  font-weight: 600;
  color: var(--green-dark);
}

.msg__tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.chip {
  font-family: inherit;
  font-size: 0.82em;
  font-weight: 700;
  padding: 8px 12px;
  min-height: 40px;
  color: var(--blue);
  background: #eef3fb;
  border: 2px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}
.chip:active { background: #dee8f7; }
.chip.is-on { background: var(--blue); border-color: var(--blue); color: #fff; }

.msg--speaking { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(27, 79, 156, 0.16); }

/* Three pulsing dots while we wait on the model. Without this the app looks
   broken for the couple of seconds a reply takes. */
.dots { display: inline-flex; gap: 6px; padding: 4px 2px; }
.dots i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--ink-soft);
  animation: blink 1.1s infinite ease-in-out;
}
.dots i:nth-child(2) { animation-delay: 0.18s; }
.dots i:nth-child(3) { animation-delay: 0.36s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

.chat__foot {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px calc(var(--safe-b) + 12px);
  background: var(--card);
  border-top: 2px solid var(--line);
}

.chat__typerow {
  display: flex;
  gap: 8px;
}

.chat__text {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 1em;
  padding: 0 14px;
  min-height: var(--tap);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 16px;
}
.chat__text:focus { outline: none; border-color: var(--blue); }

.chat__send { flex: none; min-width: 104px; }

.chat__switch {
  font-family: inherit;
  font-size: 0.9em;
  font-weight: 600;
  padding: 8px;
  color: var(--blue);
  background: none;
  border: 0;
  cursor: pointer;
  text-decoration: underline;
}

/* ---------------- Buttons ---------------- */

.actions {
  flex: none;
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 10px;
}

.nav {
  flex: none;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 12px 14px;
  font-family: inherit;
  font-size: 1.05em;
  font-weight: 700;
  border: 2px solid transparent;
  border-radius: 16px;
  cursor: pointer;
}
.btn:disabled { opacity: 0.4; }
.btn__icon { font-size: 1.2em; }

.btn--primary { background: var(--blue); color: #fff; }
.btn--primary:active:not(:disabled) { background: var(--blue-dark); }

.btn--slow { background: var(--green); color: #fff; }
.btn--slow:active:not(:disabled) { background: var(--green-dark); }

.btn--ghost {
  background: var(--card);
  color: var(--blue);
  border-color: var(--line);
}
.btn--ghost:active:not(:disabled) { background: #e9eff9; }

/* ---------------- Microphone ---------------- */

.btn--mic {
  flex: none;
  background: var(--card);
  color: var(--red);
  border-color: var(--red);
}
.btn--mic:active:not(:disabled) { background: #fdecea; }

.btn--mic.is-recording {
  color: #fff;
  background: var(--red);
  border-color: var(--red);
  animation: pulse 1.4s ease-in-out infinite;
}

.btn--mic.is-busy {
  color: var(--ink-soft);
  background: var(--card);
  border-color: var(--line);
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(179, 38, 30, 0.45); }
  50%      { box-shadow: 0 0 0 10px rgba(179, 38, 30, 0); }
}

/* ---------------- Transcript result ---------------- */

.result {
  flex: none;
  padding: 14px 16px 16px;
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 18px;
}
.result.is-perfect { border-color: var(--green); background: #f0f9f3; }

.result__head {
  margin: 0 0 8px;
  font-size: 0.9em;
  font-weight: 700;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.result__said {
  margin: 0;
  font-size: 1.35em;
  font-weight: 700;
  line-height: 1.35;
}

.result__note {
  margin: 10px 0 0;
  font-size: 0.95em;
  font-weight: 600;
  color: var(--ink-soft);
}
.result__note:empty { display: none; }

/* Word-level marking. Colour alone is not enough — underline style carries
   the same information for anyone who cannot separate red from green. */
.w { }
.w--match { color: var(--green-dark); }
.w--wrong {
  color: var(--red);
  text-decoration: underline wavy var(--red);
  text-underline-offset: 4px;
}
.w--extra {
  color: var(--amber);
  text-decoration: line-through;
}
.w--missed {
  color: var(--ink-soft);
  text-decoration: underline dotted var(--ink-soft);
  text-underline-offset: 4px;
}

.playall {
  flex: none;
  min-height: 54px;
  font-family: inherit;
  font-size: 0.98em;
  font-weight: 600;
  color: var(--blue);
  background: transparent;
  border: 2px dashed var(--line);
  border-radius: 16px;
  cursor: pointer;
}
.playall:active { background: #e9eff9; }
.playall.is-playing { color: #fff; background: var(--green); border-style: solid; border-color: var(--green); }

/* ---------------- Settings sheet ---------------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  background: rgba(16, 32, 56, 0.45);
}

.sheet__panel {
  width: 100%;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  border-radius: 22px 22px 0 0;
}

.sheet__head {
  display: flex;
  align-items: center;
  padding: 14px 14px 10px 20px;
  border-bottom: 2px solid var(--line);
}
.sheet__head h2 { margin: 0; flex: 1; font-size: 1.2em; }

.sheet__body {
  padding: 18px 20px calc(var(--safe-b) + 28px);
  overflow-y: auto;
  display: grid;
  /* minmax(0, 1fr) rather than the implicit `auto`: a bare `auto` track
     sizes to max-content, and the voice <select> is as wide as its longest
     option name, which drags the whole sheet off the right of the screen. */
  grid-template-columns: minmax(0, 1fr);
  gap: 22px;
}

.field { display: grid; gap: 10px; min-width: 0; }
.field__label { font-weight: 700; }

.select {
  width: 100%;
  min-width: 0;
  min-height: var(--tap);
  padding: 10px 14px;
  font-family: inherit;
  font-size: 1em;
  color: var(--ink);
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 14px;
}

input[type="range"] {
  width: 100%;
  height: 40px;
  accent-color: var(--blue);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.98em;
  cursor: pointer;
}
.toggle input {
  flex: none;
  width: 30px;
  height: 30px;
  accent-color: var(--blue);
}

/* ---------------- Toast ---------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 24px);
  transform: translateX(-50%);
  z-index: 90;
  max-width: 88%;
  padding: 14px 20px;
  font-size: 0.95em;
  font-weight: 600;
  text-align: center;
  color: #fff;
  background: #16202c;
  border-radius: 14px;
  box-shadow: 0 6px 20px rgba(16, 32, 56, 0.3);
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
