/* Mobile-first responsive layout */
@media(max-width: 900px) {
  body { display: block; margin: 0; padding: 0; }
  .app { max-width: 100vw; margin: 0; padding: 8px; grid-template-rows: auto 1fr auto; grid-template-columns: 1fr; }
  .header { flex-direction: row; gap: 10px; padding: 10px 8px; font-size: 16px; }
  .avatar { width: 48px; height: 48px; min-width: 48px; min-height: 48px; }
  .title h1 { font-size: 18px; }
  .title p { font-size: 12px; }
  .header-controls { gap: 4px; }
  .chat { padding: 8px; font-size: 15px; }
  .msg .bubble { padding: 10px 10px; font-size: 15px; }
  .composer { flex-direction: column; gap: 8px; padding: 0; }
  #composer-form { flex-direction: row; gap: 6px; }
  #prompt { padding: 10px 10px; font-size: 15px; }
  button { padding: 10px 12px; font-size: 15px; }
  .tts { font-size: 13px; }
  .preview-side {
    position: static;
    width: 100vw;
    max-width: 100vw;
    height: auto;
    margin: 0;
    border-radius: 0;
    box-shadow: none;
    top: auto;
    right: auto;
    z-index: 10;
    display: none !important; /* hide preview on mobile --- 3D preview is disabled on phones */
  }
  .preview-side-panel {
    padding: 8px;
    border-radius: 0;
    box-shadow: none;
    height: auto;
    min-height: 220px;
  }
  .preview-side-body {
    padding: 0;
    min-height: 180px;
    height: 220px;
    max-height: 60vw;
  }
  .preview-side-body canvas {
    width: 100vw !important;
    height: 180px !important;
    min-height: 120px;
    object-fit: contain;
    display: block;
    margin: 0 auto;
  }
  .preview-controls-top {
    gap: 6px;
    margin-left: 0;
    font-size: 14px;
  }
  .preview-controls-top input[type='range'] {
    width: 100px;
    touch-action: pan-x;
  }
  .modal-panel { width: 98vw; max-width: 98vw; padding: 8px; }
}
:root {
  color-scheme: dark;
  --bg: #0f1113;
  --panel: #15181b;
  --text: #f1f4f6;
  --muted: #9aa3ad;
  --accent: #7AC943; /* Dynavap green */
  --bubble-user: #1a1f24;
  --bubble-bot: #12171b;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.45 'Barlow', system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }

.app { max-width: 920px; margin: 0 auto; height: 100%; display: grid; grid-template-rows: auto 1fr auto; gap: 12px; padding: 16px; }

/* Reserve space for the right-side preview beside the app on wide screens */
@media(min-width: 1000px) {
  body { display: flex; gap: 18px; align-items: flex-start; }
  /* Lock the left/chat column width so adding messages won't change layout.
     Reserve fixed space for the right preview (420px) and set the app's
     max-width to the remaining space. This keeps the chat column stable. */
  .app { max-width: calc(100% - 460px); width: calc(100% - 460px); margin-left: 18px; }
  .preview-side { right: 18px; top: 18px; }
}

.header { display: flex; gap: 16px; align-items: center; background: var(--panel); border-radius: 12px; padding: 14px 18px; border: 1px solid #ffffff10; }
.title h1 { margin: 0; font-size: 22px; font-family: 'Epilogue', 'Barlow', system-ui, sans-serif; font-weight: 800; letter-spacing: 0.02em; }
.title p { margin: 4px 0 0 0; color: var(--muted); font-size: 14px; }

.avatar { width: 64px; height: 64px; flex: 0 0 64px; position: relative; overflow: hidden; border-radius: 8px; background: #0c0e10; outline: 2px solid #22282e; }
.avatar img { width: 100%; height: 100%; object-fit: cover; transform-origin: center; }
.avatar canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; display: block; }

/* Avatar preview tweaks */
.avatar:hover { outline-color: #2b6a2b; }
.avatar .preview-hint { position: absolute; right: 4px; bottom: 4px; background: #00000055; color: #fff; padding: 2px 6px; border-radius: 6px; font-size: 11px; }

/* Loading overlay */
.avatar-overlay { position: absolute; left: 0; top: 0; right: 0; bottom: 0; display:flex; align-items:center; justify-content:center; flex-direction:column; gap:6px; background: linear-gradient(180deg, rgba(0,0,0,0.35), rgba(0,0,0,0.55)); border-radius: 8px; }
.avatar-overlay .overlay-text { color: #cfe9cf; font-size: 12px; }
.spinner { width: 26px; height: 26px; border-radius: 50%; border: 3px solid rgba(255,255,255,0.12); border-top-color: var(--accent); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Header controls */
.header-controls { margin-left: auto; display:flex; gap:8px; }
.header-controls button { background: transparent; color: var(--text); border: 1px solid rgba(255,255,255,0.04); padding:8px 10px; border-radius:8px; cursor:pointer; }

/* Hide preview button and debug preview controls in production */
#preview-btn { display: none !important; }
#preview-controls { display: none !important; }

/* Modal styles */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 9998; }
#modal-root .modal { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; z-index: 9999; pointer-events: none; }
#modal-root .modal[aria-hidden="false"], #modal-root .modal.is-open { display:flex; pointer-events: auto; }
.modal-panel { width: min(920px, 92%); max-width: 920px; background: var(--panel); border-radius: 12px; padding: 16px; border: 1px solid #ffffff10; box-shadow: 0 10px 30px rgba(0,0,0,0.6); }
.modal-header { display:flex; align-items:center; justify-content:space-between; gap:12px; }
.modal-header h3 { margin:0; }
.modal-close { background:transparent; border:0; font-size:20px; cursor:pointer; color:var(--muted); }
.modal-body { margin-top:12px; }

/* Preview specific */
#preview-body { min-height: 500px; display:flex; align-items:center; justify-content:center; }

.preview-controls-top { display:flex; align-items:center; gap:8px; margin-left: auto; }
.preview-controls-top label { font-size: 13px; color: var(--muted); margin-right: 6px; }
.preview-controls-top input[type='range'] { width: 140px; }

/* Right-side docked preview */
.preview-side { position: fixed; right: 18px; top: 18px; width: 420px; max-width: 42%; height: calc(100% - 36px); z-index: 9997; display: none; pointer-events: auto; }
.preview-side[aria-hidden="false"], .preview-side.is-open { display: block; }
.preview-side-panel { background: var(--panel); border-radius: 12px; padding: 12px; border: 1px solid #ffffff10; box-shadow: 0 10px 30px rgba(0,0,0,0.6); height: 100%; display:flex; flex-direction:column; }
.preview-side-body { flex: 1; display:flex; align-items:center; justify-content:center; overflow: hidden; padding: 4px; }
.preview-side-body canvas { width: 100% !important; height: 100% !important; object-fit: contain; display:block; }

/* Idle breathing */
.avatar.idle { animation: breathe 4s ease-in-out infinite; }
@keyframes breathe {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-1.5px); }
}

/* Speaking pulse */
.avatar.speaking { animation: speakpulse 700ms ease-in-out infinite; }
@keyframes speakpulse {
  0%, 100% { box-shadow: 0 0 0 2px #1a201f, 0 0 0 6px #0e1512; transform: scale(1); }
  50% { box-shadow: 0 0 0 2px #25342a, 0 0 0 8px #15251a; transform: scale(1.02); }
}

.chat { background: var(--panel); border-radius: 12px; padding: 12px; overflow: auto; border: 1px solid #ffffff10; width: 100%; max-width: 100%; }
.msg { display: flex; gap: 10px; margin: 8px 0; }
.msg .bubble { max-width: 720px; width: auto; padding: 12px 14px; border-radius: 10px; white-space: pre-wrap; border: 1px solid #ffffff10; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--bubble-user); }
.msg.bot .bubble { background: var(--bubble-bot); }
.msg .meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.msg .sources { color: var(--muted); font-size: 12px; margin-top: 6px; }
.msg .sources a { color: var(--accent); text-decoration: none; }
.msg .sources a:hover { text-decoration: underline; }

.composer { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#composer-form { display: flex; gap: 8px; flex: 1; }
#prompt { flex: 1; padding: 12px 14px; border-radius: 8px; border: 1px solid #ffffff12; background: #0f1215; color: var(--text); box-shadow: 0 1px 0 #ffffff06 inset; }
button { background: var(--accent); color: #0b1d09; border: 0; padding: 12px 16px; border-radius: 8px; font-weight: 700; cursor: pointer; text-transform: uppercase; letter-spacing: 0.04em; font-family: 'Epilogue', 'Barlow', system-ui, sans-serif; }
button:hover { filter: brightness(1.05); }
button:active { transform: translateY(1px); }
.tts { color: var(--muted); font-size: 14px; }

.typing { color: var(--muted); font-style: italic; }
