:root {
  --bg: #f4f6f9; --panel: #ffffff; --panel2: #f8fafc; --line: #e2e7ee; --line2: #eef1f5;
  --text: #1c2533; --muted: #64707f; --accent: #1d5c8f; --accent-2: #e8f0f8; --accent-text: #fff;
  --ok: #1f7a4d; --ok-bg: #e5f4ec; --warn: #9a5b00; --warn-bg: #fff3dc; --bad: #b23a3a; --bad-bg: #fbe9e9;
  --info: #2f5fb3; --info-bg: #e8effc; --gray-bg: #eef1f5; --violet: #6b46c1; --violet-bg: #f0ebfb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .05);
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b; --panel: #161d27; --panel2: #1b2330; --line: #29333f; --line2: #222b36;
    --text: #e3e8ef; --muted: #94a0b0; --accent: #5aa0dc; --accent-2: #1b2c3e; --accent-text: #0b1520;
    --ok: #59c48e; --ok-bg: #15301f; --warn: #f0b860; --warn-bg: #352812; --bad: #ef8080; --bad-bg: #3a1c1c;
    --info: #86aef5; --info-bg: #1a2742; --gray-bg: #242d39; --violet: #b49cf0; --violet-bg: #2a2240;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 2px; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=search], input[type=date], input[type=url], input[type=password], select, textarea {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; min-height: 36px; width: 100%;
  font-size: 16px; /* iOS: không tự phóng to */
}
@media (min-width: 900px) { input[type=text], input[type=search], input[type=date], input[type=url], input[type=password], select, textarea { font-size: 14px; } }
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 7px 14px; min-height: 36px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font-weight: 600; }
.icon-btn { background: none; border: 1px solid transparent; border-radius: 6px; padding: 3px 7px; cursor: pointer; color: var(--muted); font-size: 13px; white-space: nowrap; }
.icon-btn:hover { border-color: var(--line); color: var(--accent); background: var(--panel); }

/* Đăng nhập */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(360px, 100%); display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card label { display: grid; gap: 6px; font-weight: 600; }
.err { color: var(--bad); margin: 0; min-height: 1em; }

/* Thanh trên */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 24px; padding: 0 20px; height: 56px;
  background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; }
.brand b { display: block; font-size: 14.5px; }
.brand small { display: block; color: var(--muted); font-size: 11.5px; }
.brand.big b { font-size: 18px; }
.logo { width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.mainnav { display: flex; height: 100%; }
.mainnav a { display: flex; align-items: center; padding: 0 14px; font-weight: 700; font-size: 13px; letter-spacing: .04em; color: var(--muted); border-bottom: 2px solid transparent; }
.mainnav a.active { color: var(--accent); border-bottom-color: var(--accent); text-decoration: none; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }

.wrap { max-width: 1440px; margin: 0 auto; padding: 20px 20px 60px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Thống kê */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; cursor: pointer; text-align: left; box-shadow: var(--shadow); }
.stat:hover { border-color: var(--accent); }
.stat .n { font-size: 26px; font-weight: 700; line-height: 1.1; }
.stat .t { font-weight: 600; }
.stat .s { color: var(--muted); font-size: 12px; }
.stat .bar { width: 28px; height: 4px; border-radius: 2px; margin-bottom: 8px; }

/* Tab */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabs button { background: none; border: 0; padding: 10px 14px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 18px; padding: 0 6px; border-radius: 9px; background: var(--bad); color: #fff; font-size: 11px; line-height: 18px; text-align: center; margin-left: 4px; }

/* Kho */
.lib-layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 18px; align-items: start; }
.cats { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; position: sticky; top: 70px; }
.cats-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 8px; }
.cats ul { list-style: none; margin: 0; padding: 0; }
.cats li button { width: 100%; display: flex; justify-content: space-between; gap: 8px; background: none; border: 0; padding: 7px 8px; border-radius: 7px; cursor: pointer; text-align: left; }
.cats li button:hover { background: var(--panel2); }
.cats li button.active { background: var(--accent-2); color: var(--accent); font-weight: 600; }
.cats li .cnt { color: var(--muted); font-size: 12px; }
.cat-select { display: none; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar select { width: auto; min-width: 150px; flex: 0 1 auto; }
.search { flex: 1 1 320px; position: relative; }
.search span { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search input { padding-left: 30px; }
.result-line { margin: 2px 2px 8px; }

.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; }
.grid th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; padding: 9px 10px; border-bottom: 1px solid var(--line); background: var(--panel2); white-space: nowrap; }
.grid td { padding: 10px; border-bottom: 1px solid var(--line2); vertical-align: top; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover td { background: var(--panel2); }
.grid tbody tr:last-child td { border-bottom: 0; }
.c-num { width: 150px; } .c-type { width: 104px; white-space: nowrap; } .c-iss { width: 180px; } .c-date { width: 96px; white-space: nowrap; }
.c-st { width: 150px; } .c-act { width: 1%; white-space: nowrap; text-align: right; } .c-kind { width: 170px; } .c-file { width: 200px; }
.num { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.title { font-weight: 600; }
.sub { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; padding: 1px 8px; border-radius: 10px; background: var(--gray-bg); color: var(--muted); white-space: nowrap; }
.chip.cat { background: var(--accent-2); color: var(--accent); }
.st { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 10px; white-space: nowrap; }
.st.active { background: var(--ok-bg); color: var(--ok); }
.st.pending { background: var(--info-bg); color: var(--info); }
.st.amended { background: var(--violet-bg); color: var(--violet); }
.st.replaced, .st.expired { background: var(--bad-bg); color: var(--bad); }
.st.unknown { background: var(--gray-bg); color: var(--muted); }
.st.warn { background: var(--warn-bg); color: var(--warn); }
.kind { font-size: 11.5px; font-weight: 600; padding: 1px 8px; border-radius: 4px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.kind.internal { border-color: color-mix(in srgb, var(--warn) 45%, transparent); color: var(--warn); }
.kind.reference { border-color: color-mix(in srgb, var(--violet) 45%, transparent); color: var(--violet); }
.alert { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.alert.bad { background: var(--bad-bg); color: var(--bad); }
.alert.info { background: var(--info-bg); color: var(--info); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.auto-note { color: var(--muted); font-size: 11px; }

/* Feed */
.feed-info { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; font-size: 13px; }
.feed-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.feed-list { display: grid; gap: 8px; }
.fitem { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 16px; }
.fitem.unseen { border-left: 3px solid var(--accent); }
.fitem.dismissed { opacity: .6; }
.fitem .meta { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.fitem .meta b { color: var(--text); font-weight: 600; }
.fitem .head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 3px; }
.fitem .fact { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.fitem .fact .row { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.fitem .title { cursor: pointer; }
.fitem .affect { font-size: 12.5px; margin-top: 4px; }
.state { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 4px; text-transform: uppercase; letter-spacing: .03em; }
.state.new { background: var(--accent); color: var(--accent-text); }
.state.unseen { background: var(--info-bg); color: var(--info); }
.state.seen { background: var(--gray-bg); color: var(--muted); }
.state.saved { background: var(--ok-bg); color: var(--ok); }
.state.dismissed { background: var(--gray-bg); color: var(--muted); }

/* Upload */
.drop { display: grid; justify-items: center; gap: 4px; text-align: center; border: 2px dashed var(--line); border-radius: 12px; padding: 26px 16px; background: var(--panel); cursor: pointer; margin-bottom: 14px; }
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-2); }
.drop input { display: none; }
.drop-icon { font-size: 26px; color: var(--accent); }

/* Hộp thoại */
dialog { border: 0; padding: 0; border-radius: 14px; background: var(--panel); color: var(--text); width: min(860px, calc(100vw - 24px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog.wide { width: min(1180px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(15, 20, 27, .45); }
.dlg { display: flex; flex-direction: column; max-height: calc(100vh - 32px); margin: 0; }
.dlg-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; padding: 16px 20px 12px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { font-size: 17px; margin: 0; }
.dlg-body { padding: 16px 20px; overflow-y: auto; display: grid; gap: 14px; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.dlg-foot .left { margin-right: auto; display: flex; gap: 8px; }
.x { background: none; border: 0; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.form-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; }
.form-grid label, .field { display: grid; gap: 5px; font-size: 13px; font-weight: 600; align-content: start; }
.form-grid .s2 { grid-column: span 2; } .form-grid .s3 { grid-column: span 3; } .form-grid .s4 { grid-column: span 4; } .form-grid .s6 { grid-column: span 6; }
.radio-row { display: flex; gap: 8px; flex-wrap: wrap; }
.radio-row label, .check-chips label { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; cursor: pointer; background: var(--panel); }
.radio-row label:has(input:checked), .check-chips label:has(input:checked) { border-color: var(--accent); background: var(--accent-2); color: var(--accent); }
.check-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.check-chips input, .radio-row input { margin: 0; }
.inline { display: flex; gap: 6px; }
.inline > input { flex: 1; }
.detect { font-size: 12.5px; }
.kv { display: grid; grid-template-columns: 160px minmax(0, 1fr); gap: 6px 14px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.view-grid { display: grid; grid-template-columns: minmax(0, 380px) minmax(0, 1fr); gap: 18px; }
.preview { border: 1px solid var(--line); border-radius: 8px; min-height: 420px; background: var(--panel2); overflow: auto; }
.preview iframe { width: 100%; height: 70vh; border: 0; display: block; background: #fff; }
.preview img { max-width: 100%; display: block; margin: 0 auto; }
.preview .docx-wrapper { background: transparent !important; padding: 10px !important; }
.preview .ph { padding: 40px 16px; text-align: center; color: var(--muted); }
.preview:has(> .ph:only-child) { min-height: 0; }
.files { display: grid; gap: 6px; }
.file { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; font-size: 13px; }
.file .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file.active { border-color: var(--accent); background: var(--accent-2); }
.block-title { font-weight: 700; font-size: 13px; margin: 0 0 6px; }
.pre { white-space: pre-wrap; }
.rel-list { display: grid; gap: 4px; font-size: 13px; }
.cat-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) auto; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line2); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 100; max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.2); }

@media (max-width: 1100px) {
  .c-iss { display: none; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .topbar { gap: 12px; padding: 0 12px; }
  .brand small, #lastRun { display: none; }
  .mainnav a { padding: 0 6px; font-size: 12px; }
  .wrap { padding: 14px 12px 50px; }
  .lib-layout { grid-template-columns: 1fr; }
  .cats { position: static; padding: 0; border: 0; background: none; }
  .cats-head, .cats ul { display: none; }
  .cat-select { display: block; }
  .c-type, .c-date + .c-date, .grid td.c-type, .c-kind, .c-file { display: none; }
  .toolbar select { flex: 1 1 140px; min-width: 0; }
  .fitem { grid-template-columns: 1fr; }
  .fitem .fact { align-items: flex-start; }
  .fitem .fact .row { justify-content: flex-start; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid .s3, .form-grid .s4, .form-grid .s6 { grid-column: span 2; }
  .form-grid .s2 { grid-column: span 1; }
  .view-grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 120px minmax(0, 1fr); }
  .page-head h1 { font-size: 19px; }
}
@media (max-width: 560px) {
  .c-num { width: auto; }
  .grid th.c-date, .grid td.c-date { display: none; }
  .grid th.c-st { width: auto; }
  .dlg-foot .btn { flex: 1 1 auto; justify-content: center; }
}
@media (max-width: 480px) {
  .brand > div { display: none; }
  .mainnav a { font-size: 12px; letter-spacing: .02em; }
  .stats { gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .n { font-size: 22px; }
}
