/* ---------------------------------------------------------------
   Grassi AI — design tokens
--------------------------------------------------------------- */
:root {
  --bg: #f6f7fa;
  --bg-elevated: #ffffff;
  --sidebar-bg: #eef1f6;
  --border: #e1e5ec;
  --text: #1a1d24;
  --text-muted: #666e7f;
  --text-faint: #99a1b1;

  --accent: #3661e0;
  --accent-hover: #2b4fc4;
  --accent-ink: #0d1b4c;
  --accent-soft: #e8edfc;

  --user-bubble: #eef1f6;
  --assistant-bg: transparent;
  --danger: #c93f34;

  --code-bg: #1c2230;
  --code-text: #eef1f6;
  --inline-code-bg: #e6e9f0;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(20, 25, 40, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 25, 40, 0.10);

  --sidebar-w: 272px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  --bg: #14161c;
  --bg-elevated: #1b1e27;
  --sidebar-bg: #101218;
  --border: #2b2f3a;
  --text: #e7e9ee;
  --text-muted: #9aa1b1;
  --text-faint: #666d7d;

  --accent: #6f93ff;
  --accent-hover: #86a4ff;
  --accent-ink: #0b1330;
  --accent-soft: #1c2540;

  --user-bubble: #232733;
  --assistant-bg: transparent;
  --danger: #e2685f;

  --code-bg: #0f1117;
  --code-text: #eef1f6;
  --inline-code-bg: #262b37;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161c;
    --bg-elevated: #1b1e27;
    --sidebar-bg: #101218;
    --border: #2b2f3a;
    --text: #e7e9ee;
    --text-muted: #9aa1b1;
    --text-faint: #666d7d;

    --accent: #6f93ff;
    --accent-hover: #86a4ff;
    --accent-ink: #0b1330;
    --accent-soft: #1c2540;

    --user-bubble: #232733;
    --assistant-bg: transparent;
    --danger: #e2685f;

    --code-bg: #0f1117;
    --code-text: #eef1f6;
    --inline-code-bg: #262b37;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.35);
  }
}

/* ---------------------------------------------------------------
   Reset & base
--------------------------------------------------------------- */
* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
}
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, textarea, input {
  font-family: inherit;
  color: inherit;
}
button {
  border: none;
  background: none;
  cursor: pointer;
}
a { color: var(--accent); }

/* ---------------------------------------------------------------
   Login screen
--------------------------------------------------------------- */
.app { display: none; }
body.authed .app { display: grid; }
body.authed .login-screen { display: none; }

.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 32px 28px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-mark { margin-bottom: 14px; }
.login-card h1 {
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0 0 6px;
}
.login-card > p {
  color: var(--text-muted);
  font-size: 13.5px;
  margin: 0 0 20px;
}
.login-input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: var(--radius-md);
  padding: 11px 14px;
  font-size: 15px;
  outline: none;
  margin-bottom: 12px;
  text-align: center;
}
.login-input:focus { border-color: var(--accent); }
.login-btn {
  width: 100%;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--radius-md);
  padding: 11px 14px;
  transition: background 0.15s ease;
}
.login-btn:hover { background: var(--accent-hover); }
.login-btn:disabled { opacity: 0.6; cursor: default; }
.login-error {
  margin-top: 14px;
  font-size: 13px;
  color: var(--danger);
}

/* ---------------------------------------------------------------
   App shell
--------------------------------------------------------------- */
.app {
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
  overflow: hidden;
}

.scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 20;
  padding: 0;
}

/* ---------------------------------------------------------------
   Sidebar
--------------------------------------------------------------- */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 12px;
  min-height: 0;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.sidebar-close { display: none; }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, transform 0.1s ease;
}
.new-chat-btn:hover {
  border-color: var(--accent);
  color: var(--accent-hover);
}
.new-chat-btn:active { transform: scale(0.98); }

.history {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0 -4px;
  padding: 0 4px;
}

.history-group-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  padding: 12px 8px 4px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  color: var(--text-muted);
  cursor: pointer;
  position: relative;
  white-space: nowrap;
}
.history-item span.title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--text);
}
:root[data-theme="dark"] .history-item:hover { background: rgba(255,255,255,0.06); }
.history-item.active {
  background: var(--accent-soft);
  color: var(--accent-ink);
}
:root[data-theme="dark"] .history-item.active { color: var(--text); }

.history-item .delete-btn {
  opacity: 0;
  padding: 3px;
  border-radius: 6px;
  color: var(--text-faint);
  flex-shrink: 0;
}
.history-item:hover .delete-btn { opacity: 1; }
.history-item .delete-btn:hover { color: var(--danger); background: rgba(0,0,0,0.08); }

.sidebar-bottom {
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.icon-text-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  color: var(--text-muted);
  text-align: left;
}
.icon-text-btn:hover { background: rgba(0,0,0,0.05); color: var(--text); }
:root[data-theme="dark"] .icon-text-btn:hover { background: rgba(255,255,255,0.06); }
.icon-text-btn.danger:hover { color: var(--danger); }

.icon-sun, .icon-moon { display: none; }
:root:not([data-theme="dark"]) .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: none; }
  :root:not([data-theme="light"]) .icon-moon { display: block; }
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  color: var(--text-muted);
}
.icon-btn:hover { background: rgba(0,0,0,0.06); color: var(--text); }
:root[data-theme="dark"] .icon-btn:hover { background: rgba(255,255,255,0.08); }

/* ---------------------------------------------------------------
   Chat column
--------------------------------------------------------------- */
.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid transparent;
  flex-shrink: 0;
}
.sidebar-open { display: none; }

.header-title {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-muted);
}

.model-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}
.model-badge .dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: #5cb85c;
  box-shadow: 0 0 0 3px rgba(92, 184, 92, 0.2);
}

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

/* ---------------------------------------------------------------
   Welcome screen
--------------------------------------------------------------- */
.welcome {
  max-width: 680px;
  margin: 0 auto;
  padding: 12vh 20px 20px;
  text-align: center;
}
.welcome-mark { margin-bottom: 18px; }
.welcome h1 {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
}
.welcome p {
  color: var(--text-muted);
  font-size: 15px;
  margin: 0 0 28px;
}

/* ---------------------------------------------------------------
   Messages
--------------------------------------------------------------- */
.messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 8px 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg {
  display: flex;
  gap: 12px;
  padding: 14px 4px;
  animation: msg-in 0.25s ease both;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-avatar {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg.user .msg-avatar { display: none; }
.msg.assistant .msg-avatar { background: var(--accent); }
.msg.assistant .msg-avatar svg { display: block; }

.msg-body { min-width: 0; flex: 1; }

.msg.user {
  justify-content: flex-end;
}
.msg.user .msg-body {
  flex: 0 1 auto;
  max-width: 80%;
  background: var(--user-bubble);
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  border-bottom-right-radius: 6px;
}

.msg.assistant .msg-body {
  padding-top: 3px;
}

.msg-content {
  font-size: 15px;
  line-height: 1.65;
  word-wrap: break-word;
}
.msg-content > *:first-child { margin-top: 0; }
.msg-content > *:last-child { margin-bottom: 0; }
.msg-content p { margin: 0 0 12px; }
.msg-content ul, .msg-content ol { margin: 0 0 12px; padding-left: 22px; }
.msg-content li { margin: 4px 0; }
.msg-content h1, .msg-content h2, .msg-content h3 {
  font-weight: 600;
  margin: 18px 0 8px;
  letter-spacing: -0.01em;
}
.msg-content h1 { font-size: 20px; }
.msg-content h2 { font-size: 17px; }
.msg-content h3 { font-size: 15.5px; }
.msg-content blockquote {
  margin: 0 0 12px;
  padding: 2px 14px;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
}
.msg-content strong { font-weight: 600; }
.msg-content code {
  background: var(--inline-code-bg);
  padding: 2px 5px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 0.87em;
}
.msg-content a { text-decoration: underline; text-underline-offset: 2px; }

.code-block {
  margin: 8px 0 14px;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--code-bg);
  box-shadow: var(--shadow-sm);
}
.code-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  font-size: 11.5px;
  color: #b9b0a3;
  background: rgba(255,255,255,0.04);
  font-family: var(--font-mono);
}
.code-block-head button {
  color: #b9b0a3;
  font-size: 11.5px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 6px;
  border-radius: 5px;
}
.code-block-head button:hover { color: #fff; background: rgba(255,255,255,0.08); }
.code-block pre {
  margin: 0;
  padding: 14px;
  overflow-x: auto;
}
.code-block code {
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  color: var(--code-text);
  background: none;
  padding: 0;
}

.msg-content code {
  white-space: pre-wrap;
  word-break: break-word;
}
.code-block code { white-space: pre; word-break: normal; }

/* streaming cursor */
.cursor::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 1em;
  margin-left: 2px;
  background: var(--accent);
  vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
  border-radius: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

.msg-actions {
  display: flex;
  gap: 4px;
  margin-top: 6px;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.msg.assistant:hover .msg-actions,
.msg.assistant.actions-visible .msg-actions { opacity: 1; }
.msg-actions button {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px;
  border-radius: 7px;
  font-size: 12px;
  color: var(--text-faint);
}
.msg-actions button:hover { background: rgba(0,0,0,0.06); color: var(--text); }
:root[data-theme="dark"] .msg-actions button:hover { background: rgba(255,255,255,0.08); }

.error-banner {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--accent-soft);
  color: var(--accent-ink);
  border: 1px solid var(--accent);
  font-size: 13.5px;
  line-height: 1.5;
}
:root[data-theme="dark"] .error-banner { color: var(--text); }
.error-banner code {
  background: rgba(0,0,0,0.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--font-mono);
}

/* typing dots */
.typing-dots {
  display: flex;
  gap: 4px;
  padding: 8px 0;
}
.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--text-faint);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ---------------------------------------------------------------
   Composer
--------------------------------------------------------------- */
.composer-wrap {
  padding: 6px 20px 16px;
  flex-shrink: 0;
}
.composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 18px;
  box-shadow: var(--shadow-md);
}
.composer:focus-within { border-color: var(--accent); }

.composer-input {
  flex: 1;
  border: none;
  outline: none;
  background: none;
  resize: none;
  font-size: 15px;
  line-height: 1.5;
  max-height: 200px;
  padding: 8px 0;
}
.composer-input::placeholder { color: var(--text-faint); }

.send-btn, .stop-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.4; cursor: default; }
.stop-btn { background: var(--text); }
:root[data-theme="dark"] .stop-btn { background: var(--accent-soft); color: var(--text); }

.hidden { display: none !important; }

.composer-hint {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
  margin-top: 10px;
}

/* ---------------------------------------------------------------
   Scrollbars
--------------------------------------------------------------- */
.history::-webkit-scrollbar, .chat-scroll::-webkit-scrollbar, pre::-webkit-scrollbar { width: 8px; height: 8px; }
.history::-webkit-scrollbar-thumb, .chat-scroll::-webkit-scrollbar-thumb { background: rgba(120,110,95,0.25); border-radius: 999px; }
pre::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 999px; }

/* ---------------------------------------------------------------
   Responsive
--------------------------------------------------------------- */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    z-index: 30;
    top: 0; bottom: 0; left: 0;
    width: min(var(--sidebar-w), 82vw);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-md);
  }
  .app.sidebar-visible .sidebar { transform: translateX(0); }
  .app.sidebar-visible .scrim { display: block; }
  .sidebar-close { display: flex; }
  .sidebar-open { display: flex; }
  .welcome { padding-top: 8vh; }
  .msg.user .msg-body { max-width: 92%; }
}

@media (min-width: 861px) {
  .sidebar-visible-desktop-hidden .sidebar { display: none; }
}
