:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #17202e;
  --muted: #5d6879;
  --line: #dde2ea;
  --brand: #1f3a6b;
  --brand-ink: #ffffff;
  --accent: #c8792b;
  --ok: #1f7a4d;
  --warn: #9a6a00;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c; --surface: #171e29; --surface-2: #1f2835; --text: #e7ebf2; --muted: #9aa5b6;
    --line: #2b3545; --brand: #8fb0ea; --brand-ink: #0f141c; --accent: #e3a066;
    --ok: #5cc28d; --warn: #e2b54d; --danger: #f08a80;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.5; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .4rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }
code { background: var(--surface-2); padding: 0 .3em; border-radius: 4px; font-size: .9em; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.error { color: var(--danger); min-height: 1.2em; margin: .2rem 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
        padding: 1rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.row { display: flex; gap: .5rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }

label { display: block; font-size: .9rem; font-weight: 600; margin-bottom: .75rem; }
label.check { display: flex; gap: .4rem; align-items: center; font-weight: 400; margin: 0; }
input, select, textarea { width: 100%; font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: .55rem .7rem; margin-top: .25rem; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.row input, .row select { margin-top: 0; width: auto; flex: 1 1 160px; }
input[type=checkbox], .row input[type=checkbox] { width: auto; flex: none; margin: 0; }

.btn { font: inherit; font-weight: 600; border: 1px solid var(--line); background: var(--surface);
       color: var(--text); border-radius: 8px; padding: .5rem .9rem; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .6; cursor: wait; }
.btn.file { display: inline-block; margin: 0; }
.link { background: none; border: 0; color: var(--brand); font: inherit; cursor: pointer; padding: .2rem; text-decoration: underline; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 420px; padding: 1.5rem; }
.login-card .link { display: block; margin: .8rem auto 0; }
.brand { display: flex; gap: .8rem; align-items: center; margin-bottom: 1.2rem; }
.brand h1 { font-size: 1.25rem; margin: 0; }
.brand p { margin: .1rem 0 0; font-size: .9rem; }
.mark { width: 44px; height: 44px; border-radius: 10px; background: var(--brand); color: var(--brand-ink);
        display: grid; place-items: center; font-weight: 800; letter-spacing: .5px; flex: none; }

/* app shell */
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap;
          padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.small-brand { margin: 0; }
.small-brand .mark { width: 34px; height: 34px; font-size: .85rem; border-radius: 8px; }
.small-brand .app-title { font-weight: 700; }
.who { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; font-size: .9rem; }
.tabs { display: flex; gap: .25rem; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--line);
        overflow-x: auto; position: sticky; top: 0; z-index: 5; }
.tabs button { font: inherit; background: none; border: 0; padding: .75rem .8rem; cursor: pointer; color: var(--muted);
               border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tab { max-width: 880px; margin: 0 auto; padding: 1.2rem 16px 4rem; }

/* ask */
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.chip { font: inherit; font-size: .88rem; border: 1px solid var(--line); background: var(--surface); color: var(--text);
        padding: .3rem .7rem; border-radius: 999px; cursor: pointer; }
.chip:hover { border-color: var(--brand); }
.answer .q { font-weight: 700; font-size: 1.05rem; margin-bottom: .5rem; }
.answer .body p { margin: .4rem 0; }
.answer .body ul, .answer .body ol { margin: .3rem 0; padding-left: 1.3rem; }
.cite { font: inherit; font-size: .75rem; font-weight: 700; vertical-align: super; line-height: 1; background: var(--surface-2);
        color: var(--brand); border: 0; border-radius: 4px; padding: .1rem .3rem; cursor: pointer; margin: 0 1px; }
.thinking { color: var(--muted); font-style: italic; }
.thinking::after { content: "…"; animation: dots 1.2s steps(3) infinite; }
@keyframes dots { 0% { content: "."; } 33% { content: ".."; } 66% { content: "..."; } }
.sources { margin-top: .8rem; border-top: 1px solid var(--line); padding-top: .6rem; }
.sources summary { cursor: pointer; font-weight: 600; font-size: .9rem; }
.source { padding: .5rem 0; border-bottom: 1px dashed var(--line); font-size: .9rem; }
.source:last-child { border-bottom: 0; }
.source.flash { background: var(--surface-2); border-radius: 6px; }
.source .snip { color: var(--muted); white-space: pre-wrap; margin-top: .2rem; }
.answer-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .8rem; }
.compare-box { margin-top: .8rem; padding: .8rem; background: var(--surface-2); border-radius: 10px; }

/* rules */
.rule { border-left: 4px solid var(--brand); }
.rule .meta { font-size: .82rem; color: var(--muted); display: flex; gap: .6rem; flex-wrap: wrap; margin-bottom: .3rem; }
.rule .text { white-space: pre-wrap; margin: .4rem 0; }
.rule .text.clamp { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.badge { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: .1rem .45rem;
         border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.published { color: var(--ok); }
.badge.draft { color: var(--warn); }
.badge.archived { color: var(--muted); }
.cat-head { margin: 1.2rem 0 .5rem; font-size: .95rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.filters { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr auto; gap: .5rem; }
.filters input, .filters select { margin: 0; }
@media (max-width: 720px) { .filters { grid-template-columns: 1fr; } }
.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }

/* tables */
table.list { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.list th, table.list td { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; }
.table-wrap { overflow-x: auto; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .7rem; }
.stat b { display: block; font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .82rem; }
.progress { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: .3rem; }
.progress i { display: block; height: 100%; background: var(--brand); }

/* modal & toast */
dialog { border: 0; border-radius: var(--radius); padding: 0; width: min(720px, calc(100vw - 32px));
         background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10, 15, 25, .45); }
#modal-body { padding: 1.2rem; max-height: 85vh; overflow: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; flex-wrap: wrap; margin-top: .8rem; }
.pw-box { font-family: ui-monospace, Menlo, monospace; font-size: 1.3rem; background: var(--surface-2); padding: .6rem;
          border-radius: 8px; text-align: center; user-select: all; }
.draft-out { white-space: pre-wrap; background: var(--surface-2); padding: .8rem; border-radius: 8px; min-height: 4rem; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg);
         padding: .6rem 1rem; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw; }
#toast.show { opacity: 1; }

/* chat */
.tab.wide { max-width: 1100px; }
.chat-tab { display: flex; flex-direction: column; min-height: calc(100vh - 112px); padding-bottom: 0; }
.thread { flex: 1; display: flex; flex-direction: column; gap: .8rem; padding-bottom: 1rem; }
.welcome { padding: 1.5rem 0 .5rem; }
.welcome h2 { font-size: 1.4rem; }
.msg { max-width: 100%; }
.msg.user { align-self: flex-end; max-width: 85%; background: var(--brand); color: var(--brand-ink);
            padding: .55rem .9rem; border-radius: 14px 14px 4px 14px; }
.msg.user p { margin: 0; white-space: pre-wrap; }
.msg.bot { align-self: stretch; background: var(--surface); border: 1px solid var(--line); border-radius: 14px 14px 14px 4px;
           padding: .8rem 1rem; box-shadow: var(--shadow); }
.msg.bot .body p, .msg.bot .md p { margin: .35rem 0; }
.msg.bot .body ul, .msg.bot .body ol, .md ul, .md ol { margin: .3rem 0; padding-left: 1.3rem; }
.live { color: var(--muted); font-size: .92rem; white-space: pre-wrap; max-height: 14rem; overflow: auto; }
.composer-wrap { position: sticky; bottom: 0; background: var(--bg); padding: .6rem 0 1rem; }
.composer { display: flex; gap: .5rem; align-items: flex-end; background: var(--surface); border: 1px solid var(--line);
            border-radius: 14px; padding: .4rem .4rem .4rem .8rem; box-shadow: var(--shadow); }
.composer textarea { border: 0; margin: 0; padding: .45rem 0; resize: none; max-height: 180px; }
.composer textarea:focus { outline: none; }
.builder-banner { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap;
                  background: var(--surface-2); border-radius: 10px; padding: .4rem .8rem; margin-bottom: .5rem; font-size: .88rem; }
.others-box { margin-top: .7rem; padding: .7rem .8rem; background: var(--surface-2); border-radius: 10px; }
.answer-actions { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .6rem; }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* rule builder */
.step h4 { margin: 1rem 0 .3rem; font-size: .95rem; }
.step > p:first-child { margin-top: 0; }
.step.locked { opacity: .6; }
.step.locked textarea, .step.locked .row { display: none; }
.step textarea { margin: .2rem 0 .5rem; }
.pick-row { display: flex; justify-content: space-between; gap: .8rem; align-items: center; padding: .6rem 0; border-bottom: 1px dashed var(--line); }
label.pick { display: flex; gap: .6rem; align-items: flex-start; font-weight: 400; font-size: .9rem; margin: 0; padding: .5rem 0;
             border-bottom: 1px dashed var(--line); cursor: pointer; }
label.pick input { margin-top: .3rem; }
label.pick .snip { display: block; color: var(--muted); white-space: pre-wrap; margin-top: .15rem; font-size: .85rem; }
details.mini { margin: .4rem 0; }
details.mini summary { cursor: pointer; font-size: .88rem; color: var(--brand); }
.draft-card { border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 0 10px 10px 0; padding: .7rem .9rem; margin: .6rem 0; }
.draft-card h3 { margin: .3rem 0 .1rem; }
.rule-text { white-space: pre-wrap; margin: .4rem 0; }
.rule-text.boxed { background: var(--surface-2); border-radius: 8px; padding: .7rem .9rem; }
.warn { border: 1px solid var(--warn); border-radius: 8px; padding: .5rem .8rem; margin: .5rem 0; font-size: .92rem; }
.warn p { margin: .2rem 0; }
ol.qs { padding-left: 1.3rem; margin: .2rem 0 .6rem; }
ol.qs li { margin: .25rem 0; }
.approval { background: var(--surface-2); border-radius: 10px; padding: .8rem; margin-top: .5rem; }
.approval input, .approval select { background: var(--surface); }
.done-box { border: 1px solid var(--ok); border-radius: 10px; padding: .7rem .9rem; }
.done-box p { margin-top: 0; }

/* register & trail */
.register td.no { font-weight: 700; color: var(--brand); white-space: nowrap; }
.register td { vertical-align: top; }
h4 { margin: 1rem 0 .4rem; }
ol.trail { list-style: none; padding: 0; margin: 0; border-left: 2px solid var(--line); }
ol.trail li { position: relative; padding: .2rem 0 .8rem 1rem; }
ol.trail li::before { content: ""; position: absolute; left: -6px; top: .55rem; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }
.diff ins { background: color-mix(in srgb, var(--ok) 22%, transparent); text-decoration: none; border-radius: 3px; }
.diff del { background: color-mix(in srgb, var(--danger) 18%, transparent); border-radius: 3px; }
button.related { display: block; width: 100%; text-align: left; font: inherit; color: inherit; background: var(--surface);
                 border: 1px solid var(--line); border-radius: 8px; padding: .5rem .7rem; margin: .3rem 0; cursor: pointer; }
button.related:hover { border-color: var(--brand); }
@media (max-width: 720px) {
  .register th:nth-child(3), .register td:nth-child(3), .register th:nth-child(5), .register td:nth-child(5) { display: none; }
  .msg.user { max-width: 92%; }
}
