:root {
  --bg: #f6f3ee;
  --panel: #ffffff;
  --ink: #2b2a28;
  --muted: #6d6a64;
  --accent: #2f6f62;
  --accent-ink: #ffffff;
  --user: #e6efe9;
  --bot: #f1ece4;
  --line: #e2ddd4;
  --warn: #b3412f;
  --stage: linear-gradient(180deg, #dfe9e4 0%, #f3efe7 100%);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1f1e;
    --panel: #262928;
    --ink: #ecebe8;
    --muted: #a8a59f;
    --accent: #5fb39f;
    --accent-ink: #10201c;
    --user: #2f3d37;
    --bot: #343230;
    --line: #3a3d3b;
    --warn: #ff8a73;
    --stage: linear-gradient(180deg, #2a3532 0%, #1f2221 100%);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 18px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
}
h1 { font-size: 22px; margin: 0; font-weight: 650; }

.status { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 17px; }
.status .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); }
.status.listening .dot { background: #2e9d5b; }
.status.hearing .dot { background: #2e9d5b; animation: pulse 0.8s infinite; }
.status.thinking .dot { background: #d29a22; animation: pulse 1.2s infinite; }
.status.speaking .dot { background: var(--accent); }
.status.muted .dot { background: var(--warn); }
@keyframes pulse { 50% { opacity: 0.3; transform: scale(1.3); } }

main { flex: 1; display: flex; min-height: 0; }

#stage { position: relative; flex: 1.4; background: var(--stage); min-height: 300px; }
#avatar { position: absolute; inset: 0; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
#subtitle {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  max-width: min(90%, 820px); padding: 8px 16px; border-radius: 12px;
  background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 22px; text-align: center;
  display: none;
}

#side {
  flex: 1; display: flex; flex-direction: column; min-width: 320px; max-width: 560px;
  background: var(--panel); border-left: 1px solid var(--line);
}
#voicepanel {
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--bg);
  display: flex; flex-direction: column; gap: 8px; font-size: 16px;
}
#voicepanel.hidden { display: none; }
#voicepanel h3 { margin: 0; font-size: 17px; }
#voicepanel label { display: flex; flex-direction: column; gap: 3px; }
#voicepanel label small { color: var(--muted); font-size: 13px; }
#voicepanel select { font: inherit; font-size: 15px; padding: 6px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); }
#voicepanel input[type=range] { width: 100%; accent-color: var(--accent); }
#voicepanel #vp-chatterbox, #voicepanel #vp-piper { display: flex; flex-direction: column; gap: 8px; }
.vp-buttons { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#vp-status { color: var(--muted); font-size: 14px; }
#voicebtn.active, #pause.active { border-color: var(--accent); color: var(--accent); }
#chat { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }

.msg { padding: 10px 14px; border-radius: 14px; max-width: 92%; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--user); }
.msg.bot { align-self: flex-start; background: var(--bot); }
.msg .tag { font-size: 13px; color: var(--muted); margin-right: 6px; text-transform: uppercase; }
.msg .tools { margin-top: 6px; display: flex; gap: 8px; }
.msg .tools button { font-size: 14px; padding: 4px 10px; }
.msg .pl { margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--line); color: var(--muted); }
.msg.error { align-self: center; color: var(--warn); background: transparent; font-size: 15px; }

#typebox { display: flex; gap: 8px; padding: 10px 16px; border-top: 1px solid var(--line); }
#typebox input {
  flex: 1; font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}

button {
  font: inherit; font-size: 16px; padding: 9px 14px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}
button:hover { border-color: var(--accent); }
#send, #start { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
#controls { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 16px 10px; }
#mic.off { color: var(--warn); border-color: var(--warn); }

#meter { height: 5px; margin: 0 16px 12px; background: var(--line); border-radius: 3px; overflow: hidden; }
#meter-bar { height: 100%; width: 0; background: #2e9d5b; transition: width 80ms linear; }

#overlay { z-index: 50; position: fixed; inset: 0; background: rgba(20, 22, 21, 0.55); display: grid; place-items: center; padding: 16px; }
#overlay.hidden { display: none; }
.card { background: var(--panel); border-radius: 18px; padding: 28px; max-width: 520px; text-align: center; }
.card h2 { margin-top: 0; }
#start { font-size: 22px; padding: 12px 28px; }
.hint { color: var(--muted); font-size: 15px; }
.error { color: var(--warn); }

@media (max-width: 820px) {
  /* phone: avatar on top with a fixed height, the chat fills the rest and scrolls by itself
     (before, the whole page scrolled and new messages stayed out of view) */
  html, body { height: 100dvh; overflow: hidden; }
  main { flex-direction: column; min-height: 0; }
  #stage { flex: none; height: 36vh; min-height: 0; }
  #side { flex: 1; min-height: 0; max-width: none; min-width: 0; border-left: none; border-top: 1px solid var(--line); }
  #controls { gap: 6px; padding: 0 10px 8px; }
  #controls button { padding: 6px 9px; font-size: 14px; }
  #typebox { padding-top: 6px; padding-bottom: 6px; }
  #subtitle { font-size: 18px; }
}

/* photo-real video face */
/* video-call look (live face): face in a rounded tile on a dark background, as large as the height allows but
   never bigger than its real resolution (1024 px; ?size=<px> to change), so it is not blown up */
/* one column like a messenger video call: face on top, chat below, input + buttons at the bottom; on wide
   screens the column is centered instead of spreading face and chat over the whole width */
body.callmode main { flex-direction: column; width: min(100%, 860px); margin: 0 auto; min-height: 0; }
body.callmode #stage { flex: none; height: min(52vh, 620px); min-height: 0; background: transparent; }
body.callmode #side { flex: 1; min-height: 0; width: 100%; max-width: none; min-width: 0;
  border-left: none; background: transparent; }
.tile {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  height: min(calc(100% - 20px), var(--face-max, 1024px)); aspect-ratio: 1 / 1; max-width: calc(100% - 24px);
  border-radius: 18px; overflow: hidden; background: #000; box-shadow: 0 10px 34px rgba(0, 0, 0, 0.5);
}
.tile canvas.videoface { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile-label {
  position: absolute; left: 12px; bottom: 12px; display: flex; align-items: center; gap: 8px;
  padding: 5px 12px; border-radius: 10px; background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 16px;
}
.tile-label .dot { width: 10px; height: 10px; border-radius: 50%; background: #9aa0a6; }
.tile-label .dot.listening, .tile-label .dot.hearing { background: #2e9d5b; }
.tile-label .dot.speaking { background: #4f8df5; }
.tile-label .dot.thinking { background: #d29a22; }
.tile-label .dot.muted { background: #d9534f; }
body.callmode #subtitle { bottom: 28px; }

/* ================================================================ UI versions (?ui=...)
   call (default): the video-call column above. The others reuse the same parts:
   #captionbox (current sentence big), #phrases (review cards), #actionbar (big buttons). */
#captionbox, #phrases, #actionbar { display: none; }
body:is(.ui-chat, .ui-talk, .ui-captions, .ui-learn, .ui-big) main {
  flex-direction: column; width: min(100%, 860px); margin: 0 auto; min-height: 0;
}
body:is(.ui-chat, .ui-talk, .ui-captions, .ui-learn, .ui-big) #side { border-left: none; max-width: none; min-width: 0; }

/* big round action buttons (talk, captions, big, chat) */
#actionbar { gap: 12px; justify-content: center; padding: 10px 12px 14px; flex-wrap: wrap; }
.ab { display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 76px; padding: 10px 8px;
  border-radius: 18px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); font: inherit; }
.ab .ico { font-size: 30px; line-height: 1; }
.ab .lbl { font-size: 15px; }
.ab.active { border-color: var(--accent); color: var(--accent); }
.ab.off { border-color: var(--warn); color: var(--warn); }
.ab[disabled] { opacity: 0.5; }

/* --- chat: messenger. Small round face on top, chat is the main thing, big text */
body.ui-chat #stage { flex: none; height: 168px; background: transparent; }
body.ui-chat .tile { height: 150px; border-radius: 50%; }
body.ui-chat .tile-label { left: 50%; bottom: 6px; transform: translateX(-50%); font-size: 13px; padding: 2px 8px; }
body.ui-chat #side { flex: 1; min-height: 0; }
body.ui-chat .msg { font-size: 19px; }
body.ui-chat #actionbar { display: flex; order: 3; }
body.ui-chat #ab-text { display: none; }
body.ui-chat #controls #mic, body.ui-chat #controls #pause, body.ui-chat #controls #stop { display: none; }

/* --- talk: minimal. Big face, the current sentence in big letters, 4 big buttons; text history on demand */
body.ui-talk #stage { flex: none; height: min(50vh, 560px); background: transparent; }
body.ui-talk #captionbox, body.ui-captions #captionbox { display: flex; flex-direction: column; gap: 6px; }
body.ui-talk #captionbox { padding: 6px 20px; text-align: center; min-height: 3.2em; }
body.ui-talk #cap-tutor { font-size: 22px; line-height: 1.3; }
body.ui-talk #cap-user { font-size: 15px; color: var(--muted); }
body.ui-talk #cap-pl, body.ui-captions #cap-pl { font-size: 18px; color: var(--accent); }
body.ui-talk #actionbar, body.ui-captions #actionbar { display: flex; }
body.ui-talk #side, body.ui-captions #side { display: none; }
body.ui-talk.show-chat #side, body.ui-captions.show-chat #side { display: flex; flex: 1; min-height: 0; }
body.ui-talk #controls, body.ui-captions #controls { display: none; }

/* --- captions: face with big live subtitles over the bottom of the picture */
body.ui-captions #stage { flex: none; height: min(62vh, 680px); background: transparent; }
body.ui-captions #captionbox {
  position: relative; z-index: 2; margin: -120px auto 0; width: min(92%, 620px); padding: 10px 16px;
  background: rgba(0, 0, 0, 0.62); color: #fff; border-radius: 14px; text-align: center; min-height: 92px;
}
body.ui-captions #cap-tutor { font-size: 21px; line-height: 1.3; }
body.ui-captions #cap-user { font-size: 14px; color: #c9ced4; }
body.ui-captions #cap-pl { color: #9fe0c4; }
body.ui-captions #actionbar { margin-top: 10px; }

/* --- learn: face + chat, and a list of phrases to review (corrected sentences the tutor quoted) */
body.ui-learn main { display: grid; grid-template-columns: minmax(0, 1fr) 300px; grid-template-rows: auto minmax(0, 1fr);
  width: min(100%, 1200px); gap: 0 16px; }
body.ui-learn #stage { grid-column: 1; grid-row: 1; height: min(46vh, 520px); background: transparent; }
body.ui-learn #side { grid-column: 1; grid-row: 2; min-height: 0; }
body.ui-learn #phrases { display: block; grid-column: 2; grid-row: 1 / span 2; overflow-y: auto; padding: 14px 16px;
  background: var(--panel); border-left: 1px solid var(--line); }
#phrases h3 { margin: 0 0 10px; font-size: 18px; }
#phrase-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
#phrase-list li { padding: 10px 12px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); font-size: 17px; }
#phrase-list li.empty { color: var(--muted); font-size: 15px; border-style: dashed; }

/* --- big: large text, high contrast, only the 4 essential buttons */
body.ui-big { font-size: 20px; }
body.ui-big header h1 { font-size: 26px; }
body.ui-big .status { font-size: 20px; }
body.ui-big #stage { flex: none; height: min(42vh, 480px); background: transparent; }
body.ui-big #side { flex: 1; min-height: 0; }
body.ui-big .msg { font-size: 24px; line-height: 1.35; }
body.ui-big .msg.bot { background: #fff8e6; color: #111; border: 2px solid #c9a227; }
body.ui-big .msg.user { background: #e6f2ff; color: #111; border: 2px solid #3b7dd8; }
body.ui-big #text { font-size: 22px; }
body.ui-big #controls { display: none; }
body.ui-big #actionbar { display: flex; }
body.ui-big #ab-text { display: none; }
body.ui-big .ab { min-width: 120px; padding: 14px 10px; }
body.ui-big .ab .ico { font-size: 38px; }
body.ui-big .ab .lbl { font-size: 20px; font-weight: 600; }

@media (max-width: 820px) {
  body.ui-learn main { display: flex; flex-direction: column; }
  body.ui-learn #stage { height: 30vh; }
  body.ui-learn #phrases { display: block; flex: none; max-height: 18vh; border-left: none; border-top: 1px solid var(--line); }
  body.ui-learn #side { flex: 1; }
  body.ui-talk #stage { height: 38vh; }
  body.ui-captions #stage { height: 46vh; }
  body.ui-captions #captionbox { margin-top: -96px; min-height: 80px; }
  body.ui-big #stage { height: 30vh; }
  .ab { min-width: 64px; padding: 8px 6px; }
  .ab .ico { font-size: 26px; }
  body.ui-big .ab { min-width: 76px; }
  body.ui-big .ab .ico { font-size: 30px; }
  body.ui-big .ab .lbl { font-size: 16px; }
}
/* fixes after the screenshot check */
body.ui-captions .tile-label { top: 12px; bottom: auto; }
@media (max-width: 820px) {
  #actionbar { gap: 8px; padding: 8px 6px 10px; flex-wrap: nowrap; }
  .ab { min-width: 0; flex: 1 1 0; max-width: 84px; }
  .ab .lbl { font-size: 13px; white-space: nowrap; }
  body.ui-big .ab { min-width: 0; max-width: 92px; padding: 10px 4px; }
  body.ui-big .ab .lbl { font-size: 15px; }
}
body.ui-big #text { min-width: 0; flex: 1 1 auto; }
body.ui-big #send { flex: none; }
