/* JCP AI Assistant */
:root {
  --bg: #f5f6f8; --panel: #ffffff; --text: #1d2330; --muted: #5f6b7a; --line: #dfe3e8;
  --brand: #1f4fa3; --brand-ink: #ffffff; --accent: #e8f0fd; --user: #1f4fa3; --user-ink: #fff;
  --ok: #1d7a46; --ok-bg: #e7f6ed; --warn: #8a5a00; --warn-bg: #fff4dc; --err: #a32020; --err-bg: #fdeaea;
  --draft-bg: #f2fbf5; --draft-line: #9fd5b3; --mark: #ffec99; --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --panel: #1b2029; --text: #e6e9ef; --muted: #9aa5b4; --line: #2d3440;
    --brand: #6d9cf0; --brand-ink: #0d1320; --accent: #1f2a3d; --user: #2f5fb8; --user-ink: #fff;
    --ok: #6fd39a; --ok-bg: #16301f; --warn: #f1c36b; --warn-bg: #33290f; --err: #ff8f8f; --err-bg: #3a1919;
    --draft-bg: #132a1c; --draft-line: #2f6d45; --mark: #5c4d00; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif; }
a { color: var(--brand); }
h1 { font-size: 1.4rem; margin: .2rem 0 .8rem; }
h2 { font-size: 1.15rem; margin: 1.2rem 0 .6rem; }
h3 { font-size: 1.02rem; margin: 1rem 0 .4rem; }
.small { font-size: .85rem; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.inline { display: inline; }
code { background: var(--accent); padding: 0 .3em; border-radius: 4px; }
mark { background: var(--mark); color: inherit; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .3em; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: .5rem .9rem; border-radius: 9px; font: inherit; cursor: pointer; text-decoration: none; min-height: 40px; }
.btn:hover { border-color: var(--brand); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.danger { background: var(--err-bg); color: var(--err); border-color: var(--err); }
.btn.small { padding: .25rem .6rem; min-height: 32px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: wait; }
.btn[aria-pressed="true"] { background: var(--accent); border-color: var(--brand); }
.linkbtn { background: none; border: 0; color: var(--brand); cursor: pointer; font: inherit; padding: 0; }
.iconbtn { background: none; border: 0; color: inherit; font-size: 1.15rem; cursor: pointer; padding: .3rem .5rem; border-radius: 8px; min-width: 40px; min-height: 40px; }
.iconbtn:hover { background: var(--accent); }

input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 9px; padding: .55rem .7rem; width: 100%; }
input[type=checkbox], input[type=radio] { width: auto; }
textarea { resize: vertical; }
label { display: block; margin: .6rem 0; font-weight: 500; }
label > input, label > select, label > textarea { margin-top: .25rem; font-weight: 400; }
label.check { display: flex; gap: .5rem; align-items: center; font-weight: 400; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .5rem 1rem; margin: .8rem 0; }

.topbar { display: flex; align-items: center; gap: .6rem; padding: .55rem 1rem; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
.logo { background: var(--brand); color: var(--brand-ink); border-radius: 6px; padding: .05rem .4rem; margin-right: .2rem; font-size: .9em; }
.topnav { margin-left: auto; display: flex; gap: .9rem; align-items: center; }
.langselect { width: auto; padding: .3rem .4rem; }
.stagingbar { background: #b54708; color: #fff; text-align: center; font-weight: 700; padding: .3rem; font-size: .85rem; }
.testbar { background: #b42318; color: #fff; text-align: center; font-weight: 700; padding: .3rem; font-size: .85rem; }

.flashes { max-width: 1100px; margin: .8rem auto 0; padding: 0 16px; }
.flash { padding: .6rem .8rem; border-radius: 9px; margin: .4rem 0; border: 1px solid transparent; }
.flash-ok { background: var(--ok-bg); color: var(--ok); border-color: var(--ok); }
.flash-warn { background: var(--warn-bg); color: var(--warn); border-color: var(--warn); }
.flash-error { background: var(--err-bg); color: var(--err); border-color: var(--err); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; box-shadow: var(--shadow); }
.auth { display: flex; justify-content: center; padding: 8vh 16px; }
.authcard { width: 100%; max-width: 420px; }

/* ---------- chat ---------- */
body.chatpage { height: 100dvh; overflow: hidden; }
.chat-app { display: flex; flex-direction: column; height: 100dvh; }
.chat-body { flex: 1; display: flex; min-height: 0; position: relative; }
.menu-toggle { display: none; }
.sidebar { width: 290px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: .8rem; gap: .4rem; overflow-y: auto; }
.side-h { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: .8rem 0 .2rem; }
.convlist { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.convlist li { display: flex; align-items: center; border-radius: 8px; }
.convlist li.active, .convlist li:hover { background: var(--accent); }
.conv { flex: 1; text-align: left; background: none; border: 0; color: var(--text); font: inherit; padding: .5rem .5rem; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-height: 40px; }
.del { opacity: .5; font-size: .95rem; }
.del:hover { opacity: 1; }
.side-foot { border-top: 1px solid var(--line); padding-top: .6rem; display: grid; gap: .3rem; }
.side-foot p { margin: 0; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 29; }

.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.banner { margin: .6rem 1rem 0; }
.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .9rem; }
.empty { margin: auto; text-align: center; max-width: 640px; }
.examples { display: grid; gap: .5rem; }
.example { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: .65rem .8rem; text-align: left; cursor: pointer; color: var(--text); font: inherit; }
.example:hover { border-color: var(--brand); }

.msg { max-width: 860px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: .4rem; }
.msg.user { align-items: flex-end; }
.msg .bubble { border-radius: 14px; padding: .65rem .9rem; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); color: var(--user-ink); white-space: pre-wrap; max-width: 88%; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow); }
.msg.status-error .bubble { background: var(--err-bg); border-color: var(--err); color: var(--err); }
.msg.status-refused .bubble, .msg.status-truncated .bubble { border-color: var(--warn); }
.content p { margin: .35rem 0; }
.content h4 { margin: .8rem 0 .3rem; font-size: .98rem; }
.content ul, .content ol { margin: .3rem 0; padding-left: 1.3rem; }
.tablewrap { overflow-x: auto; }
.content table { border-collapse: collapse; margin: .5rem 0; font-size: .9rem; }
.content th, .content td { border: 1px solid var(--line); padding: .3rem .5rem; text-align: left; }
.cite { display: inline-flex; align-items: center; justify-content: center; min-width: 1.35em; height: 1.35em; padding: 0 .25em; margin: 0 .1em; font-size: .72em;
  vertical-align: super; border-radius: 5px; border: 1px solid var(--brand); background: var(--accent); color: var(--brand); cursor: pointer; line-height: 1; }
.draft { border: 1px solid var(--draft-line); background: var(--draft-bg); border-radius: 10px; margin: .6rem 0; }
.draft-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .35rem .6rem; border-bottom: 1px dashed var(--draft-line); font-size: .85rem; font-weight: 600; }
.draft-head .btn { flex-shrink: 0; white-space: nowrap; }
.draft-text { white-space: pre-wrap; padding: .6rem .7rem; }
.notices .notice { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: .35rem .6rem; font-size: .85rem; margin: .2rem 0; }
.sources { font-size: .85rem; }
.src-h { font-weight: 600; margin: .1rem 0 .2rem; }
.srcitem { display: flex; gap: .4rem; text-align: left; width: 100%; background: none; border: 0; border-radius: 6px; padding: .3rem .3rem; color: var(--text); cursor: pointer; font: inherit; }
.srcitem:hover { background: var(--accent); }
.srcn { flex-shrink: 0; display: inline-flex; justify-content: center; min-width: 1.5em; border-radius: 5px; background: var(--accent); color: var(--brand); font-weight: 700; }
details.other summary { cursor: pointer; color: var(--muted); padding: .2rem .3rem; }
.msgfoot { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }
.msgfoot .tok { margin-left: auto; }
.fbform { display: grid; gap: .3rem; max-width: 520px; }
.dots i { display: inline-block; width: 6px; height: 6px; margin-right: 3px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; } .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: .5rem 1rem calc(.6rem + env(safe-area-inset-bottom)); }
.review-notice { margin: 0 auto .4rem; max-width: 860px; color: var(--warn); }
.composer-row { display: flex; gap: .5rem; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.composer-row .btn { white-space: nowrap; flex-shrink: 0; min-height: 44px; }
.composer textarea { min-height: 44px; max-height: 200px; resize: none; }

.dialog { border: 1px solid var(--line); border-radius: var(--radius); padding: 0; width: min(680px, calc(100vw - 24px)); max-height: 85dvh; background: var(--panel); color: var(--text); }
.dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: .5rem .9rem; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.dialog-head h2 { margin: 0; font-size: 1rem; }
.dialog-body { padding: .3rem 1rem 1rem; overflow-wrap: anywhere; }
.meta { display: grid; grid-template-columns: max-content 1fr; gap: .15rem .8rem; font-size: .88rem; margin: .4rem 0; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; }
.excerpt { white-space: pre-wrap; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .6rem .7rem; font-size: .9rem; max-height: 50dvh; overflow-y: auto; }

@media (max-width: 820px) {
  .menu-toggle { display: inline-block; }
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; z-index: 30; transform: translateX(-105%); transition: transform .2s ease; width: min(86vw, 320px); box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .messages { padding: .7rem 12px; }
  .composer { padding-left: 12px; padding-right: 12px; }
  .msg.user .bubble { max-width: 95%; }
  .topnav { gap: .5rem; }
}

/* ---------- admin ---------- */
.adminnav { display: flex; gap: .2rem; overflow-x: auto; padding: .4rem 16px; background: var(--panel); border-bottom: 1px solid var(--line); scrollbar-width: thin; }
.adminnav a { white-space: nowrap; padding: .35rem .65rem; border-radius: 8px; text-decoration: none; color: var(--text); font-size: .9rem; }
.adminnav a.active, .adminnav a:hover { background: var(--accent); color: var(--brand); }
.wrap { max-width: 1100px; margin: 0 auto; padding: 1rem 16px 3rem; }
.grid { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid2 { display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.stat .n { font-size: 1.6rem; font-weight: 700; }
.stat .l { color: var(--muted); font-size: .85rem; }
.tablewrap-admin { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.data th, table.data td { padding: .45rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.data th { background: var(--bg); font-weight: 600; white-space: nowrap; }
table.data tr:last-child td { border-bottom: 0; }
.badge { display: inline-block; padding: .05rem .45rem; border-radius: 999px; font-size: .78rem; font-weight: 600; border: 1px solid currentColor; white-space: nowrap; }
.b-approved, .b-ok, .b-current { color: var(--ok); }
.b-draft, .b-warning, .b-new { color: var(--warn); }
.b-needs_ocr, .b-failed, .b-empty, .b-rejected, .b-expired, .b-fail, .b-error { color: var(--err); }
.b-superseded, .b-inactive, .b-info, .b-reviewed, .b-demo, .b-future { color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; margin: .6rem 0; }
.preview { white-space: pre-wrap; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .7rem; max-height: 420px; overflow: auto; font-size: .88rem; }
.chunk { border-left: 3px solid var(--brand); padding: .2rem .6rem; margin: .4rem 0; white-space: pre-wrap; font-size: .85rem; background: var(--bg); }
.tabs { display: flex; gap: .3rem; flex-wrap: wrap; margin: .5rem 0; }
.tabs a { padding: .4rem .8rem; border: 1px solid var(--line); border-radius: 999px; text-decoration: none; color: var(--text); }
.tabs a.active { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: end; margin: .6rem 0; }
.filters label { margin: 0; }
.filters input, .filters select { width: auto; }
.pw { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 1.1rem; background: var(--bg); padding: .2rem .5rem; border-radius: 6px; user-select: all; }
.convmsg { margin: .5rem 0; padding: .6rem .8rem; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); white-space: pre-wrap; }
.convmsg.user { background: var(--accent); }
