/* =================== DESIGN TOKENS =================== */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --border-strong: #cdd5df;
  --text: #0f1b2d;
  --text-2: #4a5a70;
  --muted: #7a889c;

  --primary: #1a6cf0;
  --primary-hover: #155ad0;
  --primary-soft: #e8f0fe;
  --success: #12935a;
  --success-soft: #e3f5ec;
  --warning: #b86e00;
  --warning-soft: #fff3dc;
  --danger: #d23b3b;
  --danger-soft: #fdeaea;

  --sidebar-bg: #0f1b2d;
  --sidebar-text: #b7c3d4;
  --sidebar-active: #ffffff;

  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(15, 27, 45, 0.05), 0 1px 3px rgba(15, 27, 45, 0.06);
  --shadow-lg: 0 12px 40px rgba(15, 27, 45, 0.18);
  --font: 'Be Vietnam Pro', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
.link { color: var(--primary); cursor: pointer; background: none; border: 0; font: inherit; padding: 0; }
.link:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.text-center { text-align: center; }
.mono { font-family: var(--mono); font-size: 12px; }
.spacer { flex: 1; }
.req { color: var(--danger); }

h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 16px; font-weight: 600; }

/* =================== BUTTONS =================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 16px;
  border-radius: var(--radius-sm); border: 1px solid transparent;
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }
.btn-xs { height: 28px; padding: 0 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn-icon { width: 36px; padding: 0; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-ghost { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
.btn-warning { background: var(--warning-soft); color: var(--warning); border-color: #f2d49c; }
.btn-warning:hover:not(:disabled) { background: #ffe9bf; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b72f2f; }
.btn-danger-ghost { background: var(--surface); color: var(--danger); border-color: #f1c4c4; }
.btn-danger-ghost:hover:not(:disabled) { background: var(--danger-soft); }

/* =================== FORMS =================== */
.form-stack { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-label { font-weight: 500; font-size: 13px; color: var(--text-2); }
.field-hint { font-size: 12.5px; color: var(--muted); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

input[type="text"], input[type="password"], input[type="search"], select, textarea {
  width: 100%; height: 38px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit;
  transition: border-color .15s, box-shadow .15s;
}
textarea { height: auto; padding: 10px 12px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(26, 108, 240, .15);
}

.check-card {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); cursor: pointer;
}
.check-card input { margin-top: 3px; width: 16px; height: 16px; accent-color: var(--primary); flex-shrink: 0; }
.check-card strong { display: block; font-weight: 600; font-size: 13.5px; }
.check-card span { display: block; color: var(--text-2); font-size: 12.5px; margin-top: 2px; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; cursor: pointer; }
.switch {
  appearance: none; width: 42px; height: 24px; border-radius: 999px; flex-shrink: 0;
  background: var(--border-strong); position: relative; cursor: pointer; transition: background .2s;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.switch:checked { background: var(--primary); }
.switch:checked::after { transform: translateX(18px); }

.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.segmented-item {
  border: 0; background: transparent; font: inherit; font-size: 13px; font-weight: 500; color: var(--text-2);
  padding: 6px 12px; border-radius: 5px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.segmented-item.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.pill { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--border); color: var(--text-2); font-size: 11.5px; font-weight: 600; text-align: center; }
.pill-danger { background: var(--danger-soft); color: var(--danger); }
.pill-warn { background: var(--warning-soft); color: var(--warning); }

.alert { padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13px; }
.alert-danger { background: var(--danger-soft); color: var(--danger); }
.alert-warn { background: var(--warning-soft); color: var(--warning); margin-bottom: 12px; }

.code-box {
  display: block; padding: 9px 12px; background: var(--surface-2); border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm); font-family: var(--mono); font-size: 12px; word-break: break-all; user-select: all;
}

/* =================== AUTH =================== */
.auth-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow-lg); padding: 32px 28px; display: flex; flex-direction: column; gap: 22px;
}
.auth-card .segmented { width: 100%; }
.auth-card .segmented-item { flex: 1; justify-content: center; }

/* =================== BRAND =================== */
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center;
  background: linear-gradient(135deg, #1a6cf0, #0fb5a6); color: #fff; font-weight: 700; font-size: 14px; letter-spacing: .5px;
}
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 12px; color: var(--muted); }
.brand-lg .brand-mark { width: 44px; height: 44px; font-size: 16px; }
.brand-lg .brand-name { font-size: 18px; }

/* =================== LAYOUT =================== */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px; flex-shrink: 0; background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; padding: 20px 14px; gap: 24px;
  position: sticky; top: 0; height: 100vh;
}
.sidebar .brand-name { color: #fff; }
.sidebar .brand-sub { color: #7f8ea3; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: var(--radius-sm);
  font-weight: 500; font-size: 13.5px; color: var(--sidebar-text); transition: background .15s, color .15s;
}
.nav-item:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-item.active { background: rgba(26,108,240,.22); color: var(--sidebar-active); }
.nav-icon { width: 18px; text-align: center; opacity: .85; }
.nav-count { margin-left: auto; background: var(--primary); color: #fff; font-size: 11px; font-weight: 600; border-radius: 999px; padding: 0 7px; }

.sidebar-footer { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid rgba(255,255,255,.08); padding-top: 14px; }
.sidebar-footer .btn-ghost { background: transparent; color: var(--sidebar-text); border-color: rgba(255,255,255,.15); }
.sidebar-footer .btn-ghost:hover { background: rgba(255,255,255,.06); color: #fff; border-color: rgba(255,255,255,.25); }
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex-shrink: 0;
  background: #24364f; color: #fff; font-weight: 600; font-size: 13px;
}
.user-chip-text { min-width: 0; }
.user-chip-name { color: #fff; font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip-role { font-size: 11.5px; color: #7f8ea3; }

.main { flex: 1; min-width: 0; padding: 24px 28px 48px; display: flex; flex-direction: column; gap: 20px; }
.topbar { display: flex; align-items: center; gap: 12px; }
.page-title { font-size: 22px; font-weight: 700; }
.page-sub { color: var(--muted); font-size: 13px; }
.menu-toggle { display: none; }

.view { display: flex; flex-direction: column; gap: 20px; }
.two-col { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 20px; align-items: start; }
.stack { display: flex; flex-direction: column; gap: 20px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px; min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar select, .toolbar input { height: 32px; font-size: 13px; width: auto; min-width: 160px; }
.action-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* =================== STATS =================== */
.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat-label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.stat-value { font-size: 26px; font-weight: 700; margin: 2px 0; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12.5px; color: var(--text-2); }
.stat-card-warn .stat-value { color: var(--warning); }

.progress-strip { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; margin-bottom: 14px; }
.progress-cell { padding: 10px 8px; text-align: center; border-right: 1px solid var(--border); background: var(--surface-2); }
.progress-cell:last-child { border-right: 0; }
.progress-cell span { display: block; font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.progress-cell small { color: var(--muted); font-size: 11.5px; }
.cell-danger span { color: var(--danger); }
.cell-warn span { color: var(--warning); }

/* =================== STATUS BADGES =================== */
.status { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.status::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.status-idle { background: var(--surface-2); color: var(--muted); }
.status-pending, .status-running { background: var(--primary-soft); color: var(--primary); }
.status-running::before { animation: pulse 1.2s infinite; }
.status-paused { background: var(--warning-soft); color: var(--warning); }
.status-completed, .status-active { background: var(--success-soft); color: var(--success); }
.status-failed, .status-locked { background: var(--danger-soft); color: var(--danger); }
.status-cancelled { background: #eef1f5; color: var(--text-2); }
@keyframes pulse { 50% { opacity: .3; } }

.role { white-space: nowrap; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: #eef1f5; color: var(--text-2); }
.role-admin { background: #ede7fd; color: #6b3fd4; }

/* =================== CONSOLE =================== */
.console { border: 1px solid #1d2a3d; border-radius: var(--radius-sm); overflow: hidden; }
.console-head { display: flex; justify-content: space-between; align-items: center; padding: 7px 12px; background: #16233a; color: #9fb0c8; font-size: 12px; }
.console-head .link { color: #8fb6ff; }
.console-body { background: #0f1b2d; color: #c9d4e3; font-family: var(--mono); font-size: 12px; height: 320px; overflow-y: auto; padding: 10px 12px; }
.log-line { padding: 1px 0; word-break: break-word; }
.log-success { color: #5fd49a; }
.log-error { color: #ff8a8a; }
.log-warning { color: #ffc266; }

/* =================== TABLE =================== */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th { text-align: left; font-weight: 600; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; background: var(--surface-2); }
.table td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafbfd; }
.table tr.row-active { background: var(--primary-soft); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .empty-row td { text-align: center; color: var(--muted); padding: 32px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.cell-title { font-weight: 600; word-break: break-word; }
.cell-sub { color: var(--muted); font-size: 12px; word-break: break-all; }
.cell-reason { color: var(--text-2); font-size: 12.5px; min-width: 220px; max-width: 360px; word-break: break-word; }
.col-wide { min-width: 240px; }
.num-danger { color: var(--danger); font-weight: 600; }
.num-warn { color: var(--warning); font-weight: 600; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.filters select, .filters input { height: 34px; width: auto; min-width: 200px; flex: 1; }
.filters .segmented { flex-shrink: 0; }
.pager { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; gap: 10px; flex-wrap: wrap; }
.pager > div { display: flex; gap: 6px; }

/* =================== SO SÁNH =================== */
.cmp-summary { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.cmp-chip {
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; background: var(--surface);
  text-align: left; font: inherit; cursor: pointer; transition: border-color .15s, box-shadow .15s;
}
.cmp-chip:hover { border-color: var(--border-strong); }
.cmp-chip.active { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(26, 108, 240, .12); }
.cmp-chip-value { display: block; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.cmp-chip-label { display: block; font-size: 12px; color: var(--muted); }
.cmp-chip-sub { display: block; font-size: 11.5px; color: var(--text-2); margin-top: 2px; }
.chip-same .cmp-chip-value { color: var(--success); }
.chip-missing .cmp-chip-value { color: var(--danger); }
.chip-different .cmp-chip-value { color: var(--warning); }
.chip-extra .cmp-chip-value { color: var(--text-2); }
.chip-no_access .cmp-chip-value { color: #6b3fd4; }
.cmp-progress { background: var(--primary-soft); color: var(--primary); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; margin-bottom: 14px; word-break: break-all; }
.status-extra { background: #eef1f5; color: var(--text-2); }
.status-no_access { background: #ede7fd; color: #6b3fd4; }
.status-same { background: var(--success-soft); color: var(--success); }
.status-missing { background: var(--danger-soft); color: var(--danger); }
.status-different { background: var(--warning-soft); color: var(--warning); }
.open-links { display: flex; gap: 10px; white-space: nowrap; }

@media (max-width: 1100px) { .cmp-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 480px) { .cmp-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* =================== ACCOUNTS =================== */
.account-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.account-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.account-top { display: flex; gap: 12px; align-items: center; }
.account-top .avatar { background: var(--primary-soft); color: var(--primary); }
.account-email { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.quota-track { height: 6px; border-radius: 999px; background: var(--border); overflow: hidden; }
.quota-fill { height: 100%; background: var(--primary); border-radius: 999px; }

/* =================== EMPTY STATE =================== */
.empty { text-align: center; padding: 40px 16px; color: var(--text-2); }
.empty-icon { width: 48px; height: 48px; margin: 0 auto 12px; border-radius: 12px; display: grid; place-items: center; background: var(--surface-2); color: var(--muted); font-size: 22px; }

/* =================== MODAL =================== */
.modal { position: fixed; inset: 0; background: rgba(15, 27, 45, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-dialog { width: 100%; max-width: 460px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg); max-height: calc(100vh - 32px); overflow-y: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal #modalError { margin: 0 20px 4px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; }

/* =================== TOASTS =================== */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: #fff; padding: 11px 16px; border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); font-size: 13px; max-width: 380px; animation: toast-in .2s ease-out; }
.toast-error { background: var(--danger); }
.toast-success { background: var(--success); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* =================== RESPONSIVE =================== */
@media (max-width: 1100px) {
  .two-col { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 820px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .sidebar-backdrop { position: fixed; inset: 0; background: rgba(15,27,45,.4); z-index: 30; display: none; }
  .sidebar.open + .sidebar-backdrop { display: block; }
  .menu-toggle { display: inline-flex; }
  .main { padding: 16px 16px 40px; }
  .progress-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .progress-cell:nth-child(3) { border-right: 0; }
  .progress-cell:nth-child(-n+3) { border-bottom: 1px solid var(--border); }
  .field-row { grid-template-columns: 1fr; }
  .table-wrap { margin: 0 -20px; }
}

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat-value { font-size: 21px; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .filters .segmented { width: 100%; overflow-x: auto; }
  .toolbar select, .toolbar input, .filters select, .filters input { min-width: 0; width: 100%; }
}
