:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3250;
  --accent: #6c63ff;
  --accent-dim: #4a43b3;
  --accent-glow: rgba(108, 99, 255, 0.25);
  --danger: #ff4f4f;
  --success: #4caf88;
  --text: #e8eaf0;
  --text-dim: #8b90a8;
  --ai-bubble: #1e2235;
  --user-bubble: #1e2a1e;
  --radius: 12px;
  --font: 'Inter', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── Layout ── */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 800px;
  margin: 0 auto;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

header h1 span { color: var(--accent); }

.header-actions { display: flex; gap: 8px; }

/* ── Tabs ── */
#tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab-btn {
  flex: 1;
  padding: 10px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ── Panels ── */
.panel { display: none; flex: 1; overflow: hidden; }
.panel.active { display: flex; flex-direction: column; }

/* ── Session panel ── */
#session-panel {
  position: relative;
}

#transcript {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.transcript-empty {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
}

.transcript-empty .icon { font-size: 2.5rem; margin-bottom: 8px; }

.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.6;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}

.msg.ai   { background: var(--ai-bubble); align-self: flex-start; border-bottom-left-radius: 3px; }
.msg.user { background: var(--user-bubble); align-self: flex-end; border-bottom-right-radius: 3px; }

.msg .label {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 4px;
  opacity: 0.55;
}

.msg.ai   .label { color: var(--accent); }
.msg.user .label { color: var(--success); }

.msg.system-msg {
  background: transparent;
  align-self: center;
  color: var(--text-dim);
  font-size: 0.78rem;
  font-style: italic;
  padding: 4px 0;
}

/* ── Controls bar ── */
#controls {
  padding: 14px 16px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
}

#status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-dim);
}

#status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  flex-shrink: 0;
  transition: background 0.3s;
}

#status-dot.connected { background: var(--success); box-shadow: 0 0 6px var(--success); }
#status-dot.listening { background: var(--accent); box-shadow: 0 0 6px var(--accent); animation: pulse 1s infinite; }
#status-dot.speaking  { background: #f0a500; box-shadow: 0 0 6px #f0a500; animation: pulse 0.6s infinite; }
#status-dot.error     { background: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

#btn-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ── Visualizer ── */
#visualizer-wrap {
  flex: 1;
  height: 36px;
  display: flex;
  align-items: center;
}

#visualizer {
  width: 100%;
  height: 36px;
  border-radius: 6px;
  opacity: 0.7;
}

/* ── Buttons ── */
.btn {
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:not(:disabled):hover { background: #7d75ff; }

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:not(:disabled):hover { background: #ff6b6b; }

.btn-ghost {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:not(:disabled):hover { background: var(--border); }

.btn-icon {
  padding: 8px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-size: 1.1rem;
}

/* ── Settings panel ── */
#settings-panel {
  overflow-y: auto;
  padding: 20px;
}

.settings-section {
  margin-bottom: 28px;
}

/* ── Settings session notice ── */
#settings-session-notice {
  display: none;
  margin: 0 0 4px;
  padding: 8px 14px;
  background: rgba(240, 165, 0, 0.1);
  border-left: 3px solid #f0a500;
  border-radius: 0 6px 6px 0;
  font-size: 0.8rem;
  color: #f0a500;
}

#settings-session-notice.visible { display: block; }

/* ── Provider toggle ── */
.provider-toggle {
  display: flex;
  gap: 10px;
}

.provider-option {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 0;
  cursor: pointer;
}

.provider-option input[type="radio"] { display: none; }

.provider-option span {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.15s;
  text-align: center;
}

.provider-option span small {
  font-size: 0.68rem;
  font-weight: 400;
  color: var(--text-dim);
  margin-top: 3px;
}

.provider-option input:checked + span {
  border-color: var(--accent);
  background: rgba(108, 99, 255, 0.12);
  color: var(--accent);
}

.provider-option input:checked + span small { color: var(--accent); opacity: 0.7; }

.settings-section h2 {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 14px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}

.form-row.single { grid-template-columns: 1fr; }

.field label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.9rem;
  padding: 9px 12px;
  font-family: var(--font);
  transition: border-color 0.15s;
  outline: none;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.field select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='7'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238b90a8' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

.field textarea { resize: vertical; min-height: 80px; }

.api-key-wrap { position: relative; }
.api-key-wrap input { padding-right: 44px; font-family: monospace; font-size: 0.82rem; }
.toggle-key {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1rem;
  padding: 2px;
}

/* ── Vocabulary section ── */
#vocab-panel {
  overflow-y: auto;
  padding: 20px;
}

.vocab-stats {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}

.stat-card .num {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.stat-card .lbl {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.vocab-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.vocab-chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 6px;
}

.vocab-chip button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0;
}
.vocab-chip button:hover { color: var(--danger); }

.session-notes {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 16px;
  white-space: pre-wrap;
}

.session-history-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  font-size: 0.82rem;
}

.session-history-item .date {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-bottom: 6px;
}

.topic-chip {
  display: inline-block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.75rem;
  margin: 2px;
  color: var(--text-dim);
}

/* ── Modal / overlay ── */
#mic-overlay,
#import-overlay,
#new-user-modal,
#edit-user-modal,
#password-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 100;
  place-items: center;
  padding: 16px;
}
#mic-overlay.show,
#import-overlay.show,
#new-user-modal.show,
#edit-user-modal.show,
#password-modal.show { display: grid; }

.overlay-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  max-width: 360px;
  width: 90%;
  text-align: center;
}

.overlay-card .icon { font-size: 3rem; margin-bottom: 16px; }
.overlay-card h2 { font-size: 1.1rem; margin-bottom: 10px; }
.overlay-card p { color: var(--text-dim); font-size: 0.88rem; margin-bottom: 24px; }
.overlay-card .btn { width: 100%; }

/* ── Save session modal ── */
#save-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 100;
  place-items: center;
}
#save-modal.show { display: grid; }

#save-modal .overlay-card { max-width: 460px; text-align: left; }
#save-modal h2 { margin-bottom: 16px; }

.summary-json {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-family: monospace;
  font-size: 0.78rem;
  color: var(--text-dim);
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: 16px;
}

.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 0.85rem;
  opacity: 0;
  transition: all 0.25s;
  z-index: 200;
  pointer-events: none;
  white-space: nowrap;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Full-screen auth overlays ── */
#login-overlay,
#force-password-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 300;
  overflow-y: auto;
  padding: 24px 16px;
}

#login-overlay.show,
#force-password-overlay.show { display: block; }

.form-error {
  color: var(--danger);
  font-size: 0.82rem;
  margin-bottom: 12px;
}

.profile-screen {
  max-width: 420px;
  margin: 0 auto;
  padding-top: 40px;
}

.profile-screen-title {
  font-size: 1.8rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 6px;
}

.profile-screen-title span { color: var(--accent); }

.profile-screen-sub {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
  margin-bottom: 28px;
}

.profile-screen h2 {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 20px;
}

.profile-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color 0.15s;
}

.profile-card:hover { border-color: var(--accent); }

.profile-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.profile-card-info { flex: 1; }
.profile-card-name { font-weight: 600; font-size: 0.95rem; }
.profile-card-sub  { font-size: 0.78rem; color: var(--text-dim); margin-top: 2px; }

.profile-card-actions { display: flex; gap: 6px; }
.profile-card-actions button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 4px 6px;
  border-radius: 4px;
}
.profile-card-actions button:hover { color: var(--text); background: var(--border); }

/* ── Admin: user list ── */
#admin-app {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  padding-bottom: 60px;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
}

.user-row-info { flex: 1; min-width: 0; }
.user-row-name { font-weight: 600; font-size: 0.95rem; }
.user-row-sub  { font-size: 0.78rem; color: var(--text-dim); margin-top: 2px; }

.role-badge {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--accent-glow);
  color: var(--accent);
  margin-left: 6px;
  vertical-align: middle;
}

.key-badges { font-size: 0.75rem; color: var(--text-dim); margin-top: 2px; }
.key-badges .set    { color: var(--success); }
.key-badges .unset  { color: var(--text-dim); }

.user-row-actions { display: flex; gap: 6px; flex-shrink: 0; }
.user-row-actions button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 6px 8px;
  border-radius: 4px;
}
.user-row-actions button:hover { color: var(--text); background: var(--border); }

.one-time-password {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-family: monospace;
  font-size: 1rem;
  text-align: center;
  letter-spacing: 0.05em;
  color: var(--success);
  margin-bottom: 12px;
  user-select: all;
}

/* ── Today's Lesson ── */
#todays-lesson-wrap {
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.lesson-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.15s;
}

.lesson-toggle:hover { color: var(--text); }
.lesson-toggle.has-content { color: var(--accent); }

#lesson-toggle-arrow { font-size: 0.7rem; transition: transform 0.2s; }
#todays-lesson-body  { display: none; padding: 0 16px 12px; }
#todays-lesson-body.open { display: block; }
#todays-lesson-body.open ~ * #lesson-toggle-arrow,
.lesson-open #lesson-toggle-arrow { transform: rotate(90deg); }

#inp-todays-lesson {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  padding: 10px 12px;
  font-family: var(--font);
  resize: vertical;
  min-height: 80px;
  max-height: 160px;
  outline: none;
}

#inp-todays-lesson:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

/* ── Responsive ── */
@media (max-width: 480px) {
  .form-row { grid-template-columns: 1fr; }
  header h1 { font-size: 1rem; }
  .vocab-stats { flex-direction: column; }
}
