:root { --app-height: 100dvh; }

html,
body {
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
}

body,
main {
  height: var(--app-height);
}

main {
  min-width: 0;
  grid-template-rows: 64px minmax(0, 1fr) auto;
}

button { touch-action: manipulation; }

@media (max-width: 760px) {
  #sidebar {
    width: min(88vw, 318px);
    padding: max(14px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  }

  .mobile-menu {
    display: grid;
    place-items: center;
    top: max(12px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    width: 44px;
    height: 44px;
    border-radius: 12px;
    color: var(--text);
    background: color-mix(in srgb,var(--panel) 80%,transparent);
    border: 1px solid var(--line);
    backdrop-filter: blur(14px);
  }
  .mobile-menu svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

  main {
    width: 100%;
    grid-template-rows: calc(58px + env(safe-area-inset-top)) minmax(0, 1fr) auto;
  }

  header {
    padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) 0 62px;
  }

  header button { width: 44px; height: 44px; border-radius: 12px; }

  .messages {
    min-height: 0;
    padding: 16px max(14px, env(safe-area-inset-right)) 12px max(14px, env(safe-area-inset-left));
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .composer {
    position: relative;
    z-index: 2;
    padding: 8px max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    background: var(--bg);
  }

  .composer-inner { min-height: 54px; border-radius: 17px; padding: 8px 8px 8px 14px; }
  .composer textarea { max-height: 120px; font-size: 16px; }
  .composer > small { margin-top: 6px; }
  .welcome { margin: clamp(8px, 3vh, 28px) auto 0; }
  .hero-orb { width: 52px; height: 52px; border-radius: 17px; font-size: 21px; }
  .welcome h1 { margin: 18px 0 8px; font-size: clamp(27px, 8vw, 36px); }
  .welcome > p { max-width: 34ch; margin: 0 auto; font-size: 14px; line-height: 1.45; }
  .suggestions { gap: 8px; margin-top: 22px; }
  .suggestions button { min-height: 48px; padding: 12px 14px; }
  .message { gap: 10px; margin-bottom: 22px; }
  .avatar { flex-basis: 30px; height: 30px; }
  .bubble { max-width: calc(100% - 40px); font-size: 15px; line-height: 1.6; }
  .message.user .bubble { padding: 10px 13px; }
  .a1 { left: -180px; }
  .a2 { right: -290px; }
}

@media (max-width: 380px), (max-height: 680px) {
  .messages { padding-top: 10px; }
  .welcome { margin-top: 0; }
  .hero-orb { width: 44px; height: 44px; border-radius: 14px; }
  .welcome h1 { margin-top: 12px; font-size: 26px; }
  .welcome > p { font-size: 13px; }
  .suggestions { gap: 6px; margin-top: 14px; }
  .suggestions button { min-height: 43px; padding: 10px 12px; font-size: 13px; }
  .composer > small { display: none; }
}

@media (hover: hover) {
  header button:hover,
  .mobile-menu:hover { background: rgba(255, 255, 255, .06); }
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: max(20px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  background: radial-gradient(circle at 50% 10%, rgba(139, 92, 246, .22), transparent 34%), #090b10;
  transition: opacity .45s ease, visibility .45s ease;
}

.auth-gate.authenticated { opacity: 0; visibility: hidden; pointer-events: none; }
.auth-glow { position: absolute; width: 320px; height: 320px; border-radius: 50%; background: #22d3ee; filter: blur(150px); opacity: .09; animation: glowFloat 5s ease-in-out infinite; }
.auth-card { position: relative; width: min(100%, 420px); margin-top: clamp(-64px,-6vh,-28px); text-align: center; animation: authEnter .75s cubic-bezier(.2,.8,.2,1) both; }
.auth-logo { display: grid; place-items: center; width: 68px; height: 68px; margin: 0 auto 24px; border: 1px solid rgba(255,255,255,.16); border-radius: 23px; background: linear-gradient(145deg, rgba(139,92,246,.9), rgba(34,211,238,.58)); box-shadow: 0 24px 70px rgba(108,76,237,.34); font-size: 27px; animation: logoPulse 2.8s ease-in-out infinite; }
.auth-card h1 { margin: 0 0 10px; font-size: clamp(29px, 8vw, 42px); animation: textReveal .7s .14s ease both; }
.auth-card > p { max-width: 34ch; margin: 0 auto; color: var(--muted); line-height: 1.55; animation: textReveal .7s .22s ease both; }
.code-inputs { display: grid; grid-template-columns: repeat(6, 1fr); gap: clamp(6px, 2vw, 10px); margin: 32px auto 18px; animation: textReveal .7s .3s ease both; }
#turnstile-box { width: 100%; min-height: 65px; margin: 0 auto 14px; display: flex; justify-content: center; }
.code-inputs input { width: 100%; min-width: 0; aspect-ratio: .82; border: 1px solid rgba(255,255,255,.12); border-radius: 14px; outline: 0; background: rgba(18,22,32,.82); color: white; font: 700 clamp(22px, 7vw, 30px) Manrope; text-align: center; caret-color: var(--cyan); transition: border-color .2s, transform .2s, box-shadow .2s; }
.code-inputs input:focus { border-color: rgba(139,92,246,.85); transform: translateY(-2px); box-shadow: 0 10px 30px rgba(139,92,246,.16); }
.auth-submit { width: 100%; min-height: 52px; border: 0; border-radius: 15px; background: linear-gradient(135deg,#8b5cf6,#4f7de8); color: #fff; font-weight: 700; box-shadow: 0 16px 40px rgba(79,125,232,.2); animation: textReveal .7s .38s ease both; }
.auth-submit:disabled { opacity: .65; cursor: wait; }
.auth-error { min-height: 20px; margin: 13px 0 0; color: #fb7185; font-size: 13px; font-weight: 700; transition: all .25s ease; }
.auth-error:not(:empty) { padding: 12px 14px; border: 1px solid rgba(251,113,133,.35); border-radius: 12px; background: rgba(225,29,72,.12); box-shadow: 0 12px 32px rgba(225,29,72,.10); animation: errorIn .28s ease both; }
.auth-gate.shake .auth-card { animation: shake .38s ease; }
.app-shell-locked main,.app-shell-locked #sidebar,.app-shell-locked .mobile-menu { visibility: hidden; }

@keyframes authEnter { from { opacity: 0; transform: translateY(22px) scale(.97); } to { opacity: 1; transform: none; } }
@keyframes textReveal { from { opacity: 0; transform: translateY(12px); filter: blur(7px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes logoPulse { 50% { transform: translateY(-5px) rotate(4deg); box-shadow: 0 30px 85px rgba(108,76,237,.43); } }
@keyframes glowFloat { 50% { transform: translate(35px,-20px) scale(1.14); } }
@keyframes shake { 20%,60% { transform: translateX(-7px); } 40%,80% { transform: translateX(7px); } }
@keyframes errorIn { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .auth-card,.auth-logo,.auth-card h1,.auth-card>p,.code-inputs,.auth-submit,.auth-glow { animation: none; }
}

@media (max-height:680px) {
  .auth-card { margin-top: -18px; }
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f8fafc;
    --side: #ffffff;
    --panel: #ffffff;
    --line: rgba(15,23,42,.10);
    --text: #0f172a;
    --muted: #64748b;
    --violet: #7c3aed;
    --cyan: #0891b2;
  }

  body { background: #f8fafc; }
  #sidebar { background: rgba(255,255,255,.92); box-shadow: 14px 0 40px rgba(15,23,42,.05); }
  header { background: rgba(248,250,252,.82); }
  .new-chat,
  .composer-inner,
  .suggestions button,
  .message.user .bubble { background: rgba(255,255,255,.9); color: #1e293b; box-shadow: 0 12px 32px rgba(15,23,42,.045); }
  #history button { color: #64748b; }
  #history button:hover,
  #history button.active { background: #f1f5f9; color: #0f172a; }
  .profile>span { background: #e2e8f0; }
  .bubble { color: #1e293b; }
  textarea { color: #0f172a; }
  textarea::placeholder { color: #94a3b8; }
  .composer { background: linear-gradient(transparent,#f8fafc 28%); }
  .auth-gate { background: radial-gradient(circle at 50% 10%,rgba(124,58,237,.14),transparent 35%),#f8fafc; }
  .code-inputs input { border-color: rgba(15,23,42,.12); background: rgba(255,255,255,.9); color: #0f172a; box-shadow: 0 10px 28px rgba(15,23,42,.05); }
  .auth-error:not(:empty) { color: #be123c; background: #fff1f2; border-color: #fecdd3; }
  .auth-glow { background: #7c3aed; opacity: .08; }
  .aurora { opacity: .08; }
  header button,.mobile-menu { color: #64748b; }
}

@media (prefers-color-scheme: light) and (max-width:760px) {
  .composer { background: #f8fafc; }
}

/* Mobile sidebar: keep navigation controls separate from the brand. */
.brand { justify-content: space-between; }
.brand-identity { display: flex; align-items: center; gap: 10px; }
.brand-identity>div { display:flex; flex-direction:column; gap:1px; }
.brand-identity small { color:var(--muted); font:500 10px "DM Sans"; letter-spacing:.01em; }
.brand-identity span { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 11px; background: linear-gradient(135deg,var(--violet),var(--cyan)); box-shadow: 0 10px 30px rgba(139,92,246,.28); }
.sidebar-close { display: none; width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 12px; background:rgba(255,255,255,.025); color: var(--muted); }
.sidebar-close svg { width:18px;height:18px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round; }
.new-chat { display:flex;align-items:center;gap:10px;padding:0 12px;background:linear-gradient(135deg,rgba(139,92,246,.14),rgba(34,211,238,.06));border-color:rgba(139,92,246,.22);box-shadow:0 10px 28px rgba(20,15,50,.1); }
.new-chat svg { width:18px;height:18px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round; }
.new-chat span { flex:1; }
.new-chat kbd { display:grid;place-items:center;width:21px;height:21px;border:1px solid var(--line);border-radius:6px;color:var(--muted);font:600 12px "DM Sans"; }
.side-title { display:flex;align-items:center;gap:10px;margin:22px 9px 8px; }
.side-title i { height:1px;flex:1;background:var(--line); }
#history { padding-right:2px; }
#history button { display:flex;align-items:center;gap:9px;min-height:42px;padding:9px 10px;border-radius:11px; }
#history button svg { flex:0 0 16px;width:16px;height:16px;fill:none;stroke:currentColor;stroke-width:1.65;stroke-linejoin:round; }
#history button span { overflow:hidden;text-overflow:ellipsis;white-space:nowrap; }
#history button.active { background:linear-gradient(90deg,rgba(139,92,246,.13),rgba(139,92,246,.035));box-shadow:inset 2px 0 #8b5cf6; }
.history-empty { display:grid;place-items:center;text-align:center;padding:34px 12px;color:var(--muted); }
.history-empty>span { display:grid;place-items:center;width:38px;height:38px;margin-bottom:9px;border:1px solid var(--line);border-radius:12px;background:rgba(139,92,246,.06);color:#9b87f5; }
.history-empty p { margin:0;font-size:12px;font-weight:600;color:var(--text); }.history-empty small{margin-top:3px;font-size:10px;}
.profile { border:1px solid var(--line);border-radius:14px;background:rgba(255,255,255,.025);padding:10px; }
.profile>i { width:7px;height:7px;margin-left:auto;border-radius:50%;background:#48d59b;box-shadow:0 0 0 4px rgba(72,213,155,.09); }

@media (max-width: 760px) {
  #sidebar.open { box-shadow: 24px 0 70px rgba(3,7,18,.3); }
  #history { flex: 1; min-height: 0; }
  .brand { min-height: 58px; padding: 2px 6px 18px 8px; }
  .brand-identity { gap: 12px; }
  .brand-identity span { width: 42px; height: 42px; border-radius: 14px; }
  .sidebar-close { display: grid; place-items: center; }
  .sidebar-close:active { background: rgba(139,92,246,.12); transform: scale(.96); }
  .mobile-menu.sidebar-open { opacity: 0; visibility: hidden; pointer-events: none; }
}
