:root {
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --fg: #0d0d0d;
  --muted: #5d5d5d;
  --faint: #8f8f8f;
  --hover: #ececec;
  --active: #e3e3e3;
  --line: #e5e5e5;
  --bubble: #f4f4f4;
  --composer: #ffffff;
  --composer-line: #e3e3e3;
  --send: #0d0d0d;
  --send-fg: #ffffff;
  --menu: #ffffff;
  --danger: #e02e2a;
  --accent: #f38020;
  --code-bg: #0d0d0d;
  --shadow: 0 4px 24px rgba(0, 0, 0, .08), 0 0 0 1px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --sidebar: #181818;
    --fg: #ececec;
    --muted: #b4b4b4;
    --faint: #8a8a8a;
    --hover: #2a2a2a;
    --active: #303030;
    --line: #333333;
    --bubble: #303030;
    --composer: #303030;
    --composer-line: #3a3a3a;
    --send: #ececec;
    --send-fg: #0d0d0d;
    --menu: #353535;
    --danger: #ff6b6b;
    --code-bg: #171717;
    --shadow: 0 4px 24px rgba(0, 0, 0, .4), 0 0 0 1px rgba(255, 255, 255, .06);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--fg);
  font: 16px/1.65 ui-sans-serif, -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--fg); }
.icon-btn.round { border-radius: 50%; border: 1px solid var(--composer-line); width: 34px; height: 34px; }

.logo-mark {
  width: 24px; height: 24px; border-radius: 7px; background: var(--accent); position: relative; flex: none;
}
.logo-mark::after {
  content: ""; position: absolute; inset: 7px 5px 5px; border: 2.5px solid #fff; border-top: 0; border-radius: 0 0 10px 10px;
}
.logo-mark.big { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 20px; }
.logo-mark.big::after { inset: 18px 13px 13px; border-width: 4px; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: min(400px, 100%); text-align: center; }
.login-card h1 { font-size: 28px; line-height: 1.25; margin: 0 0 10px; letter-spacing: -.02em; }
.login-card p { color: var(--muted); margin: 0 0 28px; }
.google-btn {
  display: flex; align-items: center; justify-content: center; gap: 12px; height: 52px; border-radius: 999px;
  border: 1px solid var(--composer-line); color: var(--fg); text-decoration: none; font-weight: 500;
}
.google-btn:hover { background: var(--hover); }
.login-error { color: var(--danger); margin-top: 16px !important; font-size: 14px; }

/* Layout */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 260px; flex: none; background: var(--sidebar); display: flex; flex-direction: column;
  padding: 8px; transition: margin-left .2s ease, transform .2s ease;
}
.app.collapsed .sidebar { margin-left: -260px; }
.sidebar-top { display: flex; justify-content: space-between; padding: 0 2px 6px; }
.new-chat {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; font-size: 14px; font-weight: 500;
}
.new-chat:hover { background: var(--hover); }
.history { flex: 1; overflow-y: auto; margin: 12px -4px 0; padding: 0 4px; }
.history h3 { font-size: 12px; font-weight: 600; color: var(--faint); margin: 18px 10px 6px; }
.history h3:first-child { margin-top: 4px; }
.convo {
  position: relative; display: flex; align-items: center; border-radius: 10px; font-size: 14px;
}
.convo a { flex: 1; min-width: 0; padding: 8px 10px; color: var(--fg); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo:hover { background: var(--hover); }
.convo.active { background: var(--active); }
.convo .more { width: 30px; height: 30px; margin-right: 2px; border-radius: 6px; display: grid; place-items: center; opacity: 0; color: var(--muted); }
.convo:hover .more, .convo.active .more, .convo .more[aria-expanded="true"] { opacity: 1; }
.convo .more:hover { color: var(--fg); }
.convo input { flex: 1; min-width: 0; margin: 4px; padding: 4px 6px; font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--accent); border-radius: 6px; outline: none; }
.empty-history { color: var(--faint); font-size: 13px; padding: 8px 10px; }

.account { position: relative; border-top: 1px solid var(--line); padding-top: 8px; margin-top: 4px; }
.account-btn { width: 100%; display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; font-size: 14px; text-align: left; }
.account-btn:hover { background: var(--hover); }
.account-btn img { width: 28px; height: 28px; border-radius: 50%; background: var(--active); }
.account-btn span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.menu {
  position: absolute; z-index: 50; min-width: 180px; padding: 6px; border-radius: 14px;
  background: var(--menu); box-shadow: var(--shadow);
}
.menu-item {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  font-size: 14px; text-align: left; color: var(--fg); text-decoration: none;
}
.menu-item svg { width: 18px; height: 18px; }
.menu-item:hover { background: var(--hover); }
.menu-item.danger { color: var(--danger); }
.menu-item .check { margin-left: auto; opacity: 0; }
.menu-item.selected .check { opacity: 1; }
.menu-label { font-size: 13px; color: var(--faint); padding: 6px 10px 8px; overflow: hidden; text-overflow: ellipsis; }
.account-menu { bottom: calc(100% + 6px); left: 0; right: 0; }
.convo-menu { position: fixed; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar { height: 56px; display: flex; align-items: center; gap: 4px; padding: 0 8px; flex: none; }
.app:not(.collapsed) #open-sidebar { display: none; }
.model-picker { position: relative; }
.model-btn { display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 10px; font-size: 18px; font-weight: 600; }
.model-btn:hover { background: var(--hover); }
.model-btn span { color: var(--faint); font-weight: 500; font-size: 16px; }
.model-btn .chev { width: 16px; height: 16px; color: var(--faint); }
.model-menu { top: calc(100% + 4px); left: 0; min-width: 260px; }
.mobile-only { display: none; margin-left: auto; }

/* Thread */
.thread { flex: 1; overflow-y: auto; scroll-behavior: auto; }
.messages { max-width: 768px; margin: 0 auto; padding: 8px 24px 32px; }
.msg { margin: 0 0 28px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble {
  background: var(--bubble); padding: 10px 18px; border-radius: 22px; max-width: 75%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg .files { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-bottom: 8px; max-width: 75%; }
.file-card {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px 8px 8px; border-radius: 14px;
  border: 1px solid var(--composer-line); background: var(--bg); font-size: 13px; max-width: 260px; color: var(--fg); text-decoration: none;
}
.file-card .ficon { width: 36px; height: 36px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.file-card .ficon svg { width: 18px; height: 18px; }
.file-card .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.file-card .fmeta { color: var(--faint); font-size: 12px; }
.file-card img.thumb { width: 36px; height: 36px; border-radius: 8px; object-fit: cover; }

.msg.assistant .content { overflow-wrap: anywhere; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content p, .content ul, .content ol, .content pre, .content table, .content blockquote { margin: 0 0 16px; }
.content h1, .content h2, .content h3 { line-height: 1.3; margin: 24px 0 12px; }
.content h1 { font-size: 24px; } .content h2 { font-size: 20px; } .content h3 { font-size: 18px; }
.content ul, .content ol { padding-left: 26px; }
.content li { margin: 4px 0; }
.content a { color: #2f7de1; }
.content hr { border: 0; border-top: 1px solid var(--line); margin: 24px 0; }
.content blockquote { border-left: 3px solid var(--line); padding-left: 14px; color: var(--muted); }
.content table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 12px; text-align: left; }
.content th { background: var(--bubble); }
.content :not(pre) > code { background: var(--bubble); padding: 2px 6px; border-radius: 6px; font-size: .875em; }
.content code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.codeblock { border-radius: 12px; overflow: hidden; background: var(--code-bg); margin: 0 0 16px; }
.codeblock-head {
  display: flex; justify-content: space-between; align-items: center; padding: 6px 12px 6px 16px;
  font-size: 12px; color: #b4b4b4; background: #2f2f2f;
}
.codeblock-head button { display: flex; align-items: center; gap: 6px; font-size: 12px; color: #b4b4b4; padding: 4px 6px; border-radius: 6px; }
.codeblock-head button:hover { color: #fff; }
.codeblock-head svg { width: 14px; height: 14px; }
.codeblock pre { margin: 0; padding: 16px; overflow-x: auto; font-size: 13.5px; line-height: 1.55; color: #e6e6e6; }
.codeblock pre code.hljs { background: none; padding: 0; }

.cursor::after { content: ""; display: inline-block; width: 10px; height: 10px; margin-left: 4px; border-radius: 50%; background: var(--fg); vertical-align: middle; animation: pulse 1s ease-in-out infinite; }
.thinking { display: inline-block; width: 12px; height: 12px; border-radius: 50%; background: var(--fg); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(.6); opacity: .5; } }

.actions { display: flex; gap: 2px; margin-top: 8px; margin-left: -6px; opacity: 0; transition: opacity .15s; }
.msg:hover .actions, .msg.last .actions { opacity: 1; }
.msg.user .actions { margin: 6px -6px 0 0; }
.actions .icon-btn { width: 32px; height: 32px; }
.actions svg { width: 17px; height: 17px; }
.msg-error { color: var(--danger); font-size: 14px; margin-top: 8px; }

/* Composer */
.composer-wrap { flex: none; padding: 0 24px 10px; position: relative; }
.composer {
  max-width: 768px; margin: 0 auto; background: var(--composer); border-radius: 28px;
  box-shadow: 0 0 0 1px var(--composer-line), 0 2px 12px rgba(0, 0, 0, .05);
  padding: 10px 12px 10px 14px; display: flex; flex-direction: column; gap: 4px;
}
.composer:focus-within { box-shadow: 0 0 0 1px var(--faint), 0 2px 12px rgba(0, 0, 0, .06); }
.composer textarea {
  width: 100%; border: 0; outline: 0; resize: none; background: transparent; color: var(--fg);
  font: inherit; line-height: 1.5; max-height: 200px; padding: 6px 6px 2px;
}
.composer textarea::placeholder { color: var(--faint); }
.composer-row { display: flex; justify-content: space-between; align-items: center; }
.send {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: var(--send); color: var(--send-fg);
}
.send:disabled { opacity: .3; cursor: default; }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.send.streaming .i-send { display: none; }
.send.streaming .i-stop { display: block; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:empty { display: none; }
.chips .file-card { position: relative; margin: 2px 0 4px; }
.chips .file-card.uploading { opacity: .6; }
.chip-x {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--fg); color: var(--bg); display: grid; place-items: center;
}
.chip-x svg { width: 12px; height: 12px; stroke-width: 3; }
.disclaimer { text-align: center; font-size: 12px; color: var(--faint); margin: 10px 0 0; }
.welcome { display: none; text-align: center; font-size: 28px; font-weight: 500; letter-spacing: -.01em; margin: 0 0 28px; }
.scroll-down {
  position: absolute; left: 50%; top: -52px; transform: translateX(-50%); width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; background: var(--bg); box-shadow: var(--shadow); color: var(--fg);
}
.scroll-down svg { width: 18px; height: 18px; }

/* Empty state: composer floats in the middle, like a fresh chat */
.app.empty .thread { flex: 1 1 0; }
.app.empty .composer-wrap { position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-60%); }
.app.empty .welcome { display: block; }
.app.empty .disclaimer { visibility: hidden; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100;
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow);
}
.scrim { display: none; }

@media (max-width: 768px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; width: 280px; transform: translateX(0); }
  .app.collapsed .sidebar { margin-left: 0; transform: translateX(-100%); }
  .app:not(.collapsed) .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .4); }
  #open-sidebar { display: grid !important; }
  .mobile-only { display: grid; }
  .messages { padding: 8px 16px 24px; }
  .composer-wrap { padding: 0 12px calc(8px + env(safe-area-inset-bottom)); }
  .msg.user .bubble, .msg .files { max-width: 88%; }
  .welcome { font-size: 24px; }
}

/* ---------- Ticket desk ---------- */
:root { --st-pending: #b7791f; --st-approved: #2f7de1; --st-progress: #7c3aed; --st-info: #e0532a; --st-done: #16a34a; --st-closed: #8f8f8f; --card: #fafafa; }
@media (prefers-color-scheme: dark) {
  :root { --st-pending: #f0b44c; --st-approved: #6aa8ff; --st-progress: #b18cff; --st-info: #ff8a5c; --st-done: #4ade80; --st-closed: #8a8a8a; --card: #262626; }
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 6px 2px; }
.filter { font-size: 12.5px; padding: 3px 10px; border-radius: 999px; color: var(--muted); border: 1px solid var(--line); }
.filter:hover { background: var(--hover); }
.filter.on { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.filter .n { opacity: .7; margin-left: 4px; font-variant-numeric: tabular-nums; }
.convo a { display: flex; align-items: center; gap: 8px; }
.convo .tid { color: var(--faint); font-variant-numeric: tabular-nums; font-size: 12.5px; flex: none; }
.convo .ttl { overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--st-closed); }
.s-pending_approval { --c: var(--st-pending); } .s-approved { --c: var(--st-approved); } .s-in_progress { --c: var(--st-progress); }
.s-needs_info { --c: var(--st-info); } .s-done { --c: var(--st-done); } .s-rejected, .s-cancelled { --c: var(--st-closed); }
.dot[class*="s-"] { background: var(--c); }
.s-needs_info.dot, .s-in_progress.dot { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 25%, transparent); }
.status-pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; padding: 3px 10px; border-radius: 999px;
  color: var(--c); background: color-mix(in srgb, var(--c) 14%, transparent); white-space: nowrap;
}
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.admin-link { width: 100%; color: var(--fg); margin-bottom: 2px; }
.admin-link svg { width: 18px; height: 18px; margin: 0 3px; }
.role-tag { flex: none !important; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--hover); color: var(--muted); }

.topbar-title { font-weight: 600; font-size: 16px; padding: 0 8px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.topbar-actions { display: flex; gap: 8px; align-items: center; flex: none; }
.pill-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 14px; border-radius: 999px;
  font-size: 14px; font-weight: 500; border: 1px solid var(--composer-line); color: var(--fg); background: var(--bg);
}
.pill-btn svg { width: 17px; height: 17px; }
.pill-btn:hover { background: var(--hover); }
.pill-btn.primary { background: var(--send); color: var(--send-fg); border-color: var(--send); }
.pill-btn.primary:hover { opacity: .88; }
.pill-btn.danger { color: var(--danger); }
.pill-btn:disabled { opacity: .5; cursor: default; }
.link-btn { color: var(--muted); text-decoration: underline; font-size: 12px; padding: 0; }
.link-btn:hover { color: var(--fg); }

.ticket-head { padding: 8px 0 20px; border-bottom: 1px solid var(--line); margin-bottom: 24px; }
.ticket-head h1 { font-size: 26px; line-height: 1.25; letter-spacing: -.015em; margin: 10px 0 8px; }
.ticket-head h1 .tid { color: var(--faint); font-weight: 500; }
.ticket-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; color: var(--muted); font-size: 13.5px; }
.ticket-meta img { width: 20px; height: 20px; border-radius: 50%; vertical-align: -5px; margin-right: 6px; }
.desc-card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 18px 20px; margin-bottom: 28px; }
.notice {
  display: flex; gap: 12px; align-items: flex-start; padding: 14px 16px; border-radius: 14px; margin-bottom: 24px; font-size: 14.5px;
  background: color-mix(in srgb, var(--c) 10%, transparent); border: 1px solid color-mix(in srgb, var(--c) 30%, transparent);
}
.notice svg { color: var(--c); margin-top: 2px; }
.notice b { display: block; margin-bottom: 2px; }
.notice .actions-row { display: flex; gap: 8px; margin-top: 10px; }

.event { display: flex; justify-content: center; margin: 0 0 20px; }
.event span { font-size: 12.5px; color: var(--faint); text-align: center; }
.event b { color: var(--muted); font-weight: 600; }
.agent-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13.5px; font-weight: 600; }
.agent-avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); position: relative; flex: none; }
.agent-avatar::after { content: ""; position: absolute; inset: 8px 6px 6px; border: 2px solid #fff; border-top: 0; border-radius: 0 0 8px 8px; }
.q-badge { font-size: 11.5px; font-weight: 600; padding: 1px 8px; border-radius: 999px; color: var(--st-info); background: color-mix(in srgb, var(--st-info) 14%, transparent); }
.msg.agent .content { padding-left: 34px; }
.msg.question .content { border-left: 3px solid var(--st-info); padding-left: 14px; margin-left: 20px; }
.msg.user .who { font-size: 12.5px; color: var(--faint); margin: 0 6px 4px 0; }
.time { font-size: 12px; color: var(--faint); font-weight: 400; }

.draft-bar { max-width: 768px; margin: 0 auto 10px; display: flex; justify-content: center; }
.welcome-sub { display: none; text-align: center; color: var(--muted); margin: -16px auto 24px; max-width: 520px; padding: 0 12px; }
.app.empty .welcome-sub { display: block; }
.app.empty .disclaimer { visibility: visible; }
.app.locked .composer, .app.locked .draft-bar { display: none; }

.modal { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, .45); display: grid; place-items: center; padding: 16px; }
.modal-card {
  width: min(720px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; gap: 14px;
  background: var(--bg); border-radius: 20px; padding: 20px 22px; box-shadow: var(--shadow);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; font-size: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; color: var(--muted); font-weight: 500; }
.field em { font-style: normal; color: var(--faint); font-weight: 400; }
.field.grow { flex: 1; min-height: 0; }
.field input, .field textarea, .admin-form input, .admin-form select {
  font: inherit; font-size: 15px; color: var(--fg); background: var(--composer); border: 1px solid var(--composer-line);
  border-radius: 12px; padding: 10px 12px; outline: none;
}
.field textarea { min-height: 300px; flex: 1; resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13.5px; line-height: 1.6; }
.field input:focus, .field textarea:focus, .admin-form input:focus { border-color: var(--faint); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; }

.admin h1 { font-size: 24px; margin: 8px 0 6px; }
.admin p { color: var(--muted); margin: 0 0 20px; }
.admin-form { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.admin-form input { flex: 1; min-width: 220px; }
.email-row { display: flex; align-items: center; gap: 12px; padding: 12px 4px; border-bottom: 1px solid var(--line); }
.email-row .e { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.empty-state { text-align: center; color: var(--faint); padding: 40px 0; }

@media (max-width: 768px) {
  .ticket-head h1 { font-size: 22px; }
  .topbar-actions .pill-btn span.long { display: none; }
  .msg.agent .content { padding-left: 0; }
}
