/* ===== Design tokens ===== */
:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e7eaf3;
  --border-strong: #d7dcec;
  --text: #1a2233;
  --text-soft: #5b6477;
  --text-mut: #8a93a6;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --primary-700: #4338ca;
  --green: #16a34a;
  --green-soft: #e7f7ed;
  --amber: #d97706;
  --amber-soft: #fdf2e2;
  --red: #dc2626;
  --red-soft: #fdeaea;
  --indigo: #6366f1;
  --shadow-sm: 0 1px 2px rgba(20,28,50,.06);
  --shadow: 0 6px 24px rgba(20,28,50,.08);
  --shadow-lg: 0 18px 50px rgba(20,28,50,.18);
  --radius: 16px;
  --radius-sm: 10px;
  --sidebar-w: 256px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 14px;
}
a { text-decoration: none; color: inherit; cursor: pointer; }
button { font-family: inherit; }

/* ===== Gate ===== */
.gate {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, #2a2f55, #161a2e);
  z-index: 100;
}
.gate[hidden] { display: none; }
.gate-card {
  text-align: center; color: #e8eaf6; padding: 40px;
}
.gate-logo {
  width: 64px; height: 64px; margin: 0 auto 18px; border-radius: 18px;
  display: grid; place-items: center; font-size: 32px; color: #fff;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 10px 40px rgba(99,102,241,.5);
}
.gate-title { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.gate-msg { color: #aab0d4; font-size: 14px; max-width: 320px; }
.gate-msg.error { color: #ff8d8d; }

/* ===== App shell ===== */
.app { display: flex; min-height: 100vh; }
.app[hidden] { display: none; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: #11152a;
  color: #c7cce4;
  display: flex; flex-direction: column;
  padding: 20px 14px;
  position: sticky; top: 0; height: 100vh;
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px 22px; }
.brand-logo {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  font-size: 20px; color: #fff; background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 6px 18px rgba(99,102,241,.45);
}
.brand-title { font-weight: 700; color: #fff; font-size: 15px; }
.brand-sub { font-size: 12px; color: #7f86ad; }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 12px; border-radius: 11px; font-weight: 500; font-size: 14px;
  color: #aab0d4; transition: background .15s, color .15s;
}
.nav-item:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; box-shadow: 0 6px 16px rgba(79,70,229,.4); }
.nav-ico { font-size: 16px; width: 20px; text-align: center; }
.nav-soon-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: #5b6189; margin: 18px 12px 8px; font-weight: 600;
}
.nav-disabled .nav-item { opacity: .5; cursor: default; }
.nav-disabled .nav-item:hover { background: transparent; color: #aab0d4; }
.sidebar-foot { margin-top: auto; padding-top: 16px; }
.admin-chip {
  background: rgba(255,255,255,.06); border-radius: 10px; padding: 10px 12px;
  font-size: 13px; color: #c7cce4; display: flex; align-items: center; gap: 8px;
}
.admin-chip::before { content: '👤'; }

/* ===== Main ===== */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 14px;
  padding: 14px 28px; background: rgba(244,246,251,.85);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.topbar-title { font-size: 18px; font-weight: 700; flex: 1; }
.burger { display: none; background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--text); }
.content { padding: 28px; max-width: 1240px; width: 100%; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; gap: 7px; justify-content: center;
  border: 1px solid transparent; border-radius: 10px; padding: 9px 16px;
  font-size: 14px; font-weight: 600; cursor: pointer; transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-700); }
.btn-ghost { background: var(--surface); border-color: var(--border-strong); color: var(--text-soft); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-danger:hover { background: #fbdcdc; }
.btn-sm { padding: 6px 11px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== Cards / KPI ===== */
.section-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 18px; }
.section-head h2 { font-size: 16px; margin: 0; font-weight: 700; }
.section-head .sub { color: var(--text-mut); font-size: 13px; margin-top: 2px; }

.kpi-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px;
}
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow-sm); position: relative; overflow: hidden;
}
.kpi-ico {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  font-size: 18px; margin-bottom: 14px;
}
.kpi-label { color: var(--text-mut); font-size: 12.5px; font-weight: 500; }
.kpi-value { font-size: 26px; font-weight: 800; margin-top: 4px; letter-spacing: -.5px; }
.kpi-sub { font-size: 12px; color: var(--text-soft); margin-top: 6px; }
.bg-indigo { background: var(--primary-soft); color: var(--primary); }
.bg-green { background: var(--green-soft); color: var(--green); }
.bg-amber { background: var(--amber-soft); color: var(--amber); }
.bg-red { background: var(--red-soft); color: var(--red); }

/* ===== Panel ===== */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); margin-bottom: 24px; overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.panel-head h3 { margin: 0; font-size: 15px; font-weight: 700; }
.panel-body { padding: 20px; }
.chart-wrap { padding: 12px 8px 4px; height: 320px; }

/* ===== Segments ===== */
.segments { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.seg {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow-sm); border-left: 4px solid var(--seg, #ccc);
}
.seg-count { font-size: 28px; font-weight: 800; letter-spacing: -.5px; }
.seg-title { font-weight: 600; margin-top: 4px; }
.seg-hint { font-size: 12px; color: var(--text-mut); margin-top: 3px; }

/* ===== Table ===== */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; padding: 13px 16px; color: var(--text-mut); font-weight: 600;
  font-size: 12px; text-transform: uppercase; letter-spacing: .03em;
  border-bottom: 1px solid var(--border); background: var(--surface-2);
  white-space: nowrap;
}
tbody td { padding: 14px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.cell-title { font-weight: 600; }
.cell-mut { color: var(--text-mut); font-size: 12.5px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* ===== Badges & pills ===== */
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600;
}
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-gray { background: #eef1f6; color: var(--text-soft); }
.badge-indigo { background: var(--primary-soft); color: var(--primary); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.lvl-pill { display: inline-flex; align-items: center; gap: 7px; font-weight: 700; }
.lvl-emoji { font-size: 17px; }
.chip-block {
  display: inline-block; padding: 3px 9px; border-radius: 7px; font-size: 11px; font-weight: 600;
  background: #eef1f6; color: var(--text-soft);
}
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.dot-on { background: var(--green); }
.dot-off { background: #cbd2df; }

/* ===== Block group header (missions) ===== */
.block-group { margin-bottom: 26px; }
.block-group-head {
  display: flex; align-items: center; gap: 10px; margin: 0 0 12px; font-weight: 700; font-size: 14px;
}
.block-group-head .count { color: var(--text-mut); font-weight: 500; font-size: 13px; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(17,21,42,.55);
  display: grid; place-items: center; z-index: 80; padding: 20px;
  animation: fade .15s ease;
}
.modal {
  background: var(--surface); border-radius: 18px; width: 100%; max-width: 560px;
  max-height: 90vh; overflow: auto; box-shadow: var(--shadow-lg);
  animation: pop .18s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(.98); } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 22px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface);
}
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-close { background: none; border: 0; font-size: 22px; color: var(--text-mut); cursor: pointer; line-height: 1; }
.modal-body { padding: 22px; }
.modal-foot {
  display: flex; gap: 10px; justify-content: flex-end; padding: 16px 22px;
  border-top: 1px solid var(--border); position: sticky; bottom: 0; background: var(--surface);
}

/* ===== Forms ===== */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: var(--text-soft); }
.field .hint { font-size: 11.5px; color: var(--text-mut); }
.field input[type=text], .field input[type=number], .field textarea, .field select {
  border: 1px solid var(--border-strong); border-radius: 10px; padding: 10px 12px;
  font-size: 14px; font-family: inherit; color: var(--text); background: var(--surface);
  transition: border .15s, box-shadow .15s;
}
.field input:focus, .field textarea:focus, .field select:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
.field textarea { resize: vertical; min-height: 70px; }
.switch-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 0;
}
.switch {
  position: relative; width: 42px; height: 24px; flex-shrink: 0;
  background: #cbd2df; border-radius: 999px; cursor: pointer; transition: background .15s;
}
.switch.on { background: var(--primary); }
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  background: #fff; border-radius: 50%; transition: transform .15s; box-shadow: var(--shadow-sm);
}
.switch.on::after { transform: translateX(18px); }
.switch-label { font-size: 13.5px; font-weight: 500; }

/* ===== Toast ===== */
.toast-root { position: fixed; bottom: 22px; right: 22px; z-index: 120; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: #11152a; color: #fff; padding: 13px 18px; border-radius: 12px;
  font-size: 14px; box-shadow: var(--shadow-lg); display: flex; align-items: center; gap: 10px;
  animation: slideIn .2s ease; max-width: 340px;
}
.toast.success { background: #0f7a3d; }
.toast.error { background: #b91c1c; }
@keyframes slideIn { from { opacity: 0; transform: translateX(20px); } }

/* ===== States ===== */
.loader { display: grid; place-items: center; padding: 60px; color: var(--text-mut); }
.spinner {
  width: 30px; height: 30px; border: 3px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .7s linear infinite; margin-bottom: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.filter-row { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.chip-btn {
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-soft);
  border-radius: 999px; padding: 7px 15px; font-size: 13px; font-weight: 600; cursor: pointer;
  font-family: inherit; transition: all .15s;
}
.chip-btn:hover { background: var(--surface-2); }
.chip-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.prob-badge { margin-left: 8px; font-weight: 500; font-size: 12px; }
.aud-box {
  background: var(--primary-soft); color: var(--primary); border-radius: 10px;
  padding: 10px 12px; font-weight: 600; font-size: 13.5px; display: flex; align-items: center; gap: 7px;
}
.empty { text-align: center; padding: 50px 20px; color: var(--text-mut); }
.empty-ico { font-size: 40px; margin-bottom: 12px; display: block; color: var(--border-strong); }

.scrim { display: none; }

/* ===== Responsive ===== */
@media (max-width: 1080px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .segments { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 860px) {
  .sidebar {
    position: fixed; left: 0; top: 0; transform: translateX(-100%);
    transition: transform .25s ease; box-shadow: var(--shadow-lg);
  }
  .app.nav-open .sidebar { transform: translateX(0); }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(17,21,42,.5); z-index: 35; }
  .burger { display: block; }
  .content { padding: 18px; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
  .segments { grid-template-columns: 1fr; }
  .topbar { padding: 12px 16px; }
  #refreshBtn .lbl { display: none; }
}
