:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel2: #eef0f4; --line: #dfe3ea;
  --text: #1b1e24; --muted: #6b7280; --accent: #e8590c; --accent-2: #fff1e8;
  --user: #1f6feb; --user-text: #ffffff; --tool: #f3f4f6; --ok: #16a34a; --bad: #dc2626;
  --code: #f3f4f6; --shadow: 0 6px 24px rgba(0,0,0,.08);
  --font: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, Menlo, Consolas, "Noto Sans Thai", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317; --panel: #16181d; --panel2: #1d2027; --line: #2a2e37;
    --text: #e7e9ee; --muted: #9aa1ad; --accent: #ff7a2e; --accent-2: #2a1a10;
    --user: #2f6fd6; --tool: #1b1e24; --code: #1b1e24; --shadow: 0 6px 24px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  --bg: #111317; --panel: #16181d; --panel2: #1d2027; --line: #2a2e37;
  --text: #e7e9ee; --muted: #9aa1ad; --accent: #ff7a2e; --accent-2: #2a1a10;
  --user: #2f6fd6; --tool: #1b1e24; --code: #1b1e24; --shadow: 0 6px 24px rgba(0,0,0,.4);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.65; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--bad); min-height: 1.4em; margin: 0; }
.logo { font-weight: 600; font-size: 18px; }
.logo b { color: var(--accent); }
.logo.big { font-size: 34px; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; text-align: left; }
.btn.ghost:hover { background: var(--panel2); }
.btn.danger { border-color: var(--bad); color: var(--bad); background: transparent; }
.btn.small { padding: 2px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.icon-btn { border: 0; background: transparent; width: 38px; height: 38px; border-radius: 10px; font-size: 18px; flex: none; }
.icon-btn:hover { background: var(--panel2); }

/* login */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; display: grid; gap: 12px; box-shadow: var(--shadow); text-align: center; }
.login-card input { padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }

/* layout */
.app { display: flex; height: 100dvh; }
.drawer { width: 270px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; padding: 14px; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.section-label { font-size: 12px; color: var(--muted); margin-top: 6px; }
.tab-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.tab-list li { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px; cursor: pointer; }
.tab-list li:hover { background: var(--panel2); }
.tab-list li.on { background: var(--accent-2); }
.tab-list .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-list .x { opacity: .5; border: 0; background: none; padding: 0 4px; }
.tab-list .x:hover { opacity: 1; color: var(--bad); }
.drawer-foot { display: grid; gap: 4px; border-top: 1px solid var(--line); padding-top: 10px; }
.scrim { display: none; }
.only-mobile { display: none; }

.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; display: inline-block; }
.dot.idle { background: var(--ok); }
.dot.busy { background: var(--accent); animation: pulse 1s infinite; }
.dot.dead { background: var(--bad); }
@keyframes pulse { 50% { opacity: .3; } }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
.title-wrap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.tab-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.view-switch { display: flex; background: var(--panel2); border-radius: 10px; padding: 2px; }
.seg { border: 0; background: transparent; padding: 4px 12px; border-radius: 8px; font-size: 13px; }
.seg.on { background: var(--panel); box-shadow: 0 1px 3px rgba(0,0,0,.12); font-weight: 600; }

/* chat */
.chat-view { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }
.msgs { flex: 1; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 10px; }
.msgs > * { max-width: 860px; width: 100%; margin-left: auto; margin-right: auto; }
.empty { margin: auto; text-align: center; display: grid; gap: 10px; justify-items: center; padding: 16px; }
.bubble { border-radius: 14px; padding: 10px 14px; overflow-wrap: anywhere; }
.bubble.user { background: var(--user); color: var(--user-text); max-width: min(85%, 700px); margin-right: 0; white-space: pre-wrap; }
.bubble.user.pending { opacity: .6; }
.bubble.assistant { background: transparent; padding: 2px 2px; }
.bubble.assistant p { margin: .4em 0; }
.bubble.assistant pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; overflow-x: auto; font-family: var(--mono); font-size: 13px; line-height: 1.5; position: relative; }
.bubble.assistant code { font-family: var(--mono); font-size: 13px; background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble.assistant pre code { background: none; padding: 0; }
.bubble.assistant table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.bubble.assistant th, .bubble.assistant td { border: 1px solid var(--line); padding: 4px 10px; }
.bubble.assistant a { color: var(--accent); }
.bubble.assistant img { max-width: 100%; }
.copy { position: absolute; top: 6px; right: 6px; font-size: 11px; border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 1px 8px; opacity: .8; }
.command { align-self: flex-end; font-family: var(--mono); font-size: 13px; background: var(--accent-2); color: var(--accent); border-radius: 8px; padding: 2px 10px; width: auto; margin-right: 0; }
.note { font-family: var(--mono); font-size: 12px; color: var(--muted); white-space: pre-wrap; border-left: 3px solid var(--line); padding-left: 10px; }
.divider { text-align: center; color: var(--muted); font-size: 12px; }
.tool { background: var(--tool); border: 1px solid var(--line); border-radius: 10px; font-size: 13px; }
.tool summary { padding: 6px 10px; cursor: pointer; display: flex; gap: 8px; align-items: baseline; list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool .tname { font-weight: 600; flex: none; }
.tool .tsum { color: var(--muted); font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.tool .tstat { margin-left: auto; flex: none; }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--line); font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow-y: auto; line-height: 1.5; }
.tool pre.res.error { color: var(--bad); }
.busy { display: flex; align-items: center; gap: 10px; padding: 6px 16px; font-size: 14px; color: var(--muted); max-width: 860px; width: 100%; margin: 0 auto; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.peek { margin: 0 12px 8px; max-height: 40vh; overflow: auto; background: #0d0f12; color: #d6d9df; border-radius: 10px; padding: 10px; font-family: var(--mono); font-size: 11.5px; line-height: 1.45; white-space: pre; }

/* terminal */
.term-view { flex: 1; min-height: 0; background: #0d0f12; padding: 6px; }
#term { width: 100%; height: 100%; }

/* composer */
.composer-wrap { border-top: 1px solid var(--line); background: var(--panel); padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); }
.keys { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.keys button { flex: none; border: 1px solid var(--line); background: var(--panel2); border-radius: 8px; padding: 2px 10px; font-size: 12.5px; font-family: var(--mono); }
.attachments { display: flex; gap: 6px; flex-wrap: wrap; }
.attachments .chip { font-size: 12px; background: var(--panel2); border-radius: 8px; padding: 2px 8px; display: flex; gap: 6px; align-items: center; max-width: 100%; overflow: hidden; }
.attachments .chip button { border: 0; background: none; padding: 0; }
.composer { display: flex; align-items: flex-end; gap: 6px; max-width: 900px; margin: 0 auto; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); background: var(--bg); color: var(--text); border-radius: 12px; padding: 9px 12px; font: inherit; max-height: 40vh; min-height: 42px; }
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.send { width: 42px; height: 42px; border: 0; border-radius: 12px; background: var(--accent); color: #fff; font-size: 18px; flex: none; }
.send:disabled { opacity: .4; }

/* modal & toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; grid-template-columns: minmax(0, 640px); justify-content: center; padding: 16px; z-index: 50; }
.modal-card { min-width: 0; background: var(--panel); border-radius: 16px; width: 100%; max-width: 640px; max-height: 85dvh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.modal-body { overflow-y: auto; padding: 10px 16px 16px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 8px 6px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; cursor: pointer; }
.list li:hover { background: var(--panel2); }
.list .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .meta { color: var(--muted); font-size: 12px; flex: none; }
.crumb { font-family: var(--mono); font-size: 12px; color: var(--muted); overflow-wrap: anywhere; margin-bottom: 6px; }
.form-row { display: grid; gap: 6px; margin: 10px 0; }
.form-row input, .form-row select { padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); font: inherit; }
.toast { position: fixed; bottom: 110px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 10px; z-index: 60; font-size: 14px; max-width: calc(100% - 32px); }

@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .drawer { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; transform: translateX(-105%); transition: transform .2s; box-shadow: var(--shadow); }
  .drawer.open { transform: none; }
  .scrim.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .msgs { padding: 12px; }
  .seg { padding: 4px 9px; }
  html, body { font-size: 15.5px; }
}
