:root {
  --bg: #0b1020;
  --bg-soft: #141b33;
  --bg-input: #0f1730;
  --line: #26304f;
  --text: #eef2ff;
  --muted: #93a0c4;
  --faint: #64708f;
  --accent: #38bdf8;
  --listen: #22c55e;
  --speak: #38bdf8;
  --danger: #f87171;
  --tap: 56px;
}

* { box-sizing: border-box; }

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh; /* die Handy-Adressleiste soll das Layout nicht stauchen */
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Drei Zeilen: Kopf, Dialog (waechst), Bedienung.
   Der Dialog ist der einzige Bereich, der scrollt. */
.screen {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---------- Kopfzeile ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 6px 16px;
  min-height: 52px;
}

.topbar-text {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-text strong {
  color: var(--text);
  font-weight: 650;
  font-size: 15px;
  margin-right: 8px;
}

.dot-sep { opacity: .45; margin: 0 3px; }

.conn {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #475569;
  flex: none;
  transition: background .2s;
}
.conn[data-state="connecting"] { background: #facc15; animation: pulse 1s infinite; }
.conn[data-state="live"] { background: var(--listen); }
.conn[data-state="error"] { background: var(--danger); }

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  flex: none;
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
  border-radius: 16px;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:active { background: var(--bg-soft); }
.icon-btn svg { width: 26px; height: 26px; fill: currentColor; }

/* ---------- Dialog ---------- */

.dialog {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 4px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}
.dialog::-webkit-scrollbar { width: 5px; }
.dialog::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

/* Begruessungsbildschirm, solange nichts gesprochen wurde */
.welcome {
  margin: auto 0;
  text-align: center;
  padding: 12px 8px 20px;
  animation: fade .4s ease-out;
}
.welcome-badge {
  width: 76px;
  height: 76px;
  margin: 0 auto 18px;
  border-radius: 26px;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, #1e3358, #131b33);
  box-shadow: inset 0 0 0 1px #2b3a63;
}
.welcome-badge svg { width: 38px; height: 38px; fill: var(--accent); }
.welcome h1 {
  margin: 0 0 10px;
  font-size: 27px;
  font-weight: 650;
  letter-spacing: -.01em;
}
.welcome p {
  margin: 0 auto;
  max-width: 30ch;
  font-size: 15px;
  line-height: 1.55;
  color: var(--muted);
}

/* Sprechblasen */
.msg {
  max-width: 84%;
  padding: 10px 14px 11px;
  border-radius: 20px;
  font-size: 16.5px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  animation: rise .18s ease-out;
  user-select: text;
}

.msg.teacher {
  align-self: flex-start;
  background: #16263e;
  color: #dcecff;
  border-bottom-left-radius: 7px;
}
.msg.me {
  align-self: flex-end;
  background: #262f4d;
  color: #dfe5ff;
  border-bottom-right-radius: 7px;
}

.msg-name {
  display: block;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--faint);
  margin-bottom: 4px;
}

/* ---------- Bedienleiste ---------- */

.controls {
  padding: 6px 14px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.status {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.4;
  min-height: 1.4em;
}
.status.error { color: var(--danger); }

.control-row {
  display: flex;
  justify-content: center;
}

.talk {
  position: relative;
  border: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 32%, #2b3a63, #161d38 72%);
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 0 0 2px var(--line), 0 16px 44px rgba(0, 0, 0, .5);
  touch-action: manipulation;
  transition: width .28s cubic-bezier(.4, 0, .2, 1),
              height .28s cubic-bezier(.4, 0, .2, 1),
              box-shadow .25s ease,
              background .25s ease;
}
.talk:active { filter: brightness(1.12); }

.talk-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  z-index: 2;
}
.talk-icon { fill: currentColor; transition: width .28s, height .28s; }
.talk-label { font-weight: 650; letter-spacing: .01em; transition: font-size .28s; }

.ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid transparent;
  opacity: 0;
  pointer-events: none;
}

/* Vor dem Start: der Button ist die Einladung und darf gross sein. */
.screen[data-state="idle"] .talk,
.screen[data-state="error"] .talk {
  width: min(58vw, 34vh, 260px);
  height: min(58vw, 34vh, 260px);
}
.screen[data-state="idle"] .talk-icon,
.screen[data-state="error"] .talk-icon { width: 52px; height: 52px; }
.screen[data-state="idle"] .talk-label,
.screen[data-state="error"] .talk-label { font-size: 21px; }

/* Im Gespraech gehoert der Platz dem Dialog: der Button wird zum Stoppknopf. */
.screen[data-state="active"] .talk {
  width: 84px;
  height: 84px;
}
.screen[data-state="active"] .talk-icon { width: 26px; height: 26px; }
.screen[data-state="active"] .talk-label { font-size: 12px; }

/* Zustandsfarben */
.talk[data-state="connecting"] {
  box-shadow: 0 0 0 2px #facc15, 0 16px 44px rgba(0, 0, 0, .5);
}
.talk[data-state="connecting"] .talk-label { color: #facc15; }

.talk[data-state="listening"] {
  background: radial-gradient(circle at 50% 32%, #14532d, #101c2e 72%);
  box-shadow: 0 0 0 3px var(--listen), 0 0 48px rgba(34, 197, 94, .3), 0 16px 44px rgba(0, 0, 0, .5);
}
.talk[data-state="speaking"] {
  background: radial-gradient(circle at 50% 32%, #0c4a6e, #101c2e 72%);
  box-shadow: 0 0 0 3px var(--speak), 0 0 48px rgba(56, 189, 248, .3), 0 16px 44px rgba(0, 0, 0, .5);
}
.talk[data-state="error"] {
  box-shadow: 0 0 0 3px var(--danger), 0 16px 44px rgba(0, 0, 0, .5);
}

.talk[data-state="listening"] .ring,
.talk[data-state="speaking"] .ring { opacity: 1; }
.talk[data-state="listening"] .ring { border-color: rgba(34, 197, 94, .5); animation: ripple 2.4s ease-out infinite; }
.talk[data-state="speaking"] .ring { border-color: rgba(56, 189, 248, .5); animation: ripple 1.5s ease-out infinite; }
.talk[data-state="listening"] .ring-2 { animation-delay: 1.2s; }
.talk[data-state="speaking"] .ring-2 { animation-delay: .75s; }

/* Schnelltasten erst zeigen, wenn sie etwas bewirken koennen. */
.quickbar {
  display: flex;
  gap: 8px;
  transition: opacity .2s;
}
.screen[data-state="idle"] .quickbar,
.screen[data-state="error"] .quickbar {
  display: none;
}

.quick {
  flex: 1;
  min-width: 0;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 16px;
  color: var(--text);
  font-size: 11.5px;
  font-weight: 550;
  white-space: nowrap;
  cursor: pointer;
}
.quick svg { width: 20px; height: 20px; fill: currentColor; }
.quick:disabled { opacity: .35; }
.quick:not(:disabled):active { background: #1d2745; }

/* ---------- Einstellungen ---------- */

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 20, .72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: flex-end;
  z-index: 20;
}
.sheet[hidden] { display: none; }

.sheet-panel {
  width: 100%;
  max-height: 92dvh;
  background: var(--bg-soft);
  border-radius: 26px 26px 0 0;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  animation: slideup .22s ease-out;
}

.sheet-head {
  display: flex;
  align-items: center;
  padding: 8px 8px 8px 22px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { flex: 1; margin: 0; font-size: 19px; font-weight: 650; }

.sheet-body {
  padding: 18px 20px calc(24px + env(safe-area-inset-bottom));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.field { display: flex; flex-direction: column; gap: 7px; }
.field-label { font-size: 14px; color: var(--muted); font-weight: 550; }
.field-hint { font-size: 12px; color: #6b7799; line-height: 1.45; }

.profile-box {
  background: var(--bg-input);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 14px;
  line-height: 1.5;
  color: #b9c6e6;
  max-height: 180px;
  overflow-y: auto;
  white-space: pre-wrap;
  user-select: text;
}

.field textarea,
.field input[type="text"],
.field input[type="password"],
.field select {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text);
  font-size: 16px; /* unter 16px zoomt iOS beim Fokus hinein */
  font-family: inherit;
  appearance: none;
}
.field input[type="text"],
.field input[type="password"],
.field select { min-height: 52px; padding-top: 0; padding-bottom: 0; }
.field textarea { min-height: 92px; line-height: 1.45; resize: vertical; }

.field select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2393a0c4'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 22px;
  padding-right: 40px;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.field-row {
  flex-direction: row;
  align-items: center;
  gap: 12px;
  font-size: 15px;
  min-height: var(--tap);
}
.field-row input[type="checkbox"] {
  width: 26px;
  height: 26px;
  accent-color: var(--accent);
  flex: none;
}

.sheet-actions { display: flex; gap: 12px; margin-top: 4px; }

.btn-primary, .btn-ghost {
  flex: 1;
  min-height: 54px;
  border-radius: 16px;
  font-size: 16px;
  font-weight: 650;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
}
.btn-primary { background: var(--accent); color: #04121f; border-color: transparent; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-primary:active { filter: brightness(.9); }

.version { text-align: center; font-size: 12px; color: #566082; margin: 4px 0 0; }

/* ---------- Bewegung ---------- */

@keyframes ripple {
  0%   { transform: scale(1);    opacity: .55; }
  100% { transform: scale(1.34); opacity: 0; }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .3; }
}
@keyframes slideup {
  from { transform: translateY(12%); opacity: 0; }
  to   { transform: translateY(0);   opacity: 1; }
}
@keyframes rise {
  from { transform: translateY(6px); opacity: 0; }
  to   { transform: translateY(0);   opacity: 1; }
}
@keyframes fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

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

/* Kleine Geraete: etwas kompakter, damit alles ohne Scrollen passt. */
@media (max-height: 700px) {
  .welcome-badge { width: 62px; height: 62px; margin-bottom: 12px; }
  .welcome h1 { font-size: 23px; }
  .screen[data-state="idle"] .talk { width: min(50vw, 30vh, 210px); height: min(50vw, 30vh, 210px); }
}

/* Querformat im Auto: Dialog neben die Bedienung statt darunter. */
@media (orientation: landscape) and (max-height: 560px) {
  .screen {
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-columns: minmax(0, 1fr) 210px;
  }
  .topbar { grid-column: 1 / -1; }
  .controls {
    padding: 0 12px calc(8px + env(safe-area-inset-bottom));
    justify-content: center;
  }
  .quickbar { flex-wrap: wrap; }
  .quick { flex: 1 1 44%; min-height: 46px; }
  .screen[data-state="idle"] .talk,
  .screen[data-state="error"] .talk { width: min(30vh, 150px); height: min(30vh, 150px); }
  .screen[data-state="idle"] .talk-icon,
  .screen[data-state="error"] .talk-icon { width: 34px; height: 34px; }
  .screen[data-state="idle"] .talk-label,
  .screen[data-state="error"] .talk-label { font-size: 16px; }
  .screen[data-state="active"] .talk { width: 68px; height: 68px; }
  .welcome-badge { display: none; }
}
