:root {
  --brand: #0b5a7a;
  --brand-dark: #08445d;
  --brand-soft: #e6f0f4;
  --accent: #f2b84b;
  --ink: #16212b;
  --muted: #5b6875;
  --line: #dde3e8;
  --bg: #f6f8f9;
  --card: #ffffff;
  --danger: #b42318;
  --ok: #157347;
  --r: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; font-family: var(--font); font-size: 16px; line-height: 1.55; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
h1, h2, h3, h4, h5 { line-height: 1.25; margin: 0 0 12px; }
p { margin: 0 0 12px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.i { flex: none; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 16px; border-radius: 8px; border: 1px solid transparent; font-weight: 600; cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn-secondary { background: var(--brand-soft); color: var(--brand-dark); }
.btn-secondary:hover:not(:disabled) { background: #d6e6ed; }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { border-color: var(--brand); color: var(--brand); }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; color: var(--brand); cursor: pointer; font-size: 14px; }
.link:hover { text-decoration: underline; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 8px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--brand-soft); color: var(--brand); }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--brand-dark); }

/* Auth */
.auth { min-height: 100%; display: grid; grid-template-columns: 1.1fr 1fr; }
.auth-intro { background: var(--brand-dark); color: #e9f1f5; padding: 64px; display: flex; flex-direction: column; justify-content: center; gap: 24px; }
.auth-intro .brand { color: #fff; }
.auth-intro h1 { font-size: 40px; color: #fff; max-width: 520px; margin: 0; }
.auth-intro .lead { font-size: 18px; max-width: 520px; color: #c9dce5; margin: 0; }
.feature-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 16px; max-width: 520px; }
.feature-list li { display: flex; gap: 16px; align-items: flex-start; }
.feature-list .ico { width: 40px; height: 40px; border-radius: 8px; background: rgba(255,255,255,.08); display: grid; place-items: center; color: var(--accent); flex: none; }
.feature-list p { margin: 2px 0 0; color: #b5cbd6; font-size: 15px; }
.feature-list strong { color: #fff; }
.auth-intro .btn-ghost { align-self: flex-start; color: #fff; border-color: rgba(255,255,255,.3); }
.auth-intro .btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.auth-card { align-self: center; justify-self: center; width: 100%; max-width: 420px; padding: 48px 24px; }
.tabs { display: flex; border-bottom: 1px solid var(--line); margin-bottom: 24px; }
.tabs button { flex: 1; background: none; border: 0; padding: 12px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.active { color: var(--brand); border-color: var(--brand); }
.form { display: grid; gap: 16px; }
.form label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; }
.form small { font-weight: 400; color: var(--muted); }
.form input:not([type=checkbox]), .form select, .role-pick select { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-weight: 400; }
.form input:focus, .form select:focus, textarea:focus, .role-pick select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.form .check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.form-links { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.panel-title { font-size: 20px; margin: 0; }
.fine { font-size: 13px; color: var(--muted); margin: 0; }
.form-msg { min-height: 24px; margin: 16px 0 0; font-size: 14px; color: var(--danger); }
.form-msg.ok { color: var(--ok); }

/* App shell */
.shell { display: grid; grid-template-columns: 280px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: #fff; border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 16px; padding: 16px; min-height: 0; }
.side-label { font-size: 13px; font-weight: 600; color: var(--muted); margin: 8px 0 0; }
.chat-list { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 4px; margin: 0 -8px; padding: 0 8px; }
.chat-list .empty { font-size: 14px; color: var(--muted); }
.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--brand-soft); }
.chat-open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 8px 10px; cursor: pointer; display: grid; }
.chat-open span { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-open small { font-size: 12px; color: var(--muted); }
.chat-del { opacity: 0; }
.chat-item:hover .chat-del, .chat-item.active .chat-del, .chat-del:focus-visible { opacity: 1; }
.side-foot { border-top: 1px solid var(--line); padding-top: 12px; display: grid; gap: 4px; }
.side-link { display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 8px; border-radius: 8px; cursor: pointer; color: var(--muted); font-size: 14px; text-align: left; }
.side-link:hover { background: var(--brand-soft); color: var(--brand); }
.me { display: flex; align-items: center; gap: 10px; padding: 8px; min-width: 0; }
.me div:last-child { display: grid; min-width: 0; }
.me strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 14px; flex: none; }

.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 24px; border-bottom: 1px solid var(--line); background: #fff; }
.menu-btn { display: none; }
.role-pick { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.role-pick select { width: auto; padding: 6px 10px; color: var(--ink); }
.badge { margin-left: auto; background: #fdf1d8; color: #7a5200; border: 1px solid #f2d394; font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 999px; }
.notice { margin: 16px 24px 0; padding: 12px 16px; border-radius: 8px; background: #fdf1d8; color: #6b4800; font-size: 14px; border: 1px solid #f2d394; }

.thread { flex: 1; overflow-y: auto; padding: 32px 24px; }
.welcome, .messages { max-width: 820px; margin: 0 auto; }
.welcome h1 { font-size: 28px; }
.quick-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-top: 24px; }
.quick { display: flex; gap: 12px; align-items: flex-start; text-align: left; padding: 16px; background: #fff; border: 1px solid var(--line); border-radius: var(--r); cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.quick:hover { border-color: var(--brand); box-shadow: 0 2px 8px rgba(11,90,122,.08); }
.quick-ico { width: 36px; height: 36px; border-radius: 8px; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; flex: none; }
.quick strong { display: block; font-size: 15px; }
.quick small { color: var(--muted); font-size: 14px; }

.messages { display: grid; gap: 24px; }
.msg { display: flex; gap: 12px; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--brand); color: #fff; border-radius: 14px 14px 4px 14px; padding: 12px 16px; max-width: 80%; white-space: normal; overflow-wrap: anywhere; }
.msg.ai .bubble { flex: 1; min-width: 0; background: #fff; border: 1px solid var(--line); border-radius: 4px 14px 14px 14px; padding: 16px 20px; }
.ai-mark { width: 32px; height: 32px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; flex: none; }
.md h2 { font-size: 20px; margin-top: 16px; }
.md h3 { font-size: 17px; margin-top: 16px; }
.md h4, .md h5 { font-size: 16px; margin-top: 12px; }
.md > :first-child { margin-top: 0; }
.md ul, .md ol { padding-left: 22px; margin: 0 0 12px; }
.md li { margin: 4px 0; }
.md li.nested { margin-left: 20px; }
.md blockquote { margin: 0 0 12px; padding: 12px 16px; border-left: 3px solid var(--accent); background: #fdf8ec; border-radius: 0 8px 8px 0; }
.md code { background: var(--bg); padding: 2px 6px; border-radius: 4px; font-size: 14px; }
.md pre { background: #10202b; color: #e6edf2; padding: 16px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; color: inherit; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.table-wrap { overflow-x: auto; margin: 0 0 12px; }
.md table, table.data { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th, .md td, table.data th, table.data td { border: 1px solid var(--line); padding: 8px 10px; text-align: left; vertical-align: top; }
.md th, table.data th { background: var(--bg); font-weight: 600; }
.msg-actions { display: flex; justify-content: flex-end; margin-top: 8px; }
.copy-btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 4px 10px; font-size: 13px; color: var(--muted); cursor: pointer; }
.copy-btn:hover { color: var(--brand); border-color: var(--brand); }
.dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 820px; width: calc(100% - 48px); margin: 0 auto; padding: 8px; background: #fff; border: 1px solid var(--line); border-radius: 12px; }
.composer:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.composer textarea { flex: 1; border: 0; resize: none; padding: 8px; max-height: 200px; background: transparent; line-height: 1.5; }
.composer textarea:focus { outline: none; box-shadow: none; }
.send { width: 42px; height: 42px; padding: 0; }
.composer-note { text-align: center; font-size: 12px; color: var(--muted); margin: 8px 16px 12px; }

/* Admin */
.admin { overflow-y: auto; padding: 32px 24px; }
.admin-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; max-width: 1000px; margin: 0 auto 24px; }
.admin-head h1 { margin: 0; font-size: 26px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; max-width: 1000px; margin: 0 auto 24px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--r); padding: 16px; display: grid; gap: 4px; }
.stat small { color: var(--muted); font-size: 14px; }
.stat strong { font-size: 26px; }
.admin .table-wrap { max-width: 1000px; margin: 0 auto; background: #fff; border-radius: var(--r); }
.tag { background: var(--brand-soft); color: var(--brand); font-size: 12px; padding: 1px 6px; border-radius: 4px; font-weight: 600; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 50; max-width: calc(100% - 32px); }
.scrim { position: fixed; inset: 0; background: rgba(16,32,43,.4); z-index: 30; }

@media (max-width: 960px) {
  .auth { grid-template-columns: 1fr; }
  .auth-intro { padding: 40px 24px; }
  .auth-intro h1 { font-size: 30px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .topbar { padding: 8px 12px; }
  .role-pick { min-width: 0; }
  .role-pick select { max-width: 170px; }
  .thread { padding: 20px 16px; }
  .welcome h1 { font-size: 24px; }
  .quick-grid { grid-template-columns: 1fr; }
  .composer { width: calc(100% - 24px); }
  .msg.ai .ai-mark { display: none; }
  .msg.ai .bubble { padding: 14px 16px; }
  .msg.user .bubble { max-width: 90%; }
  .admin { padding: 20px 16px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
