:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e5ea;
  --border-strong: #c9ced6;
  --text: #1c2430;
  --text-muted: #687384;
  --accent: #0f6fde;
  --accent-soft: #e6f0fc;
  --ok: #157f3b;
  --ok-soft: #e4f5ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --bad: #c0262d;
  --bad-soft: #fde8e8;
  --bot: #6d3fc0;
  --bot-soft: #efe8fb;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1318;
    --surface: #171c23;
    --surface-2: #1d232b;
    --border: #2a313b;
    --border-strong: #3a4350;
    --text: #e6e9ee;
    --text-muted: #96a0ae;
    --accent: #5aa2ff;
    --accent-soft: #1b2b40;
    --ok: #4fc47a;
    --ok-soft: #16301f;
    --warn: #f0b350;
    --warn-soft: #352713;
    --bad: #ff6b70;
    --bad-soft: #3a1a1c;
    --bot: #b598f5;
    --bot-soft: #2a2140;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
code, kbd, pre { font-family: var(--mono); font-size: 12.5px; }
kbd { padding: 1px 5px; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 4px; background: var(--surface); }
.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Khung ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; white-space: nowrap; }
.brand--large { font-size: 20px; justify-content: center; }
.brand__mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent) 0 50%, var(--ok) 50% 100%);
}
.tabs { display: flex; gap: 4px; height: 100%; overflow-x: auto; flex: 1; }
.tab {
  border: 0; background: none; color: var(--text-muted); font: inherit; font-weight: 550;
  padding: 0 12px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.account { display: flex; align-items: center; gap: 10px; margin: 0; }
.account__name { color: var(--text-muted); }

main { padding: 20px; max-width: 1400px; margin: 0 auto; }
.panel { display: flex; flex-direction: column; gap: 12px; }

/* ---------- Nút & ô nhập ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 550; text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn--danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn--ghost { border-color: transparent; background: transparent; }
.btn--sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn--block { width: 100%; }
.btn:focus-visible, .tab:focus-visible, .input:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.input, .field input, .field select {
  height: 34px; padding: 0 10px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font: inherit;
}
.input[type="file"] { height: auto; padding: 8px; }
.input--search { width: 200px; }
.field { display: flex; flex-direction: column; gap: 4px; font-weight: 550; font-size: 13px; }
.field input, .field select { font-weight: 400; font-size: 14px; }

/* ---------- Thanh công cụ ---------- */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.toolbar__group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar__stats { color: var(--text-muted); font-size: 13px; }
.hint { margin: 0; color: var(--text-muted); font-size: 12.5px; }

/* ---------- Bảng ---------- */
.grid-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: auto; max-height: calc(100vh - 210px);
}
.grid { width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }
.grid th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2); color: var(--text-muted); font-weight: 600; font-size: 12px;
  text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.grid th[data-sort] { cursor: pointer; user-select: none; }
.grid th[data-sort]:hover { color: var(--text); }
.grid th[data-dir="asc"]::after { content: " ▲"; font-size: 9px; }
.grid th[data-dir="desc"]::after { content: " ▼"; font-size: 9px; }
.grid td { padding: 0 12px; height: 34px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.grid tbody tr:hover td { background: var(--surface-2); }
.grid .num { text-align: right; }
.grid .col-idx { width: 44px; color: var(--text-muted); font-size: 12px; text-align: right; }
.grid .col-symbol { font-weight: 650; letter-spacing: .02em; }
.grid .col-actions { width: 40px; text-align: center; }
.grid .meta { color: var(--text-muted); font-size: 12.5px; }
.grid .reason { white-space: normal; min-width: 220px; padding-top: 6px; padding-bottom: 6px; }
.grid .empty { text-align: center; color: var(--text-muted); padding: 40px 12px; height: auto; white-space: normal; }
.grid--readonly td { height: 36px; }
.grid--compact th, .grid--compact td { padding: 4px 8px; height: 28px; font-size: 12.5px; }

.cell { cursor: cell; position: relative; }
.cell.is-active { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft) !important; }
.cell.is-editing { padding: 0; box-shadow: inset 0 0 0 2px var(--accent); }
.cell-editor {
  width: 100%; height: 34px; border: 0; outline: 0; padding: 0 12px;
  background: var(--surface); color: var(--text); font: inherit; text-align: inherit;
}
.cell .saving { opacity: .5; }
.flash-ok { animation: flash-ok 1.2s ease-out; }
.flash-error { animation: flash-error 1.2s ease-out; }
@keyframes flash-ok { from { background: var(--ok-soft); } }
@keyframes flash-error { from { background: var(--bad-soft); } }

.icon-btn {
  width: 26px; height: 26px; border: 0; border-radius: 6px; background: transparent;
  color: var(--text-muted); cursor: pointer; opacity: 0;
}
tr:hover .icon-btn, .icon-btn:focus-visible { opacity: 1; }
.icon-btn:hover { background: var(--bad-soft); color: var(--bad); }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill--yes, .pill--ok { background: var(--ok-soft); color: var(--ok); }
.pill--no { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.pill--error { background: var(--bad-soft); color: var(--bad); }
.pill--warn { background: var(--warn-soft); color: var(--warn); }
.by { margin-left: 4px; }
.by--bot { color: var(--bot); background: var(--bot-soft); padding: 0 6px; border-radius: 4px; }

/* ---------- Tra cứu ---------- */
.lookup-form { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.lookup-form .input { width: 180px; }
.lookup-result { display: flex; flex-direction: column; gap: 10px; max-width: 560px; }
.bubble {
  margin: 0; padding: 14px 16px; white-space: pre-wrap; word-break: break-word;
  background: var(--surface); border: 1px solid var(--border); border-radius: 4px 14px 14px 14px;
  box-shadow: var(--shadow); font: 14px/1.5 var(--font);
}
.decision { display: flex; flex-direction: column; gap: 2px; padding: 12px 16px; border-radius: var(--radius); border: 1px solid; }
.decision strong { font-size: 16px; }
.decision span { font-size: 13px; }
.decision--ok { background: var(--ok-soft); color: var(--ok); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.decision--error { background: var(--bad-soft); color: var(--bad); border-color: color-mix(in srgb, var(--bad) 30%, transparent); }
.decision--warn { background: var(--warn-soft); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }

/* ---------- Hệ thống ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; align-items: start; }
.card {
  display: flex; flex-direction: column; gap: 12px; padding: 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.card h2 { margin: 0; font-size: 15px; }
.card__body p { margin: 0 0 6px; }
.card__actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card .btn { align-self: flex-start; }
.status { font-weight: 650; }
.status--ok { color: var(--ok); }
.status--warn { color: var(--warn); }
.status--bad { color: var(--bad); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 8px 0; font-size: 13px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; word-break: break-all; }
.sources { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.sources em { color: var(--text-muted); font-style: normal; font-size: 12px; }
.sources small, .checklist small { display: block; color: var(--text-muted); }
.checklist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.checklist li { padding-left: 24px; position: relative; }
.checklist li.is-ok::before { content: "✓"; color: var(--ok); position: absolute; left: 4px; font-weight: 700; }
.checklist li.is-bad::before { content: "✕"; color: var(--bad); position: absolute; left: 4px; font-weight: 700; }

/* ---------- Telegram ---------- */
.card--wide { grid-column: 1 / -1; }
.card h2 { display: flex; align-items: center; gap: 8px; }
.step {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 700;
}
.inline-form { display: flex; align-items: flex-end; gap: 8px; flex-wrap: wrap; }
.inline-form .input { width: 240px; }
.card .inline-form .btn { align-self: flex-end; }
.steps-hint { margin: 0; padding-left: 20px; color: var(--text-muted); font-size: 13px; display: flex; flex-direction: column; gap: 2px; }
.check { display: flex; gap: 8px; align-items: baseline; font-size: 13.5px; }
.input--short { width: 110px; }
.grid-wrap--auto { max-height: 360px; }

/* ---------- Hộp thoại & thông báo ---------- */
.dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 22px; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(15, 20, 30, .45); }
.dialog h2 { margin: 0; font-size: 17px; }
.dialog p { margin: 0; }
.dialog__actions { display: flex; justify-content: flex-end; gap: 8px; }
.form-stack { display: flex; flex-direction: column; gap: 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--bad); white-space: pre-line; font-size: 13px; }
.radio-group { border: 0; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.radio-group label { display: flex; gap: 8px; align-items: baseline; font-weight: 400; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: var(--radius);
  background: var(--text); color: var(--surface); font-weight: 550; box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  white-space: pre-line;
}
.toast[data-kind="error"] { background: var(--bad); color: #fff; }

.notice { padding: 12px 14px; border-radius: var(--radius); font-size: 13.5px; }
.notice p { margin: 6px 0; }
.notice pre { white-space: pre-wrap; word-break: break-all; margin: 6px 0 0; }
.notice--error { background: var(--bad-soft); color: var(--bad); }
.notice--warn { background: var(--warn-soft); color: var(--warn); }
.notice--ok { background: var(--ok-soft); color: var(--ok); }

/* ---------- Đăng nhập ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); padding: 32px 28px; display: flex; flex-direction: column; gap: 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); text-align: center;
}
.login-card > p { margin: -10px 0 0; }
.login-card .form-stack { text-align: left; }

/* ---------- Cài đặt ---------- */
.setup-card {
  width: min(680px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 18px; margin: 24px 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.setup-card > p { margin: 0; text-align: center; }
.setup-section { display: flex; flex-direction: column; gap: 10px; }
.setup-section h2 { margin: 0; font-size: 15px; }
.checklist li.is-warn::before { content: "!"; color: var(--warn); position: absolute; left: 6px; font-weight: 700; }
.config-box {
  width: 100%; padding: 12px; border-radius: var(--radius); border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--text); font: 12.5px/1.5 var(--mono); resize: vertical;
}
details summary { cursor: pointer; color: var(--text-muted); margin-bottom: 8px; }

/* ---------- Điện thoại ---------- */
@media (max-width: 720px) {
  .topbar { height: auto; flex-wrap: wrap; gap: 0 12px; padding: 10px 16px 0; }
  .brand { flex: 1; }
  .tabs { order: 3; flex-basis: 100%; height: 42px; margin: 6px -16px 0; padding: 0 8px; }
  .account__name { display: none; }
  main { padding: 16px; }
  .input--search { width: 100%; }
  .toolbar__group { width: 100%; }
  .grid-wrap { max-height: none; }
  .form-grid { grid-template-columns: 1fr; }
  .lookup-form .input { width: 100%; }
  .lookup-form .field, .lookup-form .btn { flex: 1 1 100%; }
  .inline-form .input { width: 100%; }
  .inline-form .field, .inline-form .btn { flex: 1 1 100%; }
  .icon-btn { opacity: 1; }
}
