:root {
  --bg: #f2f2f7;
  --surface: rgba(255,255,255,0.82);
  --surface-solid: #ffffff;
  --card: #ffffff;
  --text: #1c1c1e;
  --text-2: #6e6e73;
  --text-3: #aeaeb2;
  --sep: rgba(60,60,67,0.12);
  --blue: #0a84ff;
  --blue-2: #2f95ff;
  --green: #34c759;
  --red: #ff3b30;
  --orange: #ff9f0a;
  --bubble-me: linear-gradient(180deg, #2f95ff, #0a7aff);
  --bubble-them: #e9e9eb;
  --bubble-bot: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", Inter, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: rgba(28,28,30,0.78);
    --surface-solid: #1c1c1e;
    --card: #1c1c1e;
    --text: #f5f5f7;
    --text-2: #98989d;
    --text-3: #636366;
    --sep: rgba(84,84,88,0.5);
    --bubble-them: #2c2c2e;
    --bubble-bot: #1c1c1e;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--font); background: var(--bg); color: var(--text);
  font-size: 15px; line-height: 1.4; -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
[hidden] { display: none !important; }
a { color: var(--blue); text-decoration: none; }

/* ---------- login ---------- */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 20% -10%, rgba(10,132,255,.14), transparent 60%),
              radial-gradient(900px 500px at 110% 110%, rgba(52,199,89,.12), transparent 60%), var(--bg);
}
.login-card {
  width: 100%; max-width: 360px; background: var(--card); border-radius: 24px; padding: 36px 28px 28px;
  box-shadow: var(--shadow); text-align: center;
}
.logo {
  width: 64px; height: 64px; border-radius: 18px; margin: 0 auto 16px; display: grid; place-items: center;
  background: var(--bubble-me); color: #fff; font-size: 30px; font-weight: 700; letter-spacing: -1px;
  box-shadow: 0 8px 20px rgba(10,132,255,.35);
}
.login h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -0.4px; font-weight: 700; }
.login p { margin: 0 0 24px; color: var(--text-2); font-size: 14px; }
.field {
  display: block; width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--sep); font-size: 16px;
  background: var(--bg); outline: none; margin-bottom: 10px; transition: border-color .15s, box-shadow .15s;
}
.field:focus { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(10,132,255,.15); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 18px; border-radius: 12px; font-weight: 600; transition: transform .1s, opacity .15s, background .15s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--blue); color: #fff; width: 100%; }
.btn-primary:hover { background: var(--blue-2); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-ghost { color: var(--blue); padding: 8px 12px; border-radius: 10px; }
.btn-ghost:hover { background: rgba(10,132,255,.08); }
.btn-danger { color: var(--red); }
.btn-danger:hover { background: rgba(255,59,48,.08); }
.error { color: var(--red); font-size: 13px; min-height: 18px; margin: 4px 0 10px; }

/* ---------- app shell ---------- */
.app { height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; }
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--sep);
}
.topbar .title { font-weight: 700; font-size: 17px; letter-spacing: -0.3px; padding-left: 4px; }
.topbar .spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 999px;
  background: rgba(120,120,128,.12); font-size: 13px; color: var(--text); font-weight: 500; white-space: nowrap;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px rgba(52,199,89,.2); }
.pill:hover { background: rgba(120,120,128,.2); }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; color: var(--text-2); }
.icon-btn:hover { background: rgba(120,120,128,.14); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

.popover {
  pointer-events: auto; position: absolute; right: 14px; top: calc(100% + 6px); background: var(--surface-solid); border-radius: 14px;
  box-shadow: var(--shadow); border: 1px solid var(--sep); padding: 8px; min-width: 200px; z-index: 20;
}
.popover .row { display: flex; align-items: center; gap: 10px; padding: 10px 10px; border-radius: 10px; font-size: 14px; width: 100%; text-align: left; }
.popover .row.link { color: var(--text-2); border-top: 1px solid var(--sep); border-radius: 0; margin-top: 4px; padding-top: 12px; font-size: 13px; }
.popover .row.link:hover { color: var(--red); }
.popover .row .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.popover .row .tag { margin-left: auto; font-size: 11px; color: var(--blue); font-weight: 600; }
.popover .empty { padding: 10px; color: var(--text-2); font-size: 13px; text-align: center; }

/* ---------- messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 16px 14px 8px; scroll-behavior: smooth; }
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 2px 0; max-width: 78%; animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }
.msg.me { align-self: flex-end; align-items: flex-end; margin-left: auto; }
.msg .name { font-size: 11.5px; color: var(--text-2); margin: 8px 0 3px 12px; font-weight: 500; }
.msg.me .name { display: none; }
.msg.cont .name { display: none; }
.bubble {
  padding: 9px 13px; border-radius: var(--radius); background: var(--bubble-them); color: var(--text);
  white-space: pre-wrap; word-wrap: break-word; position: relative; line-height: 1.35;
}
.msg.me .bubble { background: var(--bubble-me); color: #fff; }
.msg.query .bubble { background: rgba(10,132,255,.12); color: var(--text); border: 1px solid rgba(10,132,255,.18); }
.msg.me.query .bubble { background: var(--bubble-me); color: #fff; border: 0; }
.bubble .q-mark { font-weight: 700; opacity: .7; margin-right: 4px; }
.msg .time { font-size: 10.5px; color: var(--text-3); margin: 3px 12px 0; }
.msg.me .time { text-align: right; }

.msg.bot { max-width: 86%; }
.msg.bot .bubble {
  background: var(--bubble-bot); border: 1px solid var(--sep); box-shadow: 0 2px 10px rgba(0,0,0,.04);
  padding: 10px 14px 9px; white-space: normal;
}
.msg.bot .name { color: var(--blue); }
.bot-q { font-size: 13px; color: var(--text-2); margin-bottom: 10px; }
.bot-q b { color: var(--text); font-weight: 600; }
.bot-q .nb { color: var(--text-3); }
.bot-list { display: flex; flex-direction: column; gap: 6px; }
.bot-list.alt { margin-top: 6px; color: var(--text-2); }
.bot-line { display: flex; gap: 8px; align-items: baseline; line-height: 1.35; }
.bot-line .bul { color: var(--text-3); flex: 0 0 auto; }
.bot-line .en { font-weight: 600; letter-spacing: -0.1px; }
.bot-line .dash { color: var(--text-3); }
.bot-none { color: var(--text-2); }
.msg.system { align-self: center; max-width: 90%; align-items: center; margin: 10px 0; }
.msg.system .bubble { background: transparent; color: var(--text-2); font-size: 12px; padding: 2px 8px; text-align: center; }

.typing { display: inline-flex; gap: 4px; align-items: center; padding: 12px 14px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

/* ---------- composer ---------- */
.composer {
  position: sticky; bottom: 0; padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--sep);
}
.composer form { display: flex; align-items: flex-end; gap: 8px; }
.mode {
  flex: 0 0 auto; height: 42px; min-width: 42px; padding: 0 10px; border-radius: 13px; font-weight: 700; font-size: 18px;
  background: rgba(120,120,128,.14); color: var(--text-2); transition: all .15s;
}
.mode.on { background: var(--blue); color: #fff; box-shadow: 0 4px 12px rgba(10,132,255,.35); }
.input-wrap { flex: 1; display: flex; align-items: center; background: var(--bg); border: 1px solid var(--sep); border-radius: 21px; padding: 0 5px 0 14px; min-height: 42px; }
.input-wrap:focus-within { border-color: var(--blue); }
.input-wrap textarea {
  flex: 1; border: 0; background: transparent; outline: none; resize: none; padding: 10px 0; max-height: 120px; line-height: 1.3; font-size: 16px;
}
.send {
  width: 34px; height: 34px; border-radius: 50%; background: var(--blue); color: #fff; display: grid; place-items: center;
  transition: transform .1s, opacity .15s; flex: 0 0 auto;
}
.send:disabled { opacity: .35; }
.send svg { width: 18px; height: 18px; }

/* ---------- admin sheet ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; animation: fade .2s; }
@keyframes fade { from { opacity: 0 } }
.sheet {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100%); background: var(--bg); z-index: 50;
  box-shadow: -10px 0 40px rgba(0,0,0,.2); display: flex; flex-direction: column; animation: slide .25s cubic-bezier(.2,.8,.2,1);
}
@keyframes slide { from { transform: translateX(40px); opacity: 0 } }
.sheet header { display: flex; align-items: center; gap: 10px; padding: 14px 16px; padding-top: calc(14px + env(safe-area-inset-top)); border-bottom: 1px solid var(--sep); background: var(--surface-solid); }
.sheet header h2 { margin: 0; font-size: 17px; font-weight: 700; flex: 1; }
.sheet .body { flex: 1; overflow-y: auto; padding: 16px; }
.card { background: var(--card); border-radius: 16px; padding: 16px; margin-bottom: 14px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.card h3 { margin: 0 0 10px; font-size: 15px; font-weight: 700; }
.card .desc { color: var(--text-2); font-size: 13px; margin: -4px 0 12px; }
.card textarea.field { min-height: 160px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.row-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.note { font-size: 13px; color: var(--text-2); margin-top: 8px; }
.note.ok { color: var(--green); }
.note.err { color: var(--red); }
.test { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--sep); }
.test:first-of-type { border-top: 0; }
.test .t-title { flex: 1; min-width: 0; }
.test .t-title .n { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.test .t-title .m { font-size: 12px; color: var(--text-2); }
.switch { position: relative; width: 44px; height: 26px; border-radius: 13px; background: rgba(120,120,128,.3); transition: background .2s; flex: 0 0 auto; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.25); transition: transform .2s; }
.switch.on { background: var(--green); }
.switch.on::after { transform: translateX(18px); }
.try-out { margin-top: 10px; font-size: 13px; }
.try-out .ans { background: var(--bg); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; white-space: pre-wrap; }
.try-out table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.try-out td { padding: 4px 6px; border-top: 1px solid var(--sep); vertical-align: top; }
.try-out td.s { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); white-space: nowrap; }
.try-out td.k { color: var(--text-3); font-size: 11.5px; }
.words-list { max-height: 320px; overflow: auto; font-size: 13px; margin-top: 10px; border-top: 1px solid var(--sep); }
.words-list div { padding: 5px 0; border-bottom: 1px solid var(--sep); display: grid; grid-template-columns: 28px 1fr; gap: 6px; }
.words-list .num { color: var(--text-3); }
.words-list .tr { color: var(--text-2); font-size: 12px; }
.bar { height: 6px; border-radius: 3px; background: rgba(120,120,128,.18); margin-top: 8px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--blue); border-radius: 3px; transition: width .4s; }
.btn-ghost.attn { color: var(--orange); }
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg) } }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 13px; z-index: 60; box-shadow: var(--shadow); animation: pop .2s; }

@media (max-width: 600px) {
  .msg { max-width: 86%; }
  .msg.bot { max-width: 94%; }
  .messages { padding: 12px 10px 6px; }
  .topbar { padding-left: 12px; padding-right: 10px; }
}
