:root{
  --ink-deep:#0A2B2E;
  --ink-panel:#0F3A3E;
  --ink-panel-2:#123F44;
  --teal-line: rgba(212,169,74,0.14);
  --gold:#D4A94A;
  --gold-soft:#E6C878;
  --coral:#E8734A;
  --leaf:#5C8A72;
  --cream:#F3ECDA;
  --cream-dim:#C9C2AC;
  --shadow: 0 20px 60px rgba(0,0,0,0.35);
  --radius-lg: 22px;
  --radius-md: 14px;
  --font-display:'Fraunces', serif;
  --font-body:'Inter', sans-serif;
  --font-mono:'JetBrains Mono', monospace;
}

*{box-sizing:border-box;}
html,body{height:100%;}
body{
  margin:0;
  background:var(--ink-deep);
  color:var(--cream);
  font-family:var(--font-body);
  overflow:hidden;
}

.app{
  display:grid;
  grid-template-columns: 1.05fr 1fr;
  height:100vh;
  height:100dvh; /* accounts for mobile browser chrome (address bar / keyboard) */
  width:100vw;
}

/* ================= STAGE (avatar side) ================= */
.stage{
  position:relative;
  min-height:0; /* let grid item shrink instead of overflowing the row */
  min-width:0;
  background:
    radial-gradient(120% 90% at 20% 0%, #12474C 0%, #0A2B2E 60%),
    var(--ink-deep);
  display:flex;
  flex-direction:column;
  padding:40px 44px 32px;
  overflow:hidden;
  border-right:1px solid var(--teal-line);
}

.stage__topo{
  position:absolute; inset:0;
  background-image:
    repeating-radial-gradient(circle at 78% 18%, transparent 0, transparent 26px, var(--teal-line) 27px, transparent 28px);
  opacity:.55;
  pointer-events:none;
}

.stage__header{ position:relative; z-index:2; }
.stage__eyebrow{
  font-family:var(--font-mono);
  font-size:11px;
  letter-spacing:.14em;
  color:var(--gold-soft);
  opacity:.85;
}
.stage__title{
  font-family:var(--font-display);
  font-weight:700;
  font-size:56px;
  line-height:1;
  margin:10px 0 4px;
  color:var(--cream);
}
.stage__subtitle{
  margin:0;
  font-size:15px;
  color:var(--cream-dim);
  max-width:34ch;
}

.stage__canvas-wrap{
  position:relative;
  flex:1;
  min-height:0;
  margin-top:12px;
  border-radius:var(--radius-lg);
  overflow:hidden;
  background: linear-gradient(180deg, rgba(212,169,74,0.06), transparent 40%);
}
#avatar-canvas{ width:100%; height:100%; display:block; }

.stage__status{
  position:absolute;
  left:18px; bottom:18px;
  display:flex; align-items:center; gap:8px;
  font-family:var(--font-mono);
  font-size:12px;
  color:var(--cream-dim);
  background:rgba(10,43,46,0.55);
  backdrop-filter: blur(6px);
  padding:7px 12px;
  border-radius:999px;
  border:1px solid var(--teal-line);
  max-width: calc(100% - 36px);
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.status-dot{
  width:7px;height:7px;border-radius:50%;
  background:var(--leaf);
  box-shadow:0 0 0 0 rgba(92,138,114,.6);
  animation: pulse 2.2s infinite;
  flex-shrink:0;
}
.status-dot.thinking{ background:var(--gold); }
.status-dot.talking{ background:var(--coral); }
@keyframes pulse{
  0%{ box-shadow:0 0 0 0 rgba(92,138,114,.5); }
  70%{ box-shadow:0 0 0 8px rgba(92,138,114,0); }
  100%{ box-shadow:0 0 0 0 rgba(92,138,114,0); }
}

.stage__route{ position:relative; z-index:2; margin-top:14px; opacity:.8; flex-shrink:0; }
.route-svg{ width:100%; height:24px; display:block; }

.stage__actions{
  position:relative; z-index:2;
  margin-top:18px;
  display:flex;
  gap:10px;
  flex-wrap:wrap;
}

.settings-btn{
  align-self:flex-start;
  display:flex; align-items:center; gap:8px;
  background:transparent;
  border:1px solid var(--teal-line);
  color:var(--cream-dim);
  font-family:var(--font-body);
  font-size:13px;
  padding:9px 14px;
  border-radius:999px;
  cursor:pointer;
  transition:border-color .2s, color .2s, opacity .2s;
  flex-shrink:0;
}
.settings-btn:hover{ border-color:var(--gold); color:var(--gold-soft); }
.settings-btn svg{ opacity:.8; }

#voice-toggle-btn.muted{ opacity:.6; }
#voice-toggle-btn.muted svg{ opacity:.5; }

/* ================= CHAT SIDE ================= */
.chat{
  display:flex;
  flex-direction:column;
  background:var(--ink-panel);
  padding:0;
  min-height:0; /* critical: lets this grid item's children scroll internally
                   instead of the whole column growing past the grid row */
  min-width:0;
  height:100%;
}

.chat__log{
  flex:1;
  min-height:0; /* critical: without this, flex:1 + overflow-y:auto is ignored
                   and the log pushes chips/input out of view as it grows */
  overflow-y:auto;
  -webkit-overflow-scrolling:touch;
  padding:36px 40px 12px;
  display:flex;
  flex-direction:column;
  gap:16px;
  overscroll-behavior:contain;
}
.chat__log::-webkit-scrollbar{ width:8px; }
.chat__log::-webkit-scrollbar-thumb{ background:var(--teal-line); border-radius:8px; }

.msg{
  max-width:78%;
  padding:13px 16px;
  border-radius:var(--radius-md);
  font-size:14.5px;
  line-height:1.55;
  white-space:pre-wrap;
  word-wrap:break-word;
  overflow-wrap:anywhere;
}
.msg.user{
  align-self:flex-end;
  background:var(--gold);
  color:#26210C;
  border-bottom-right-radius:4px;
}
.msg.assistant{
  align-self:flex-start;
  background:var(--ink-panel-2);
  border:1px solid var(--teal-line);
  color:var(--cream);
  border-bottom-left-radius:4px;
}
.msg.system{
  align-self:center;
  background:transparent;
  color:var(--cream-dim);
  font-family:var(--font-mono);
  font-size:12px;
  border:1px dashed var(--teal-line);
  padding:8px 14px;
}
.msg .label{
  display:block;
  font-family:var(--font-mono);
  font-size:10.5px;
  letter-spacing:.08em;
  opacity:.55;
  margin-bottom:5px;
  text-transform:uppercase;
}

.typing-dots{ display:inline-flex; gap:4px; align-items:center; }
.typing-dots span{
  width:5px;height:5px;border-radius:50%;
  background:var(--gold-soft);
  animation: blink 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2){ animation-delay:.15s; }
.typing-dots span:nth-child(3){ animation-delay:.3s; }
@keyframes blink{ 0%,80%,100%{opacity:.2;} 40%{opacity:1;} }

.chat__chips{
  display:flex; gap:10px; flex-wrap:wrap;
  padding:6px 40px 18px;
  flex-shrink:0; /* never let the log squeeze these out */
}
.chip{
  background:transparent;
  border:1px solid var(--teal-line);
  color:var(--cream-dim);
  font-family:var(--font-body);
  font-size:12.5px;
  padding:8px 14px;
  border-radius:999px;
  cursor:pointer;
  transition:.2s;
  white-space:nowrap;
}
.chip:hover{ border-color:var(--gold); color:var(--gold-soft); }

.chat__input{
  display:flex; gap:10px;
  padding:18px 24px 24px;
  border-top:1px solid var(--teal-line);
  flex-shrink:0; /* always pinned, never pushed off */
  padding-bottom: max(24px, env(safe-area-inset-bottom)); /* iOS home-indicator */
}
.chat__input input{
  flex:1;
  min-width:0;
  background:var(--ink-panel-2);
  border:1px solid var(--teal-line);
  color:var(--cream);
  font-family:var(--font-body);
  font-size:14px;
  padding:13px 16px;
  border-radius:999px;
  outline:none;
}
.chat__input input:focus{ border-color:var(--gold); }
.chat__input input::placeholder{ color:var(--cream-dim); opacity:.6; }
.chat__input button{
  width:44px; height:44px;
  border-radius:50%;
  border:none;
  background:var(--gold);
  color:#26210C;
  display:flex; align-items:center; justify-content:center;
  cursor:pointer;
  transition:transform .15s;
  flex-shrink:0;
}
.chat__input button:hover{ transform:scale(1.06); }
.chat__input button:disabled{ opacity:.5; cursor:default; transform:none; }

#mic-btn{
  background:transparent;
  border:1px solid var(--teal-line);
  color:var(--cream-dim);
}
#mic-btn:hover{ border-color:var(--gold); color:var(--gold-soft); transform:none; }
#mic-btn.recording{
  background:var(--coral);
  border-color:var(--coral);
  color:#fff;
  animation: mic-pulse 1.1s infinite;
}
@keyframes mic-pulse{
  0%{ box-shadow:0 0 0 0 rgba(232,115,74,.55); }
  70%{ box-shadow:0 0 0 10px rgba(232,115,74,0); }
  100%{ box-shadow:0 0 0 0 rgba(232,115,74,0); }
}

/* ================= DRAWER ================= */
.drawer-overlay{
  position:fixed; inset:0;
  background:rgba(6,20,22,0.6);
  opacity:0; pointer-events:none;
  transition:opacity .25s;
  z-index:10;
}
.drawer-overlay.open{ opacity:1; pointer-events:auto; }

.drawer{
  position:fixed;
  top:0; right:0; bottom:0;
  width:min(440px, 92vw);
  background:var(--ink-panel);
  border-left:1px solid var(--teal-line);
  padding:28px 28px 24px;
  transform:translateX(100%);
  transition:transform .3s ease;
  z-index:11;
  display:flex; flex-direction:column;
  overflow-y:auto;
  -webkit-overflow-scrolling:touch;
}
.drawer.open{ transform:translateX(0); }

.drawer__head{ display:flex; align-items:center; justify-content:space-between; margin-bottom:20px; }
.drawer__head h2{ font-family:var(--font-display); font-size:24px; margin:0; color:var(--cream); }
.drawer__close{ background:none; border:none; color:var(--cream-dim); font-size:26px; cursor:pointer; line-height:1; }

.drawer__section{ margin-bottom:22px; }
.drawer__section label{ display:block; font-size:13px; font-weight:600; margin-bottom:8px; color:var(--gold-soft); }
.drawer__section label .hint{ font-weight:400; color:var(--cream-dim); font-size:11.5px; }
.drawer__note{ font-size:12px; color:var(--cream-dim); line-height:1.5; margin:6px 0 10px; }
.drawer__note a{ color:var(--gold-soft); }

#api-key{
  width:100%;
  background:var(--ink-panel-2);
  border:1px solid var(--teal-line);
  color:var(--cream);
  padding:11px 13px;
  border-radius:10px;
  font-family:var(--font-mono);
  font-size:13px;
  outline:none;
}
#api-key:focus{ border-color:var(--gold); }

#knowledge-text{
  width:100%;
  min-height:260px;
  background:var(--ink-panel-2);
  border:1px solid var(--teal-line);
  color:var(--cream);
  padding:13px;
  border-radius:10px;
  font-family:var(--font-mono);
  font-size:12.5px;
  line-height:1.6;
  resize:vertical;
  outline:none;
}
#knowledge-text:focus{ border-color:var(--gold); }

.drawer__actions{ display:flex; gap:10px; margin-top:auto; padding-top:10px; }
.btn{
  flex:1;
  padding:12px 16px;
  border-radius:10px;
  font-family:var(--font-body);
  font-size:13.5px;
  font-weight:600;
  cursor:pointer;
  border:1px solid transparent;
}
.btn--primary{ background:var(--gold); color:#26210C; }
.btn--primary:hover{ background:var(--gold-soft); }
.btn--ghost{ background:transparent; border-color:var(--teal-line); color:var(--cream-dim); }
.btn--ghost:hover{ border-color:var(--coral); color:var(--coral); }

/* ================= RESPONSIVE ================= */

/* Tablets / small laptops: keep two columns but narrower stage */
@media (max-width: 1080px) and (min-width: 861px){
  .stage{ padding:32px 30px 26px; }
  .stage__title{ font-size:44px; }
  .chat__log{ padding:28px 26px 10px; }
}

/* Phones & narrow windows: stack vertically, stage collapses to a compact header */
@media (max-width: 860px){
  body{ overflow:auto; } /* let the page itself handle scroll edge-cases (e.g. keyboard) */
  .app{
    grid-template-columns:1fr;
    grid-template-rows: minmax(0, 34vh) minmax(0, 1fr);
    grid-template-rows: minmax(0, 34dvh) minmax(0, 1fr);
    height:100vh;
    height:100dvh;
  }
  .stage{
    border-right:none;
    border-bottom:1px solid var(--teal-line);
    padding:18px 20px 14px;
    flex-direction:row;
    flex-wrap:wrap;
    align-items:center;
    gap:12px;
  }
  .stage__header{ flex:1 1 auto; min-width:0; }
  .stage__title{ font-size:30px; }
  .stage__subtitle{ display:none; } /* saves vertical space on small screens */
  .stage__canvas-wrap{
    flex:0 0 88px;
    width:88px;
    height:88px;
    margin-top:0;
    order:2;
  }
  .stage__route{ display:none; } /* decorative only, drop it to reclaim space */
  .stage__status{ display:none; } /* avatar status not critical on mobile, avoids clutter/overlap */
  .stage__actions{
    order:3;
    margin-top:0;
    gap:8px;
  }
  .settings-btn{
    font-size:0;
    padding:9px;
    gap:0;
  }
  .settings-btn svg{ margin:0; }
  #voice-toggle-label{ display:none; } /* icon-only on small screens */

  .chat{ min-height:0; }
  .chat__log{ padding:18px 16px 8px; gap:12px; }
  .msg{ max-width:88%; font-size:14px; }
  .chat__chips{ padding:6px 16px 12px; }
  .chip{ font-size:12px; padding:7px 12px; }
  .chat__input{ padding:12px 14px; padding-bottom:max(12px, env(safe-area-inset-bottom)); }
}

/* Very small phones */
@media (max-width: 400px){
  .stage__title{ font-size:26px; }
  .stage__canvas-wrap{ flex-basis:64px; width:64px; height:64px; }
  .chat__chips{ gap:6px; }
  .chip{ font-size:11px; padding:6px 10px; }
  .msg{ max-width:92%; font-size:13.5px; padding:11px 13px; }
}

/* Short viewports (landscape phones, or keyboard open) */
@media (max-height: 480px) and (max-width: 900px){
  .app{ grid-template-rows: minmax(0, 84px) minmax(0, 1fr); }
  .stage{ padding:10px 16px; }
  .stage__subtitle, .stage__route, .stage__status{ display:none; }
  .stage__canvas-wrap{ flex-basis:56px; width:56px; height:56px; }
  .stage__title{ font-size:20px; margin:0; }
  .chat__chips{ display:none; } /* reclaim space once user is mid-conversation */
}

@media (prefers-reduced-motion: reduce){
  .status-dot{ animation:none; }
  .typing-dots span{ animation:none; }
}