/* Accessibility-first styling. Elderly-friendly defaults:
   - base font 20px, generous line-height
   - big touch targets (min 56px)
   - high contrast palette, no gray-on-gray
   - focus rings that are actually visible
*/

:root {
  --bg: #fbf7f2;
  --bg-tint: #f5ece2;
  --surface: #ffffff;
  --ink: #1c1c1c;
  --ink-soft: #3a3a3a;
  --brand: #b6465f;      /* warm rose */
  --brand-dark: #8f2f47;
  --brand-tint: #fdeef2;
  --accent: #2f6d4f;     /* deep green for "connected" */
  --warning: #b45309;
  --danger: #b91c1c;
  --border: #e5decf;
  --shadow-soft: 0 6px 20px rgba(60, 30, 20, 0.06);
  --shadow-lift: 0 12px 30px rgba(60, 30, 20, 0.09);
  --radius-lg: 22px;
  --radius-md: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: "Inter", "Helvetica Neue", Arial, sans-serif;
  font-size: 20px;
  line-height: 1.55;
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-tint) 100%);
  /* Lock the whole page so only inner scroll regions ever move. Keeps
     the chat compose row anchored no matter how tall the sidebar grows. */
  height: 100vh;
  overflow: hidden;
}

h1 { font-size: 2.2rem; margin: 0 0 0.5rem; letter-spacing: -0.01em; }
h2 { font-size: 1.6rem; margin: 0 0 0.5rem; letter-spacing: -0.005em; }
h3 { font-size: 1.25rem; margin: 0 0 0.5rem; }

button, .btn {
  min-height: 56px;
  padding: 0.75rem 1.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.18s ease,
              box-shadow 0.18s ease, color 0.18s ease;
}
button:hover:not(:disabled), .btn:hover:not(:disabled) {
  transform: translateY(-1px);
}
button:active:not(:disabled), .btn:active:not(:disabled) {
  transform: translateY(0);
}
button:focus-visible, .btn:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 3px;
}

.btn-primary {
  background: var(--brand);
  color: white;
  box-shadow: 0 4px 12px rgba(182, 70, 95, 0.25);
}
.btn-primary:hover:not(:disabled) {
  background: var(--brand-dark);
  box-shadow: 0 6px 16px rgba(143, 47, 71, 0.3);
}
.btn-primary:disabled { background: #cbb0b7; cursor: not-allowed; box-shadow: none; }

.btn-ghost {
  background: transparent;
  color: var(--brand-dark);
  border-color: var(--brand-dark);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--brand-tint);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow-soft);
}

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1.5rem;
}

/* -- Login page ---------------------------------------------------------- */

.hero {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
  background: linear-gradient(160deg, #fbf7f2 0%, #f2e2df 100%);
}
.hero-card {
  max-width: 560px;
  text-align: center;
  box-shadow: var(--shadow-lift);
}
.hero-card h1 { color: var(--brand-dark); }
.hero-card p  { color: var(--ink-soft); font-size: 1.15rem; }

/* -- App layout ---------------------------------------------------------- */

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 1.5rem;
  height: 100vh;        /* pin to viewport so inner regions own scrolling */
  padding: 1.5rem;
}
@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }
  html, body { overflow: auto; height: auto; }
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 1rem;
  margin-bottom: 1rem;
  gap: 1rem;
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-weight: 700;
  color: var(--brand-dark);
  font-size: 1.5rem;
  letter-spacing: -0.01em;
}
.brand-dot {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-tint);
}

/* Main card is a flex column that fills the viewport row. min-height:0
   is essential so the internal chat-log can shrink and scroll instead
   of pushing the compose row past the fold. */
main.card {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* -- Chat --------------------------------------------------------------- */

.chat-log {
  flex: 1 1 auto;
  min-height: 0;           /* allow shrink inside flex parent */
  overflow-y: auto;
  padding: 0.5rem 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  scroll-behavior: smooth;
}
.msg {
  padding: 0.55rem 1rem;
  border-radius: 20px;
  max-width: 82%;
  font-size: 1.15rem;
  line-height: 1.5;
  text-align: left;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 2px rgba(60, 30, 20, 0.05);
  animation: msg-in 0.25s ease-out both;
}
/* Markdown-rendered bubble body. Keep paragraph gaps tight; the bubble
   already has its own padding. */
.msg .md > :first-child { margin-top: 0; }
.msg .md > :last-child  { margin-bottom: 0; }
.msg .md p { margin: 0 0 0.55rem; }
.msg .md ul,
.msg .md ol { margin: 0 0 0.55rem; padding-left: 1.4rem; }
.msg .md li { margin: 0.15rem 0; }
.msg .md li > p { margin: 0; }
.msg .md h1,
.msg .md h2,
.msg .md h3,
.msg .md h4 {
  margin: 0.6rem 0 0.35rem;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.005em;
}
.msg .md h1 { font-size: 1.35rem; }
.msg .md h2 { font-size: 1.25rem; }
.msg .md h3 { font-size: 1.15rem; }
.msg .md h4 { font-size: 1.05rem; }
.msg .md a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.msg .md strong { font-weight: 700; }
.msg .md em     { font-style: italic; }
.msg .md code {
  font-family: "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: rgba(0, 0, 0, 0.06);
  padding: 0.05em 0.35em;
  border-radius: 6px;
}
.msg .md pre {
  margin: 0 0 0.55rem;
  padding: 0.6rem 0.8rem;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  overflow-x: auto;
}
.msg .md pre code { background: transparent; padding: 0; }
.msg .md blockquote {
  margin: 0 0 0.55rem;
  padding: 0.2rem 0 0.2rem 0.8rem;
  border-left: 3px solid currentColor;
  opacity: 0.85;
}
.msg .md hr {
  border: 0;
  border-top: 1px solid currentColor;
  opacity: 0.25;
  margin: 0.6rem 0;
}
/* User bubble is on brand-dark; make inline code/pre readable on it. */
.msg-user .md code { background: rgba(255, 255, 255, 0.18); }
.msg-user .md pre  { background: rgba(255, 255, 255, 0.15); }
.msg-user .md a    { text-decoration-color: rgba(255, 255, 255, 0.7); }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0);   }
}
.msg-user {
  align-self: flex-end;
  background: var(--brand);
  color: white;
  border-bottom-right-radius: 6px;
}
.msg-livia {
  align-self: flex-start;
  background: #fff5ee;
  color: var(--ink);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.msg-system {
  align-self: center;
  background: transparent;
  color: var(--ink-soft);
  font-style: italic;
  font-size: 0.95rem;
  box-shadow: none;
  animation: none;
}

/* Typing indicator — shown in the Livia bubble while we wait for the
   first streamed token so the user sees the AI is thinking. */
.typing {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 2px 0;
}
.typing > span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--brand-dark);
  opacity: 0.35;
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing > span:nth-child(2) { animation-delay: 0.15s; }
.typing > span:nth-child(3) { animation-delay: 0.30s; }
@keyframes typing-bounce {
  0%, 80%, 100% { transform: translateY(0);   opacity: 0.35; }
  40%           { transform: translateY(-4px); opacity: 1;    }
}

/* -- Compose row -------------------------------------------------------- */

.compose {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  margin-top: 1rem;
  flex-shrink: 0;   /* never yield space to the chat log */
}
.compose input[type="text"] {
  flex: 1;
  min-height: 56px;
  padding: 0 1rem;
  font-size: 1.15rem;
  border-radius: var(--radius-md);
  border: 2px solid var(--border);
  background: white;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.compose input[type="text"]:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-tint);
}
.mic-btn {
  width: 84px; height: 84px;
  border-radius: 50%;
  background: var(--brand);
  color: white;
  border: none;
  font-size: 2rem;
  display: grid; place-items: center;
  box-shadow: 0 4px 12px rgba(182, 70, 95, 0.35);
}
.mic-btn.active {
  background: var(--danger);
  animation: pulse 1.4s infinite ease-in-out;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(185, 28, 28, 0.6); }
  50%      { box-shadow: 0 0 0 16px rgba(185, 28, 28, 0); }
}

/* -- Voice page --------------------------------------------------------- */

.voice-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 1.5rem;
  gap: 1.5rem;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}
.voice-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  text-align: center;
  padding: 3rem 2rem;
}
.voice-status {
  font-size: 1.4rem;
  color: var(--brand-dark);
  font-weight: 600;
  margin: 0;
}
.voice-hint {
  color: var(--ink-soft);
  font-size: 1.05rem;
  max-width: 32rem;
  margin: 0;
}
.mic-btn-xl {
  width: 168px; height: 168px;
  font-size: 4rem;
}
.mic-btn.speaking {
  background: var(--accent);
  animation: pulse-green 1.4s infinite ease-in-out;
}
@keyframes pulse-green {
  0%, 100% { box-shadow: 0 0 0 0 rgba(47, 109, 79, 0.55); }
  50%      { box-shadow: 0 0 0 20px rgba(47, 109, 79, 0); }
}

/* -- Sidebar ------------------------------------------------------------ */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow-y: auto;           /* independent scroll if content overflows */
  padding-right: 4px;         /* room for scrollbar without jitter */
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 600;
}
.status-pill.ok { background: #e6f4ec; color: var(--accent); }
.status-pill.off { background: #fdecec; color: var(--danger); }

.reminder {
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 1rem;
}
.reminder:last-child { border-bottom: none; }
.reminder-when { font-size: 0.95rem; color: var(--ink-soft); margin-top: 0.15rem; }
.reminder-kind {
  font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--brand-dark); font-weight: 700;
  margin-top: 0.35rem;
}

.emergency {
  background: #fff1f1;
  border: 2px solid #f3b8b8;
  border-radius: var(--radius-lg);
  padding: 1rem;
  color: var(--danger);
}
.emergency strong { font-size: 1.3rem; }
.emergency a { color: var(--danger); text-decoration: none; font-weight: 700; }
.emergency a:hover { text-decoration: underline; }

/* Respect users who ask for reduced motion. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .msg { animation: none; }
}
