/* ===== Reset & base ===== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
html { -webkit-text-size-adjust: 100%; }

:root {
  --bg:           #fafafa;
  --bg-elevated:  #ffffff;
  --bg-input:     #f1f1f3;
  --text:         #0a0a0c;
  --text-muted:   #6b6b73;
  --border:       #e4e4e8;
  --accent:       #0a84ff;
  --accent-text:  #ffffff;
  --user-bubble:  #0a84ff;
  --user-text:    #ffffff;
  --asst-bubble:  #f1f1f3;
  --asst-text:    #0a0a0c;
  --error:        #ff3b30;
  --shadow:       0 1px 3px rgba(0,0,0,.04), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg:    0 10px 30px rgba(0,0,0,.10), 0 2px 6px rgba(0,0,0,.06);
  --radius:       14px;
  --radius-sm:    10px;
  --radius-lg:    20px;
  --topbar-h:     54px;
  --safe-top:     env(safe-area-inset-top, 0px);
  --safe-bottom:  env(safe-area-inset-bottom, 0px);
  --safe-left:    env(safe-area-inset-left, 0px);
  --safe-right:   env(safe-area-inset-right, 0px);

  /* iOS-style smooth easings */
  --ease-out:     cubic-bezier(.16,1,.3,1);
  --ease-in:      cubic-bezier(.6,0,.84,0);
  --ease-spring:  cubic-bezier(.34,1.56,.64,1);
}

:root[data-theme="dark"], :root:not([data-theme]) {
  --bg:           #0a0a0c;
  --bg-elevated:  #1a1a1e;
  --bg-input:     #1f1f24;
  --text:         #f1f1f3;
  --text-muted:   #8e8e93;
  --border:       #28282d;
  --accent:       #0a84ff;
  --accent-text:  #ffffff;
  --user-bubble:  #0a84ff;
  --user-text:    #ffffff;
  --asst-bubble:  #1f1f24;
  --asst-text:    #f1f1f3;
  --error:        #ff453a;
  --shadow:       0 1px 3px rgba(0,0,0,.5);
  --shadow-lg:    0 14px 40px rgba(0,0,0,.5);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg:           #fafafa;
    --bg-elevated:  #ffffff;
    --bg-input:     #f1f1f3;
    --text:         #0a0a0c;
    --text-muted:   #6b6b73;
    --border:       #e4e4e8;
    --asst-bubble:  #f1f1f3;
    --asst-text:    #0a0a0c;
  }
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.error { color: var(--error); }

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  padding: 0;
}
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

textarea, input {
  font: inherit;
  color: inherit;
  background: var(--bg-input);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 12px 14px;
  outline: none;
  -webkit-appearance: none;
  width: 100%;
}
textarea { resize: none; line-height: 1.4; }
textarea:focus, input:focus { border-color: var(--accent); }

/* ===== Login overlay ===== */
.login-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  display: grid; place-items: center;
  z-index: 100;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  animation: fadeIn .25s var(--ease-out);
}
.login-card {
  width: min(360px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 24px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.login-logo {
  width: 64px; height: 64px;
  margin: 0 auto 16px;
  background: var(--accent);
  border-radius: 18px;
  display: grid; place-items: center;
  font-size: 32px;
  color: var(--accent-text);
  font-weight: 700;
}
.login-card h1 { margin: 0 0 6px; font-size: 22px; font-weight: 600; letter-spacing: -.01em; }
.login-card .muted { margin: 0 0 16px; font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; }
.login-card button[type="submit"] {
  background: var(--accent); color: var(--accent-text);
  padding: 12px; border-radius: var(--radius); font-weight: 600;
  transition: transform .15s var(--ease-out), opacity .15s;
}
.login-card button[type="submit"]:active { transform: scale(.98); opacity: .9; }
.login-card .error { margin-top: 12px; font-size: 14px; }

/* ===== App layout ===== */
.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr;
  grid-template-areas:
    "topbar"
    "main"
    "composer";
  height: 100dvh;
  height: 100vh;
}

.topbar {
  grid-area: topbar;
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  padding-top: calc(8px + var(--safe-top));
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  height: calc(var(--topbar-h) + var(--safe-top));
  position: sticky; top: 0; z-index: 10;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 6px; }
.brand { font-weight: 600; letter-spacing: -.01em; }
.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 10px;
  font-size: 20px;
  transition: background .15s;
}
.icon-btn:active, .icon-btn:hover { background: var(--bg-input); }

/* ===== Sidebar (desktop static, mobile drawer) ===== */
.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: 300px;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform .3s var(--ease-out);
  z-index: 30;
  display: flex; flex-direction: column;
  gap: 18px;
}
.sidebar.open { transform: translateX(0); }
.sidebar-scrim {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 20;
  opacity: 0; pointer-events: none;
  transition: opacity .25s var(--ease-out);
}
.sidebar-scrim.open { opacity: 1; pointer-events: auto; }
.sidebar-section h3 {
  margin: 0 0 8px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-muted);
}
.sidebar-section.bottom { margin-top: auto; padding-top: 16px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }
.ghost {
  padding: 10px 12px; border-radius: var(--radius-sm);
  text-align: left; font-size: 14px;
  transition: background .15s;
}
.ghost:hover, .ghost:active { background: var(--bg-input); }

.templates-list { display: flex; flex-direction: column; gap: 4px; }
.template-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  font-size: 14px;
  transition: background .15s var(--ease-out);
  text-align: left; width: 100%;
}
.template-item:hover, .template-item:active { background: var(--bg-input); }
.template-item.active { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.template-icon { font-size: 18px; line-height: 1; flex-shrink: 0; }
.template-label { font-weight: 500; }

/* ===== Main ===== */
.main {
  grid-area: main;
  overflow-y: auto;
  padding: 16px;
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
  scroll-behavior: smooth;
  position: relative;
}

.active-template {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  margin: 0 auto 12px;
  padding: 8px 14px;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  border-radius: 999px;
  max-width: 720px;
  font-size: 14px;
  font-weight: 500;
  animation: slideDown .25s var(--ease-out);
}
.active-template button {
  font-size: 20px; line-height: 1;
  width: 24px; height: 24px;
  border-radius: 50%;
  display: grid; place-items: center;
  transition: background .15s;
}
.active-template button:hover { background: color-mix(in srgb, var(--accent) 20%, transparent); }

/* Empty state */
.empty-state {
  max-width: 720px; margin: 60px auto 0;
  text-align: center;
  animation: fadeIn .5s var(--ease-out);
}
.empty-logo {
  width: 64px; height: 64px;
  margin: 0 auto 16px;
  background: var(--accent);
  border-radius: 18px;
  display: grid; place-items: center;
  font-size: 32px;
  color: var(--accent-text);
  font-weight: 700;
}
.empty-state h2 { margin: 0 0 6px; font-size: 22px; font-weight: 600; letter-spacing: -.01em; }
.empty-state .muted { margin: 0 0 24px; }
.empty-templates {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
  margin-top: 16px;
  text-align: left;
}
.empty-template-card {
  padding: 12px 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex; gap: 10px; align-items: flex-start;
  transition: transform .15s var(--ease-out), border-color .15s;
}
.empty-template-card:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); transform: translateY(-1px); }
.empty-template-card:active { transform: translateY(0) scale(.98); }
.empty-template-card .template-icon { font-size: 22px; margin-top: 2px; }
.empty-template-card .stack { display: flex; flex-direction: column; min-width: 0; }
.empty-template-card .stack b { font-size: 14px; font-weight: 600; line-height: 1.2; }
.empty-template-card .stack span { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Messages */
.messages {
  max-width: 720px;
  margin: 0 auto;
  display: flex; flex-direction: column;
  gap: 10px;
  padding-bottom: 24px;
}
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.45;
  animation: bubblePop .25s var(--ease-spring);
}
.bubble.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--asst-bubble);
  color: var(--asst-text);
  border-bottom-left-radius: 6px;
}
.bubble.assistant.streaming::after {
  content: "▍";
  display: inline-block;
  margin-left: 2px;
  animation: caret 1s infinite steps(2);
  color: var(--accent);
}
.bubble.error {
  background: color-mix(in srgb, var(--error) 14%, var(--bg-elevated));
  color: var(--error);
  align-self: flex-start;
  font-size: 14px;
  border-radius: var(--radius);
}

/* Render code blocks inside bubbles */
.bubble pre {
  background: rgba(0,0,0,.25);
  padding: 10px 12px;
  border-radius: 10px;
  overflow-x: auto;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 13px;
  margin: 8px 0;
}
.bubble.assistant pre { background: rgba(0,0,0,.30); color: #f1f1f3; }
.bubble code:not(pre code) {
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: .92em;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(128,128,128,.20);
}

/* Composer */
.composer {
  grid-area: composer;
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
}
.composer-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.composer textarea {
  max-height: 200px;
  min-height: 44px;
}
.send-btn {
  background: var(--accent);
  color: var(--accent-text);
  width: 44px; height: 44px; flex-shrink: 0;
  border-radius: 50%;
  font-size: 22px;
  display: grid; place-items: center;
  transition: transform .12s var(--ease-out), opacity .15s;
}
.send-btn:active { transform: scale(.9); }
.send-btn:disabled { opacity: .35; cursor: default; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 40;
  display: grid; place-items: center;
  padding: 16px;
  animation: fadeIn .25s var(--ease-out);
}
.modal-inner {
  width: min(640px, 100%);
  max-height: 80vh;
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: modalIn .3s var(--ease-spring);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.modal-body {
  padding: 16px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.6;
}
.modal-body pre {
  background: var(--bg-input);
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  white-space: pre-wrap;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 12.5px;
}

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideDown { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes bubblePop { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes modalIn { from { opacity: 0; transform: scale(.96); } to { opacity: 1; transform: scale(1); } }
@keyframes caret { 0%, 50% { opacity: 1; } 50.01%, 100% { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ===== Desktop (>= 900px): static sidebar ===== */
@media (min-width: 900px) {
  .app {
    grid-template-columns: 280px 1fr;
    grid-template-areas:
      "sidebar topbar"
      "sidebar main"
      "sidebar composer";
  }
  .sidebar {
    grid-area: sidebar;
    position: static;
    transform: none;
    border-right: 1px solid var(--border);
    background: var(--bg);
    width: auto;
  }
  .sidebar-scrim { display: none; }
  #menu-btn { display: none; }
  .topbar { border-top-left-radius: 0; }
}
