/*
 * Two predefined themes, "dark" and "light". Every color pair below was
 * checked against WCAG AA (4.5:1 for text, verified for both the page
 * background and the surface/card background it can sit on). --on-primary
 * is the ink color for text/icons drawn on top of a solid --primary (or
 * similarly saturated --danger) fill — dark theme's --primary is bright, so
 * it needs dark ink; light theme's --primary is a deeper blue, so it needs
 * light ink. Don't swap --primary's brightness between themes without
 * re-deriving --on-primary, or a CTA's label contrast will silently fail.
 */
:root {
  --radius: 16px;

  /* dark palette (default; also the no-JS / pre-choice fallback) */
  --bg: #0f0f0f;
  --surface: #1c1c1e;
  --text: #f2f2f7;
  --text-muted: #8e8e93;
  --primary: #0a84ff;
  --on-primary: #04101f;
  --success: #30d158;
  --danger: #ff453a;
  --bubble-ai: #1c1c3a;
  --bubble-user: #1a3a1a;
  --border: rgba(255, 255, 255, 0.1);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  /* Only applies before the user (or app init) has set an explicit theme */
  :root:not([data-theme]) {
    --bg: #f2f2f7;
    --surface: #ffffff;
    --text: #1c1c1e;
    --text-muted: #6c6c70;
    --primary: #0864d1;
    --on-primary: #ffffff;
    --danger: #b3261e;
    --bubble-ai: #e5e5ea;
    --bubble-user: #d1f0d1;
    --border: rgba(0, 0, 0, 0.1);
    color-scheme: light;
  }
}

:root[data-theme="light"] {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --text: #1c1c1e;
  --text-muted: #6c6c70;
  --primary: #0864d1;
  --on-primary: #ffffff;
  --danger: #b3261e;
  --bubble-ai: #e5e5ea;
  --bubble-user: #d1f0d1;
  --border: rgba(0, 0, 0, 0.1);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0f0f0f;
  --surface: #1c1c1e;
  --text: #f2f2f7;
  --text-muted: #8e8e93;
  --primary: #0a84ff;
  --on-primary: #04101f;
  --danger: #ff453a;
  --bubble-ai: #1c1c3a;
  --bubble-user: #1a3a1a;
  --border: rgba(255, 255, 255, 0.1);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { height: 100%; }

body {
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 400;
  font-size: clamp(17px, 4vw, 20px);
  line-height: 1.55;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

/* ── Top nav ──────────────────────────────────────────────────────────────── */

#topnav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  /* Align buttons with content column on wide viewports */
  padding: 0.5rem max(1rem, calc((100% - 680px) / 2));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.nav-btn {
  background: transparent;
  border: 1px solid var(--text-muted);
  color: var(--text);
  border-radius: 8px;
  padding: 0.3rem 0.75rem;
  font-size: 0.82em;
  cursor: pointer;
  touch-action: manipulation;
}

.nav-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ── Screens ──────────────────────────────────────────────────────────────── */

main { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }

/* Explicitly re-apply hidden so display:flex above doesn't win */
.screen[hidden] { display: none; }

.screen {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.5rem 1rem 2rem;
  max-width: 680px;
  width: 100%;
  margin: 0 auto;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

/* Interview screen needs a fixed-bottom-controls layout */
#screen-interview {
  padding: 0;
  overflow: hidden;
}

.interview-header {
  padding: 1rem 1rem 0;
  flex-shrink: 0;
}

/* ── Typography ───────────────────────────────────────────────────────────── */

h1, h2, h3 { font-weight: 700; }
h1 { font-size: clamp(1.5rem, 5vw, 2rem); }
h2 { font-size: clamp(1.1rem, 3.5vw, 1.4rem); }
h3 { font-size: 1rem; }

.lead { font-weight: 400; }
.role-summary { color: var(--text-muted); font-size: 0.9em; }

/* ── Form elements ────────────────────────────────────────────────────────── */

.field { display: flex; flex-direction: column; gap: 0.4rem; }
.field label { font-weight: 400; font-size: 0.9em; }

input[type="text"],
select,
textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  padding: 0.75rem 1rem;
  width: 100%;
}

input[type="text"]:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238e8e93' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  padding-right: 2.5rem;
  cursor: pointer;
}

textarea { resize: none; }

/* ── Work role feedback ───────────────────────────────────────────────────── */

.field-hint {
  padding: 0.75rem 1rem;
  border-radius: 10px;
  border-left: 3px solid var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, transparent);
  font-size: 0.9em;
  line-height: 1.5;
}

.field-error {
  color: var(--danger);
  font-size: 0.875em;
  padding: 0.15rem 0;
}

.field-example {
  color: var(--text-muted);
  font-size: 0.85em;
}

/* ── Settings rows ────────────────────────────────────────────────────────── */

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.setting-disabled { opacity: 0.4; }

.setting-label {
  flex: 1;
  padding-right: 0.75rem;
  cursor: pointer;
}

.setting-hint {
  font-size: 0.82em;
  color: var(--text-muted);
  line-height: 1.4;
}

.setting-select {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

/* ── Toggle switch ────────────────────────────────────────────────────────── */

.toggle {
  position: relative;
  width: 52px;
  height: 30px;
  min-width: 52px;
  min-height: 30px;
  background: var(--text-muted);
  border: none;
  border-radius: 15px;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
  touch-action: manipulation;
  padding: 0;
}

.toggle[aria-checked="true"] { background: var(--primary); }

.toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  pointer-events: none;
}

.toggle[aria-checked="true"] .toggle-thumb { transform: translateX(22px); }

/* ── Buttons ──────────────────────────────────────────────────────────────── */

.btn-primary, .btn-start, .btn-secondary {
  border: none;
  border-radius: var(--radius);
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  touch-action: manipulation;
  transition: opacity 0.15s;
}

.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-start   { background: var(--success); color: #000; font-size: 1.05rem; }
.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  touch-action: manipulation;
}

.btn-danger:active { opacity: 0.75; }
.btn-danger:focus-visible { outline: 3px solid var(--danger); outline-offset: 3px; }

.clear-data-row { margin-top: auto; padding-top: 1rem; display: flex; flex-direction: column; gap: 0.75rem; }
.clear-data-warning { font-size: 0.875em; color: var(--danger); }
.clear-data-actions { display: flex; flex-direction: column; gap: 0.5rem; }

.btn-primary:disabled,
.btn-start:disabled,
.btn-secondary:disabled { opacity: 0.45; cursor: default; }

.btn-primary:active:not(:disabled),
.btn-start:active:not(:disabled),
.btn-secondary:active:not(:disabled) { opacity: 0.75; }

.btn-primary:focus-visible,
.btn-start:focus-visible,
.btn-secondary:focus-visible { outline: 3px solid var(--primary); outline-offset: 3px; }

/* ── Exercise list ────────────────────────────────────────────────────────── */

.exercise-list { display: flex; flex-direction: column; gap: 0.75rem; }

.btn-exercise {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  text-align: left;
  touch-action: manipulation;
  transition: border-color 0.15s;
}

.btn-exercise:hover:not(:disabled)  { border-color: var(--primary); }
.btn-exercise:active:not(:disabled) { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, transparent); }
.btn-exercise:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn-exercise--disabled, .btn-exercise:disabled { opacity: 0.4; cursor: default; }

.exercise-icon { font-size: 1.4rem; flex-shrink: 0; }

/* ── Badge ────────────────────────────────────────────────────────────────── */

.badge {
  margin-left: auto;
  font-size: 0.7em;
  font-weight: 400;
  color: var(--text-muted);
  background: color-mix(in srgb, var(--text) 8%, transparent);
  padding: 0.2em 0.6em;
  border-radius: 8px;
}

/* ── Interview screen layout ──────────────────────────────────────────────── */

#status {
  min-height: 1.4em;
  font-size: 0.85em;
  color: var(--primary);
  padding-bottom: 0.25rem;
}

#conversation {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding: 0.5rem 1rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-bottom: 0.5rem;
}

.message {
  padding: 0.875rem 1.125rem;
  border-radius: var(--radius);
  max-width: 85%;
  word-break: break-word;
  margin: 0;
}

.message--assistant {
  background: var(--bubble-ai);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.message--user {
  background: var(--bubble-user);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  text-align: right;
}

.message-body {
  margin: 0;
  white-space: pre-wrap;
}

.message-actions {
  margin-top: 0.65rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.btn-plain-language {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0.35rem 0.75rem;
  font-size: 0.82em;
  font-weight: 700;
  color: var(--text);
  background: color-mix(in srgb, var(--text) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--text) 22%, transparent);
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-plain-language:hover:not(:disabled) {
  background: color-mix(in srgb, var(--primary) 18%, transparent);
  border-color: var(--primary);
  color: var(--text);
}

.btn-plain-language:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn-plain-language:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.plain-language-panel {
  margin-top: 0.5rem;
  padding: 0.55rem 0.7rem;
  border-left: 3px solid var(--primary);
  background: color-mix(in srgb, var(--primary) 8%, transparent);
  border-radius: 0 8px 8px 0;
}

.plain-language-panel[hidden] {
  display: none;
}

.plain-language-label {
  margin: 0 0 0.25rem;
  font-size: 0.75em;
  font-weight: 400;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.plain-language-text {
  margin: 0;
  font-size: 0.95em;
  white-space: pre-wrap;
}

#controls {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 1rem 1rem;
  border-top: 1px solid var(--border);
}

/* ── Composer (mic + text + send) ─────────────────────────────────────────── */

.composer {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.mic-hint-panel {
  font-size: 0.78em;
  color: var(--text-muted);
  line-height: 1.45;
  padding: 0.5rem 0.65rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.mic-hint-panel p { margin: 0; }

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.composer-voice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  flex-shrink: 0;
}

.composer-voice[hidden] {
  display: none;
}

.composer-icon-btn[hidden] {
  display: none;
}

.composer-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 50%;
  cursor: pointer;
  touch-action: manipulation;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.composer-icon-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.composer-icon-btn[aria-expanded="true"] {
  color: var(--primary);
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent);
}

.composer-icon-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

#mic-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--composer-btn-size, 52px);
  height: var(--composer-btn-size, 52px);
  padding: 0;
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  -webkit-tap-highlight-color: transparent;
}

#mic-btn:active:not(:disabled) { transform: scale(0.97); }

#mic-btn[data-state="listening"] {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(255, 69, 58, 0.35);
  animation: pulse 1.2s ease-in-out infinite;
}

#mic-btn[data-state="processing"],
#mic-btn[data-state="speaking"] {
  opacity: 0.55;
  cursor: wait;
}

#mic-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#mic-btn:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 4px;
}

.mic-icon {
  flex-shrink: 0;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.04); }
}

.composer-row #text-input {
  flex: 1;
  min-width: 0;
  height: var(--composer-btn-size, 52px);
  min-height: var(--composer-btn-size, 52px);
  max-height: calc(var(--composer-btn-size, 52px) * 2.5);
  padding: 0.6rem 0.75rem;
  line-height: 1.35;
  border-radius: 12px;
  resize: none;
  overflow-y: auto;
}

.btn-send {
  flex-shrink: 0;
  min-width: 4.5rem;
  height: var(--composer-btn-size, 52px);
  min-height: var(--composer-btn-size, 52px);
  background: var(--primary);
  color: var(--on-primary);
  border: none;
  border-radius: 12px;
  padding: 0 1.1rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-send:active { opacity: 0.75; }
.btn-send:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ── Feedback ─────────────────────────────────────────────────────────────── */

#status-feedback {
  min-height: 1.4em;
  font-size: 0.85em;
  color: var(--primary);
}

#feedback-content { display: flex; flex-direction: column; gap: 0.35rem; flex: 1; }
#feedback-content h3 { font-size: 0.95em; color: var(--primary); margin-top: 0.75rem; }
#feedback-content p  { line-height: 1.6; }

/* ── Motion ───────────────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .toggle-thumb    { transition: none; }
  .toggle          { transition: none; }
  #conversation    { scroll-behavior: auto; }
  #mic-btn[data-state="listening"] { animation: none; }
}
