/* =========================================================
   Assistente Pessoal — Dr. Aguinel
   Estética: refinada, médica sem ser clínica, calma
   ========================================================= */

:root {
  /* Cores base */
  --bg: #FAF7F2;
  --surface: #FFFFFF;
  --surface-warm: #F3EFE7;
  --ink: #1A1A1A;
  --ink-soft: #4A4A4A;
  --ink-mute: #8A8580;
  --line: #E6E1D7;
  --accent: #2E5D4F;
  --accent-soft: #3F7E6E;
  --bubble-user: #1A1A1A;
  --bubble-user-text: #FAF7F2;
  --bubble-assistant: #F0EDE6;
  --rec: #B7472A;

  /* Tipografia */
  --serif: 'Instrument Serif', 'Iowan Old Style', Georgia, serif;
  --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  /* Espaçamento */
  --radius: 18px;
  --radius-sm: 12px;
  --shadow-soft: 0 1px 2px rgba(26, 26, 26, 0.04), 0 4px 16px rgba(26, 26, 26, 0.04);

  /* Safe areas para iOS */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Garante que o atributo HTML `hidden` funciona mesmo quando o CSS
   define display:flex ou display:grid no mesmo elemento */
[hidden] { display: none !important; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
  background: var(--bg);
}

/* ===== TOPBAR ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(14px + var(--safe-top)) 20px 14px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 8px; flex: 1; }
.brand-mark { font-family: var(--serif); font-size: 22px; color: var(--accent); }
.brand-name { font-family: var(--serif); font-style: italic; font-size: 22px; color: var(--ink); letter-spacing: 0.2px; }

.status {
  font-size: 12px;
  color: var(--ink-mute);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.4px;
  text-transform: lowercase;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-warm);
}
.status[data-state="recording"] { color: var(--rec); background: rgba(183, 71, 42, 0.08); }
.status[data-state="thinking"]   { color: var(--accent); background: rgba(46, 93, 79, 0.08); }
.status[data-state="error"]      { color: var(--rec); background: rgba(183, 71, 42, 0.1); }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  width: 36px; height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.18s ease, color 0.18s ease;
}
.ghost-btn:hover, .ghost-btn:active { background: var(--surface-warm); color: var(--ink); }

/* ===== CONVERSA ===== */
.conversation {
  overflow-y: auto;
  padding: 24px 20px 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 320px;
  color: var(--ink-mute);
}
.empty-state .greet {
  font-family: var(--serif);
  font-size: 38px;
  line-height: 1.1;
  color: var(--ink);
  margin: 0 0 12px;
}
.empty-state .greet em { color: var(--accent); font-style: italic; }
.empty-state .hint { font-size: 14px; margin: 0; }

.bubble {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 15.5px;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: bubble-in 0.32s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.bubble.user {
  align-self: flex-end;
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 6px;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--bubble-assistant);
  color: var(--ink);
  border-bottom-left-radius: 6px;
  position: relative;
}

.bubble.assistant .tts-btn {
  margin-top: 8px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.18s ease;
}
.bubble.assistant .tts-btn:hover { background: var(--surface); color: var(--accent); border-color: var(--accent); }
.bubble.assistant .tts-btn.playing { background: var(--accent); color: white; border-color: var(--accent); }

.bubble.error {
  align-self: center;
  background: rgba(183, 71, 42, 0.08);
  color: var(--rec);
  font-size: 13.5px;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
}

/* Indicador "pensando" */
.thinking {
  align-self: flex-start;
  display: inline-flex;
  gap: 4px;
  padding: 14px 18px;
  background: var(--bubble-assistant);
  border-radius: var(--radius);
  border-bottom-left-radius: 6px;
}
.thinking span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-mute);
  animation: think-pulse 1.2s ease-in-out infinite;
}
.thinking span:nth-child(2) { animation-delay: 0.18s; }
.thinking span:nth-child(3) { animation-delay: 0.36s; }
@keyframes think-pulse {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* ===== COMPOSER ===== */
.composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px calc(12px + var(--safe-bottom)) 16px;
  background: var(--bg);
  border-top: 1px solid var(--line);
  align-items: center;
}

.mic-btn {
  flex-shrink: 0;
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.15s ease, background 0.2s ease;
}
.mic-btn:active { transform: scale(0.94); }
.mic-btn.recording { background: var(--rec); animation: rec-pulse 1.2s ease-in-out infinite; }
@keyframes rec-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(183, 71, 42, 0.5); }
  50%       { box-shadow: 0 0 0 12px rgba(183, 71, 42, 0); }
}

.text-form {
  flex: 1;
  display: flex;
  gap: 8px;
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 6px 6px 18px;
  border: 1px solid var(--line);
  align-items: center;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}
.text-form:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(46, 93, 79, 0.1);
}

#text-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-family: var(--sans);
  font-size: 15.5px;
  color: var(--ink);
  padding: 8px 0;
}
#text-input::placeholder { color: var(--ink-mute); }

.send-btn {
  flex-shrink: 0;
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.18s ease, transform 0.15s ease;
}
.send-btn:hover { background: var(--accent-soft); }
.send-btn:active { transform: scale(0.92); }
.send-btn:disabled { background: var(--ink-mute); cursor: not-allowed; }

/* ===== OVERLAY DE GRAVAÇÃO ===== */
.recording-overlay {
  position: fixed;
  inset: 0;
  background: rgba(26, 26, 26, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 36px;
  z-index: 100;
  animation: overlay-in 0.25s ease;
}
@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }

.rec-pulse {
  width: 120px; height: 120px;
  border-radius: 50%;
  background: var(--rec);
  display: flex; align-items: center; justify-content: center;
  position: relative;
  animation: rec-pulse-big 1.4s ease-in-out infinite;
}
.rec-pulse::before {
  content: '';
  position: absolute;
  inset: -12px;
  border-radius: 50%;
  border: 2px solid rgba(183, 71, 42, 0.4);
  animation: ring-expand 1.4s ease-out infinite;
}
@keyframes rec-pulse-big {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.06); }
}
@keyframes ring-expand {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1.4); opacity: 0; }
}

.rec-time {
  font-family: var(--serif);
  font-size: 48px;
  color: white;
  font-variant-numeric: tabular-nums;
  letter-spacing: 1px;
}

.rec-actions { display: flex; gap: 24px; }
.rec-btn {
  width: 64px; height: 64px;
  border-radius: 50%;
  border: none;
  font-size: 28px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.15s ease;
}
.rec-btn:active { transform: scale(0.92); }
.rec-cancel { background: rgba(255, 255, 255, 0.2); color: white; }
.rec-stop   { background: white; color: var(--accent); }

/* ===== MARKDOWN BÁSICO NAS BUBBLES ===== */
.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; font-family: var(--serif); }
.bubble code {
  font-family: ui-monospace, 'SF Mono', monospace;
  font-size: 0.92em;
  background: rgba(26, 26, 26, 0.06);
  padding: 1px 5px;
  border-radius: 4px;
}
.bubble.user code { background: rgba(255, 255, 255, 0.15); }
.bubble a { color: var(--accent); text-decoration: underline; }
.bubble.user a { color: var(--bubble-user-text); }

/* ===== MODAL DE CONFIGURAÇÕES ===== */
.settings-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(26, 26, 26, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: overlay-in 0.2s ease;
}

.settings-sheet {
  position: relative;
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 0 0 calc(24px + var(--safe-bottom)) 0;
  box-shadow: 0 -4px 32px rgba(26,26,26,0.12);
  animation: sheet-up 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
  max-height: 80dvh;
  overflow-y: auto;
}
@keyframes sheet-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

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

.settings-title {
  font-family: var(--serif);
  font-style: italic;
  font-size: 20px;
  color: var(--ink);
}

.settings-body {
  padding: 20px 20px 8px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin-bottom: -8px;
}

.settings-select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: var(--sans);
  font-size: 15px;
  color: var(--ink);
  appearance: none;
  -webkit-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='%238A8580' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  cursor: pointer;
}
.settings-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(46,93,79,0.1);
}

.rate-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.rate-btn {
  padding: 10px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: var(--sans);
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  text-align: center;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.rate-btn:hover { border-color: var(--accent); color: var(--accent); }
.rate-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
}

.preview-btn {
  width: 100%;
  padding: 14px;
  background: var(--surface-warm);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-family: var(--sans);
  font-size: 15px;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.18s ease;
  margin-top: 4px;
}
.preview-btn:hover { background: var(--line); }
.preview-btn:active { transform: scale(0.98); }
