:root {
  --bg: #ffffff;
  --bg-side: #f9f9f9;
  --bg-soft: #f4f4f4;
  --bg-hover: #ececec;
  --bg-bubble: #f1f1f1;
  --border: #e3e3e3;
  --text: #0d0d0d;
  --text-2: #5d5d5d;
  --text-3: #8f8f8f;
  --accent: #10a37f;
  --accent-text: #ffffff;
  --danger: #d93025;
  --warn: #b26a00;
  --code-bg: #f6f8fa;
  --shadow: 0 4px 24px rgba(0, 0, 0, .12);
  --radius: 14px;
  --side-w: 268px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --bg-side: #171717;
    --bg-soft: #2f2f2f;
    --bg-hover: #2a2a2a;
    --bg-bubble: #303030;
    --border: #3a3a3a;
    --text: #ececec;
    --text-2: #b4b4b4;
    --text-3: #8a8a8a;
    --accent: #19c37d;
    --accent-text: #0b1f17;
    --danger: #ff6b5e;
    --warn: #ffb74d;
    --code-bg: #0d1117;
    --shadow: 0 4px 24px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.hidden { display: none !important; }

/* ---------- buttons, inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--bg); border-radius: 10px;
  padding: 8px 14px; min-height: 38px; white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.primary:hover { opacity: .88; }
.btn.accent { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border: 0; background: transparent; border-radius: 8px; color: var(--text-2);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn svg, .btn svg { width: 18px; height: 18px; }
input[type=text], input[type=password], textarea, select {
  width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: 10px;
  padding: 10px 12px; font-size: 16px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--text-3); }
label { display: block; font-size: 13px; color: var(--text-2); margin: 14px 0 6px; }
.field-hint { font-size: 12px; color: var(--text-3); margin-top: 4px; }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 380px; }
.auth-logo { display: flex; align-items: center; gap: 10px; justify-content: center; margin-bottom: 8px; font-size: 22px; font-weight: 650; }
.auth-sub { text-align: center; color: var(--text-2); margin: 0 0 22px; font-size: 14px; }
.auth-card .btn { width: 100%; margin-top: 20px; min-height: 44px; }
.auth-switch { text-align: center; margin-top: 18px; font-size: 14px; color: var(--text-2); }
.auth-switch a { cursor: pointer; }
.form-msg { margin-top: 12px; font-size: 14px; min-height: 1em; }
.form-msg.err { color: var(--danger); }
.form-msg.ok { color: var(--accent); }
.logo-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; flex-shrink: 0; }
.logo-mark svg { width: 18px; height: 18px; stroke: #fff; }

/* ---------- layout ---------- */
.layout { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: var(--side-w); flex-shrink: 0; background: var(--bg-side); display: flex; flex-direction: column;
  border-right: 1px solid var(--border); transition: transform .22s ease, margin .22s ease;
  padding-top: env(safe-area-inset-top);
}
.layout.side-closed .sidebar { margin-left: calc(-1 * var(--side-w)); }
.side-top { display: flex; align-items: center; gap: 4px; padding: 10px 10px 6px; }
.side-top .brand { flex: 1; display: flex; align-items: center; gap: 8px; font-weight: 650; padding-left: 4px; }
.new-chat {
  margin: 4px 10px 8px; display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px;
  border: 0; background: transparent; text-align: left; font-weight: 500;
}
.new-chat:hover { background: var(--bg-hover); }
.new-chat svg { width: 18px; height: 18px; }
.side-nav { padding: 0 10px; }
.side-link { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 10px; color: var(--text); text-decoration: none; font-size: 14px; }
.side-link:hover, .side-link.active { background: var(--bg-hover); }
.side-link svg { width: 17px; height: 17px; color: var(--text-2); }
.badge { margin-left: auto; background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; line-height: 18px; }
.conv-list { flex: 1; overflow-y: auto; padding: 6px 10px 10px; }
.conv-group { font-size: 12px; color: var(--text-3); padding: 14px 12px 4px; font-weight: 600; }
.conv-item {
  position: relative; display: flex; align-items: center; border-radius: 10px; padding: 8px 12px; font-size: 14px;
  cursor: pointer; color: var(--text);
}
.conv-item:hover, .conv-item.active { background: var(--bg-hover); }
.conv-item .t { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv-item .more { opacity: 0; width: 28px; height: 28px; margin: -4px -6px -4px 4px; }
.conv-item:hover .more, .conv-item.active .more { opacity: 1; }
@media (hover: none) { .conv-item .more { opacity: 1; } }
.conv-item input { padding: 2px 6px; font-size: 14px; border-radius: 6px; }
.side-bottom { border-top: 1px solid var(--border); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); position: relative; }
.user-btn { width: 100%; display: flex; align-items: center; gap: 10px; padding: 8px; border: 0; background: transparent; border-radius: 10px; text-align: left; }
.user-btn:hover { background: var(--bg-hover); }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 14px; font-weight: 600; flex-shrink: 0; }
.user-btn .who { flex: 1; min-width: 0; }
.user-btn .who b { display: block; font-weight: 550; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-btn .who small { color: var(--text-3); font-size: 12px; }

.menu {
  position: absolute; z-index: 50; min-width: 180px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow); padding: 6px;
}
.menu button, .menu a {
  display: flex; width: 100%; align-items: center; gap: 10px; border: 0; background: transparent; text-align: left;
  padding: 8px 10px; border-radius: 8px; font-size: 14px; color: var(--text); text-decoration: none;
}
.menu button:hover, .menu a:hover { background: var(--bg-hover); }
.menu .danger { color: var(--danger); }
.menu svg { width: 16px; height: 16px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
  min-height: 52px; flex-shrink: 0;
}
.topbar .title { flex: 1; min-width: 0; font-weight: 600; font-size: 16px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); display: inline-block; margin-right: 6px; }
.status-dot.on { background: var(--accent); }
.status-dot.off { background: var(--danger); }
.model-pill { display: inline-flex; align-items: center; font-size: 13px; color: var(--text-2); padding: 4px 10px; border-radius: 999px; background: var(--bg-soft); white-space: nowrap; }
.effort-select { width: auto; padding: 6px 28px 6px 10px; font-size: 14px; border-radius: 999px; background-color: var(--bg-soft); border-color: transparent; }
.backdrop { display: none; }

/* ---------- chat ---------- */
.chat-scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.thread { max-width: 780px; margin: 0 auto; padding: 8px 20px 24px; }
.msg { margin: 18px 0; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--bg-bubble); padding: 10px 16px; border-radius: 20px; max-width: 80%;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.msg-actions { display: flex; gap: 2px; margin-top: 4px; color: var(--text-3); align-items: center; }
.msg-actions .icon-btn { width: 30px; height: 30px; }
.msg-actions .icon-btn svg { width: 16px; height: 16px; }
.msg-actions .stat { font-size: 12px; margin-left: 6px; }
.msg.user .msg-actions { justify-content: flex-end; opacity: 0; }
.msg.user:hover .msg-actions { opacity: 1; }
.msg.user { flex-direction: column; align-items: flex-end; }

.think { margin: 0 0 10px; border-left: 2px solid var(--border); padding-left: 12px; }
.think summary { cursor: pointer; color: var(--text-2); font-size: 14px; list-style: none; display: inline-flex; align-items: center; gap: 6px; user-select: none; }
.think summary::-webkit-details-marker { display: none; }
.think summary::after { content: "›"; transition: transform .15s; display: inline-block; }
.think[open] summary::after { transform: rotate(90deg); }
.think .think-body { color: var(--text-2); font-size: 14px; white-space: pre-wrap; margin-top: 6px; max-height: 360px; overflow-y: auto; }
.thinking-label { background: linear-gradient(90deg, var(--text-3), var(--text), var(--text-3)); background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 1.6s linear infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }
.cursor::after { content: "●"; font-size: 12px; margin-left: 2px; animation: blink 1s infinite; color: var(--text-2); }
@keyframes blink { 50% { opacity: .2; } }
.msg-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); border-radius: 10px; padding: 8px 12px; font-size: 14px; margin-top: 6px; }
.stopped-note { color: var(--text-3); font-size: 13px; margin-top: 4px; }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: .6em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.1em 0 .5em; line-height: 1.35; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.28em; } .md h3 { font-size: 1.12em; }
.md ul, .md ol { padding-left: 1.5em; margin: .5em 0; }
.md li { margin: .2em 0; }
.md blockquote { margin: .6em 0; padding: 2px 14px; border-left: 3px solid var(--border); color: var(--text-2); }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .88em; background: var(--bg-soft); padding: .12em .38em; border-radius: 6px; }
.md pre { margin: .8em 0; border-radius: 12px; overflow: hidden; background: var(--code-bg); border: 1px solid var(--border); }
.md pre code { display: block; padding: 14px 16px; overflow-x: auto; background: transparent; font-size: 13.5px; line-height: 1.55; }
.code-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px 6px 16px; font-size: 12px; color: var(--text-2); background: var(--bg-soft); }
.code-head button { border: 0; background: transparent; color: var(--text-2); font-size: 12px; display: inline-flex; gap: 4px; align-items: center; padding: 2px 6px; border-radius: 6px; }
.code-head button:hover { background: var(--bg-hover); }
.md table { border-collapse: collapse; margin: .8em 0; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 12px; text-align: left; }
.md th { background: var(--bg-soft); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.md img { max-width: 100%; }

/* empty state */
.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; text-align: center; }
.empty h1 { font-size: 26px; font-weight: 600; margin: 0 0 24px; }
.suggests { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; max-width: 640px; width: 100%; }
.suggest { border: 1px solid var(--border); background: var(--bg); border-radius: 14px; padding: 12px 14px; text-align: left; font-size: 14px; color: var(--text-2); }
.suggest b { display: block; color: var(--text); font-weight: 550; margin-bottom: 2px; }
.suggest:hover { background: var(--bg-hover); }

/* composer */
.composer-wrap { padding: 0 16px calc(12px + env(safe-area-inset-bottom)); flex-shrink: 0; }
.composer {
  max-width: 780px; margin: 0 auto; background: var(--bg-soft); border-radius: 26px; padding: 8px;
  display: flex; flex-direction: column; gap: 6px; border: 1px solid transparent;
}
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.attach-btn { width: 38px; height: 38px; border-radius: 50%; }
.attach-strip { display: flex; gap: 8px; flex-wrap: wrap; padding: 4px 4px 0; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 12px; overflow: hidden; border: 1px solid var(--border); background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; display: block; }
.thumb-x { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: rgba(0, 0, 0, .65); color: #fff; display: grid; place-items: center; padding: 0; }
.thumb-x svg { width: 12px; height: 12px; }
.msg-images { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; max-width: 80%; }
.msg-images img { max-width: 220px; max-height: 220px; border-radius: 14px; border: 1px solid var(--border); cursor: zoom-in; object-fit: cover; }
.lightbox { position: fixed; inset: 0; z-index: 150; background: rgba(0, 0, 0, .85); display: grid; place-items: center; padding: 16px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.main.dragging::after { content: "放開以附加圖片"; position: absolute; inset: 8px; border: 2px dashed var(--accent); border-radius: 18px; background: color-mix(in srgb, var(--accent) 8%, transparent); display: grid; place-items: center; font-size: 18px; color: var(--accent); z-index: 60; pointer-events: none; }
.composer:focus-within { border-color: var(--border); }
.composer textarea {
  flex: 1; align-self: center; border: 0; background: transparent; resize: none; padding: 8px 0; max-height: 220px; min-height: 24px;
  line-height: 1.5; font-size: 16px;
}
.send-btn { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); display: grid; place-items: center; flex-shrink: 0; }
.send-btn:disabled { opacity: .3; cursor: default; }
.send-btn svg { width: 18px; height: 18px; }
.composer-note { text-align: center; color: var(--text-3); font-size: 12px; padding-top: 8px; }
.scroll-down { position: absolute; left: 50%; transform: translateX(-50%); bottom: 110px; width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--border); background: var(--bg); box-shadow: var(--shadow); display: grid; place-items: center; z-index: 5; }
.scroll-down svg { width: 18px; height: 18px; }

/* ---------- pages ---------- */
.page { flex: 1; overflow-y: auto; }
.page-inner { max-width: 880px; margin: 0 auto; padding: 8px 20px calc(40px + env(safe-area-inset-bottom)); }
.page h1 { font-size: 24px; margin: 8px 0 4px; }
.page .lead { color: var(--text-2); margin: 0 0 20px; }
.card { border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; background: var(--bg); }
.card h2 { font-size: 17px; margin: 0 0 4px; }
.card .sub { color: var(--text-2); font-size: 14px; margin: 0 0 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 8px 14px; font-size: 14px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13.5px; }
.keybox { display: flex; gap: 8px; align-items: center; background: var(--bg-soft); border-radius: 10px; padding: 8px 8px 8px 14px; margin: 8px 0 12px; }
.keybox code { flex: 1; min-width: 0; overflow-wrap: anywhere; font-family: ui-monospace, Menlo, monospace; font-size: 13.5px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; overflow-x: auto; }
.tab { border: 0; background: transparent; padding: 8px 12px; border-bottom: 2px solid transparent; color: var(--text-2); white-space: nowrap; font-size: 14px; }
.tab.active { color: var(--text); border-bottom-color: var(--text); font-weight: 550; }
.code-card pre { margin: 0; border-radius: 12px; background: var(--code-bg); border: 1px solid var(--border); overflow-x: auto; }
.code-card pre code { display: block; padding: 14px 16px; font-size: 13px; line-height: 1.55; font-family: ui-monospace, Menlo, monospace; }
.params { width: 100%; border-collapse: collapse; font-size: 14px; }
.params th, .params td { text-align: left; border-bottom: 1px solid var(--border); padding: 8px 6px; vertical-align: top; }
.params th { color: var(--text-2); font-weight: 550; }
.params td:first-child { white-space: nowrap; }
.test-out { background: var(--code-bg); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; min-height: 80px; white-space: pre-wrap; font-size: 14px; margin-top: 12px; overflow-wrap: anywhere; max-height: 420px; overflow-y: auto; }
.test-out.raw { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text-2); margin: 0; }
.dl-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.dl-item { display: flex; align-items: center; gap: 10px; border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; text-decoration: none; color: var(--text); font-size: 14px; }
.dl-item:hover { background: var(--bg-hover); }
.dl-item svg { width: 18px; height: 18px; color: var(--text-2); flex-shrink: 0; }
.dl-item small { display: block; color: var(--text-3); font-size: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }
.form-actions { margin-top: 16px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* admin */
.utable { width: 100%; border-collapse: collapse; font-size: 14px; }
.utable th, .utable td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.utable th { color: var(--text-2); font-weight: 550; font-size: 13px; }
.utable td.actions { text-align: right; white-space: nowrap; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--bg-soft); color: var(--text-2); white-space: nowrap; }
.tag.pending { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.tag.active { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.tag.disabled { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.tag.admin { background: var(--text); color: var(--bg); }
.pending-item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.pending-item:last-child { border-bottom: 0; }
.pending-item .who { flex: 1; min-width: 160px; }
.pending-item .who small { color: var(--text-3); display: block; font-size: 12px; }
.muted { color: var(--text-3); font-size: 14px; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--bg); border-radius: 18px; width: 100%; max-width: 460px; max-height: calc(100dvh - 32px); overflow-y: auto; padding: 20px 22px; box-shadow: var(--shadow); }
.modal h3 { margin: 0 0 4px; font-size: 18px; }
.modal .form-actions { justify-content: flex-end; }

#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 14px; opacity: 0; pointer-events: none; transition: all .2s; z-index: 200; max-width: calc(100vw - 32px); text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .sidebar { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; transform: translateX(-100%); margin-left: 0 !important; box-shadow: var(--shadow); width: min(84vw, 300px); }
  .layout.side-open .sidebar { transform: translateX(0); }
  .layout.side-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 30; }
  .thread { padding: 4px 14px 20px; }
  .msg.user .bubble { max-width: 88%; }
  .msg-images { max-width: 88%; }
  .msg-images img { max-width: 160px; max-height: 160px; }
  .composer-wrap { padding: 0 10px calc(8px + env(safe-area-inset-bottom)); }
  .composer-note { display: none; }
  .suggests { grid-template-columns: 1fr; }
  .suggest:nth-child(n+3) { display: none; }
  .empty h1 { font-size: 22px; }
  .page-inner { padding: 4px 14px calc(32px + env(safe-area-inset-bottom)); }
  .card { padding: 16px; }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dd { margin-bottom: 8px; }
  .form-grid { grid-template-columns: 1fr; }
  .model-pill { display: none; }
  .utable thead { display: none; }
  .utable, .utable tbody, .utable tr, .utable td { display: block; width: 100%; }
  .utable tr { border-bottom: 1px solid var(--border); padding: 10px 0; }
  .utable td { border: 0; padding: 2px 0; }
  .utable td.actions { text-align: left; padding-top: 8px; }
  .msg-actions .icon-btn { width: 34px; height: 34px; }
  .msg.user .msg-actions { opacity: 1; }
}
@media (min-width: 821px) {
  .only-mobile { display: none !important; }
}
