:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --line: #e2e6ef;
  --primary: #c8102e;
  --primary-ink: #ffffff;
  --accent: #fcd116;
  --ok: #1a7f4b;
  --warn: #b26a00;
  --err: #b42318;
  --user: #1f3b73;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); min-height: 100vh; }
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
.badge { font-size: 11px; background: var(--accent); padding: 2px 6px; border-radius: 6px; vertical-align: middle; }
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: none; padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; color: var(--muted); }
.tab.active { background: #fde8eb; color: var(--primary); font-weight: 600; }
.api-base { margin-left: auto; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.api-base input { width: 200px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; font: 12px ui-monospace, monospace; }

.tab-panel { display: grid; gap: 16px; padding: 16px 20px; max-width: 1280px; margin: 0 auto; }
#tab-chat { grid-template-columns: minmax(0, 1fr) 360px; height: calc(100vh - 58px); }
#tab-ocr { grid-template-columns: minmax(0, 1fr); align-items: start; }
.tool-row { display: grid; grid-template-columns: minmax(0, 380px) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 900px) {
  #tab-chat, #tab-ocr, .tool-row { grid-template-columns: 1fr; height: auto; }
}
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
h2 { margin: 0 0 6px; font-size: 18px; }
.muted { color: var(--muted); font-size: 13px; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: #eef1f6; color: var(--muted); margin-left: 6px; }
.pill.ok { background: #e3f4ea; color: var(--ok); }
.pill.warn { background: #fff3dc; color: var(--warn); }

button { font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px; border: 1px solid var(--line); background: var(--card); }
button:disabled { opacity: .5; cursor: not-allowed; }
.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 600; }
.ghost { background: none; }
.danger { color: var(--err); }
.icon { padding: 8px 10px; }
select { font: inherit; padding: 6px; border-radius: 8px; border: 1px solid var(--line); }

/* Chat */
.chat { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.chat-head, .panel-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); }
.chat-actions { display: flex; gap: 6px; }
.chat-log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; min-height: 300px; }
.msg { max-width: 80%; padding: 10px 14px; border-radius: 14px; line-height: 1.5; font-size: 14.5px; overflow-wrap: anywhere; }
.msg.bot { background: #f1f3f8; align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.user { background: var(--user); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.system { align-self: center; background: none; color: var(--muted); font-size: 12.5px; padding: 0; }
.msg.error { align-self: center; background: #fdecea; color: var(--err); font-size: 13px; }
.msg p { margin: 0 0 6px; } .msg p:last-child { margin: 0; }
.msg ul { margin: 4px 0 6px; padding-left: 20px; }
.msg h4 { margin: 8px 0 4px; font-size: 14px; }
.msg code { background: rgba(0,0,0,.06); padding: 0 4px; border-radius: 4px; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.chat-input { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.chat-input textarea { flex: 1; resize: none; font: inherit; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; max-height: 140px; }
.upload-banner { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 16px; background: #fff8e1; border-top: 1px solid #f3e2a9; font-size: 14px; }
.consent { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }

/* Form panel */
.form-panel { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.progress { height: 6px; background: #eef1f6; border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.form-group h5 { margin: 10px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.field { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 13px; }
.field .label { color: var(--muted); }
.field .value { grid-column: 1 / -1; font-weight: 500; overflow-wrap: anywhere; }
.field .value.empty { color: #b4bccb; font-weight: 400; }
.field .src { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: #eef1f6; color: var(--muted); }
.field .src.ocr { background: #e6eefc; color: #2456b8; }
.field .src.user { background: #e3f4ea; color: var(--ok); }
.field .err { grid-column: 1 / -1; color: var(--err); font-size: 12px; }
.field.flash { animation: flash 1.2s; }
@keyframes flash { from { background: #fff3b0; } to { background: transparent; } }
.form-ready { background: #e3f4ea; border-radius: 10px; padding: 10px 12px; }
.form-ready p { margin: 0 0 8px; }
pre { background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: 8px; overflow: auto; font-size: 12px; max-height: 420px; }

/* OCR */
.ocr-upload { display: flex; flex-direction: column; gap: 12px; }
.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px; text-align: center; color: var(--muted); cursor: pointer; }
.dropzone.drag { border-color: var(--primary); background: #fde8eb; }
.dropzone input { display: none; }
.previews { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.previews img { width: 100%; max-height: 240px; object-fit: contain; background: #f4f6fa; border-radius: 8px; border: 1px solid var(--line); }
.badges { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.warning { background: #fff3dc; color: var(--warn); padding: 8px 10px; border-radius: 8px; font-size: 13px; margin-bottom: 6px; }
table.kv { width: 100%; border-collapse: collapse; font-size: 14px; margin: 8px 0; }
table.kv td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.kv td:first-child { color: var(--muted); width: 40%; }
table.kv td.mono { font-family: ui-monospace, monospace; font-size: 12px; word-break: break-all; }
.conf { font-size: 11px; color: var(--muted); margin-left: 6px; }
.conf.low { color: var(--err); }

/* Portrait → ID photo */
.verdict { padding: 10px 12px; border-radius: 10px; font-weight: 600; margin: 10px 0; }
.verdict.ok { background: #e3f4ea; color: var(--ok); }
.verdict.bad { background: #fdecea; color: var(--err); }
.issue { padding: 8px 10px; border-radius: 8px; font-size: 13px; margin-bottom: 6px; }
.issue.error { background: #fdecea; color: var(--err); }
.issue.warning { background: #fff3dc; color: var(--warn); }
.portrait-compare { display: grid; grid-template-columns: repeat(2, minmax(0, 240px)); gap: 12px; margin: 12px 0; align-items: start; }
.portrait-compare figure { margin: 0; text-align: center; }
.portrait-compare img { width: 100%; border-radius: 6px; border: 1px solid var(--line); background: repeating-conic-gradient(#eee 0 25%, #fff 0 50%) 0 0 / 16px 16px; }
.portrait-compare figcaption { font-size: 12px; color: var(--muted); margin-top: 4px; }
a.button { display: inline-block; text-decoration: none; padding: 8px 14px; border-radius: 8px; }
