/* ── Reset & tokens ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface-alt: #f3f0eb;
  --border: #e4ddd4;
  --text: #1a1714;
  --text-2: #5c5650;
  --text-3: #9a948d;
  --accent: #5b8c7a;
  --accent-light: #e8f2ee;
  --accent-dark: #3d6b5a;
  --warn: #c4622b;
  --warn-light: #fdf0e9;
  --urgent: #b33a3a;
  --urgent-light: #fceaea;
  --user-bubble: #e8f2ee;
  --assistant-bubble: #ffffff;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 2px 8px rgba(0,0,0,.04);
  --shadow-lg: 0 4px 20px rgba(0,0,0,.12);
  --radius: 16px;
  --radius-sm: 10px;
  --font: -apple-system, 'Helvetica Neue', Arial, sans-serif;
  --font-size: 16px;
  --transition: 0.18s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12100e;
    --surface: #1e1b18;
    --surface-alt: #252220;
    --border: #332f2a;
    --text: #f0ece6;
    --text-2: #b0a99f;
    --text-3: #6e6760;
    --accent: #6aad92;
    --accent-light: #1a2e26;
    --accent-dark: #8fcdb4;
    --warn: #e07a4a;
    --warn-light: #2e1a0e;
    --urgent: #d95f5f;
    --urgent-light: #2e1010;
    --user-bubble: #1a2e26;
    --assistant-bubble: #1e1b18;
  }
}

[data-font="small"]  { --font-size: 14px; }
[data-font="medium"] { --font-size: 16px; }
[data-font="large"]  { --font-size: 18px; }
[data-font="xlarge"] { --font-size: 21px; }

body {
  font-family: var(--font);
  font-size: var(--font-size);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
}

.reduced-motion * { transition: none !important; animation: none !important; }

/* Discrete mode: plain appearance, no app name */
.discrete #app-name { display: none; }
.discrete #quick-actions { display: none; }

/* ── Layout ──────────────────────────────────────────────────────────────── */

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 680px;
  margin: 0 auto;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

#app-name {
  font-size: 1.1em;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.02em;
  flex: 1;
}

#mock-badge {
  font-size: 0.7em;
  padding: 2px 8px;
  background: var(--warn-light);
  color: var(--warn);
  border-radius: 20px;
  font-weight: 600;
}

.header-controls { display: flex; gap: 6px; align-items: center; }

/* ── Safety panel ────────────────────────────────────────────────────────── */

#safety-panel {
  background: var(--warn-light);
  border-bottom: 2px solid var(--warn);
  padding: 12px 16px;
  flex-shrink: 0;
}

#safety-panel.urgent {
  background: var(--urgent-light);
  border-color: var(--urgent);
}

#safety-panel h2 {
  font-size: 0.95em;
  font-weight: 700;
  color: var(--warn);
  margin-bottom: 8px;
}

#safety-panel.urgent h2 { color: var(--urgent); }

#safety-resources { display: flex; flex-direction: column; gap: 6px; }

.resource-link, .resource-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1.5px solid var(--border);
  font-weight: 600;
  font-size: 0.95em;
  text-decoration: none;
  color: var(--text);
}

.resource-link.urgent { border-color: var(--urgent); color: var(--urgent); }
.resource-note { font-weight: 400; color: var(--text-2); font-size: 0.85em; margin-left: auto; }

#safety-panel footer { margin-top: 8px; display: flex; justify-content: flex-end; }

/* ── Quick actions ───────────────────────────────────────────────────────── */

#quick-actions {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  scrollbar-width: none;
}

#quick-actions::-webkit-scrollbar { display: none; }

.qa-btn {
  flex-shrink: 0;
  padding: 7px 14px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85em;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--transition), border-color var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.qa-btn:hover, .qa-btn:focus-visible { background: var(--accent-light); border-color: var(--accent); color: var(--accent-dark); }
.qa-btn--ground { border-color: var(--warn); color: var(--warn); }
.qa-btn--ground:hover { background: var(--warn-light); }

/* ── Chat ────────────────────────────────────────────────────────────────── */

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

.message { display: flex; flex-direction: column; max-width: 88%; }
.message--user { align-self: flex-end; align-items: flex-end; }
.message--assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.55;
  box-shadow: var(--shadow);
}

.message--user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.message--assistant .bubble { background: var(--assistant-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.message-time { font-size: 0.72em; color: var(--text-3); margin-top: 3px; padding: 0 4px; }
.caption { font-size: 0.78em; color: var(--text-3); font-style: italic; padding: 2px 4px; }

/* Thinking dots */
.thinking .bubble { padding: 16px 20px; }
.dot {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-3);
  margin: 0 2px;
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-6px); opacity: 1; }
}

/* ── Transcript preview ──────────────────────────────────────────────────── */

.transcript-preview {
  margin: 0 12px;
  padding: 8px 14px;
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-style: italic;
  font-size: 0.9em;
  flex-shrink: 0;
}

/* ── Input bar ───────────────────────────────────────────────────────────── */

#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#input-text {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-family: var(--font);
  font-size: var(--font-size);
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  max-height: 150px;
  min-height: 44px;
  outline: none;
  transition: border-color var(--transition);
}

#input-text:focus { border-color: var(--accent); }
#input-text::placeholder { color: var(--text-3); }

.icon-btn {
  width: 44px; height: 44px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2em;
  flex-shrink: 0;
  transition: background var(--transition), transform var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:active { transform: scale(0.92); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#mic-btn { background: var(--surface-alt); color: var(--text-2); border: 1.5px solid var(--border); }
#mic-btn.listening { background: var(--warn); color: white; border-color: var(--warn); animation: pulse 1.5s infinite; }
#send-btn { background: var(--accent); color: white; }
#send-btn:hover { background: var(--accent-dark); }
#stop-speak { background: var(--surface-alt); color: var(--text-3); border: 1.5px solid var(--border); }
#stop-speak.active { background: var(--warn-light); color: var(--warn); border-color: var(--warn); }
#mute-btn { background: transparent; color: var(--text-2); border: none; font-size: 1.3em; }

@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(196,98,43,.4); } 50% { box-shadow: 0 0 0 8px rgba(196,98,43,0); } }

#mic-status { font-size: 0.8em; color: var(--warn); font-weight: 500; text-align: center; padding: 2px 0 4px; flex-shrink: 0; display: none; }
#mic-status.active { display: block; }

/* ── Panels (modal drawers) ──────────────────────────────────────────────── */

#overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 50;
  backdrop-filter: blur(2px);
}

.panel {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  max-width: 680px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  z-index: 100;
  max-height: 90dvh;
  overflow-y: auto;
  padding: 24px 20px 32px;
  box-shadow: var(--shadow-lg);
}

.panel-handle {
  width: 40px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 20px;
}

.panel h2 {
  font-size: 1.15em;
  font-weight: 700;
  margin-bottom: 20px;
  color: var(--text);
}

.panel h3 { font-size: 1em; font-weight: 600; margin: 16px 0 8px; }

/* ── Forms ───────────────────────────────────────────────────────────────── */

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.88em; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }

.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input[type=range] { flex: 1; accent-color: var(--accent); }
.range-val { font-size: 1em; font-weight: 700; color: var(--accent); min-width: 28px; text-align: right; }

input[type=text], textarea, select, input[type=number], input[type=password] {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: var(--font-size);
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color var(--transition);
}

input:focus, textarea:focus, select:focus { border-color: var(--accent); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.95em;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.btn--primary { background: var(--accent); color: white; width: 100%; }
.btn--primary:hover { background: var(--accent-dark); }
.btn--secondary { background: var(--surface-alt); color: var(--text); border: 1.5px solid var(--border); }
.btn--secondary:hover { background: var(--border); }
.btn--warn { background: var(--urgent-light); color: var(--urgent); border: 1.5px solid var(--urgent); }
.btn--close { position: absolute; top: 16px; right: 16px; background: transparent; border: none; cursor: pointer; color: var(--text-3); font-size: 1.4em; padding: 4px 8px; }

/* ── Grounding panel ─────────────────────────────────────────────────────── */

.grounding-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.ground-tool-btn {
  padding: 16px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  text-align: left;
  transition: background var(--transition), border-color var(--transition);
}

.ground-tool-btn:hover { background: var(--accent-light); border-color: var(--accent); }
.ground-tool-btn strong { display: block; margin-bottom: 4px; font-size: 0.95em; }
.ground-tool-btn span { font-size: 0.8em; color: var(--text-2); }

#grounding-active { text-align: center; }

#grounding-tool-title { font-size: 1.05em; font-weight: 700; color: var(--accent); margin-bottom: 24px; }
#grounding-step { font-size: 1.15em; line-height: 1.6; margin-bottom: 20px; min-height: 3em; }
#grounding-progress { color: var(--text-3); font-size: 0.85em; margin-bottom: 20px; }

/* ── Pattern cards ───────────────────────────────────────────────────────── */

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 20px; }
.stat-card { background: var(--surface-alt); border-radius: var(--radius-sm); padding: 14px 10px; text-align: center; }
.stat-value { font-size: 1.4em; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 0.75em; color: var(--text-2); margin-top: 2px; }

.pattern-section { margin-bottom: 16px; }
.pattern-section h4 { font-size: 0.88em; font-weight: 600; color: var(--text-2); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.04em; }
.pattern-section ul { list-style: none; display: flex; flex-direction: column; gap: 4px; }
.pattern-section li { padding: 6px 10px; background: var(--surface-alt); border-radius: 8px; font-size: 0.9em; }
.count { color: var(--text-3); font-size: 0.85em; float: right; }

.empty-state { color: var(--text-3); font-style: italic; text-align: center; padding: 24px 0; }

/* ── Summary ─────────────────────────────────────────────────────────────── */

#summary-content { background: var(--surface-alt); border-radius: var(--radius-sm); padding: 16px; margin-bottom: 16px; font-size: 0.92em; line-height: 1.65; }
#summary-raw { display: none; }

/* ── Settings ────────────────────────────────────────────────────────────── */

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.settings-row label { font-size: 0.92em; font-weight: 500; }
.settings-note { font-size: 0.8em; color: var(--text-3); margin-top: 4px; }

input[type=checkbox] { width: 44px; height: 26px; accent-color: var(--accent); cursor: pointer; }

.divider { height: 1px; background: var(--border); margin: 20px 0; }

/* ── PIN screen ──────────────────────────────────────────────────────────── */

#pin-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  padding: 32px;
  gap: 16px;
  background: var(--bg);
}

#pin-screen h1 { font-size: 1.5em; color: var(--accent); }
#pin-screen p { font-size: 0.9em; color: var(--text-2); text-align: center; max-width: 280px; }
#pin-input { text-align: center; letter-spacing: 0.2em; font-size: 1.3em; max-width: 200px; }
#pin-error { color: var(--urgent); font-size: 0.88em; min-height: 1.2em; }

/* ── Toast ───────────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 24px;
  font-size: 0.88em;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  z-index: 200;
  transition: opacity var(--transition), transform var(--transition);
  white-space: nowrap;
}

.toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast--error { background: var(--urgent); }
.toast--warn { background: var(--warn); }

/* ── Voice unavailable notice ────────────────────────────────────────────── */

#voice-unavailable {
  margin: 0 12px;
  padding: 8px 14px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82em;
  color: var(--text-2);
  flex-shrink: 0;
}

/* ── Utilities ───────────────────────────────────────────────────────────── */

.hidden { display: none !important; }

.btn-row { display: flex; gap: 10px; margin-top: 16px; }
.btn-row .btn { flex: 1; }

/* ── Accessibility ───────────────────────────────────────────────────────── */

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
a:focus-visible { outline: 2px solid var(--accent); border-radius: 4px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ── Desktop ─────────────────────────────────────────────────────────────── */

@media (min-width: 600px) {
  .panel { border-radius: var(--radius); bottom: auto; top: 50%; transform: translateY(-50%); left: 50%; right: auto; width: 90%; max-width: 520px; transform: translate(-50%, -50%); max-height: 88vh; }
  .grounding-grid { grid-template-columns: repeat(2, 1fr); }
}
