/* ===== Design tokens ===== */
:root {
  --bg: #0f1420;
  --bg-soft: #161d2e;
  --surface: #1c2438;
  --surface-2: #222c44;
  --border: #2c3752;
  --text: #e8edf7;
  --text-dim: #9aa8c4;
  --primary: #e4322b;      /* China red */
  --primary-600: #c22821;
  --accent: #ffcf3f;       /* gold */
  --green: #34d399;
  --green-soft: rgba(52, 211, 153, .14);
  --red: #f87171;
  --red-soft: rgba(248, 113, 113, .14);
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --font: "Segoe UI", Tahoma, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

/* `hidden` attribute must always win over layout display rules below */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: radial-gradient(1200px 600px at 100% -10%, #1b2338 0%, var(--bg) 55%);
  color: var(--text);
  font-family: var(--font);
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
}

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px clamp(14px, 4vw, 40px);
  background: rgba(15, 20, 32, .82);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  font-size: 26px;
  background: linear-gradient(135deg, var(--primary), var(--primary-600));
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(228, 50, 43, .4);
}
.brand-title { margin: 0; font-size: 19px; letter-spacing: .3px; }
.brand-sub { margin: 2px 0 0; font-size: 12.5px; color: var(--text-dim); }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ===== Buttons ===== */
.btn {
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: transform .06s ease, background .18s ease, border-color .18s ease;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-600); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.plus { font-size: 18px; line-height: 1; }
.icon-btn {
  background: transparent; border: none; color: var(--text-dim);
  font-size: 18px; cursor: pointer; padding: 6px; border-radius: 8px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ===== Layout ===== */
.container { padding: clamp(16px, 4vw, 34px); max-width: 1400px; margin: 0 auto; }

/* ===== Stat cards ===== */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.stat {
  background: linear-gradient(160deg, var(--surface) 0%, var(--bg-soft) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  position: relative;
  overflow: hidden;
}
.stat::before {
  content: "";
  position: absolute; inset-inline-start: 0; top: 0; bottom: 0;
  width: 4px; background: var(--accent);
}
.stat.green::before { background: var(--green); }
.stat.red::before { background: var(--red); }
.stat.blue::before { background: #60a5fa; }
.stat-label { font-size: 12.5px; color: var(--text-dim); margin: 0 0 8px; }
.stat-value { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: .3px; }
.stat-sub { font-size: 12px; color: var(--text-dim); margin: 5px 0 0; }

/* ===== Toolbar ===== */
.toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 14px; flex-wrap: wrap;
}
.search-wrap {
  position: relative; flex: 1 1 240px; max-width: 380px;
}
.search-wrap input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 38px 10px 14px;
  font-family: inherit; font-size: 14px;
}
html[dir="ltr"] .search-wrap input { padding: 10px 14px 10px 38px; }
.search-wrap input:focus { outline: none; border-color: var(--primary); }
.search-icon {
  position: absolute; inset-inline-end: 12px; top: 50%; transform: translateY(-50%);
  opacity: .6; font-size: 14px; pointer-events: none;
}
.filters { display: flex; gap: 7px; flex-wrap: wrap; }
.chip {
  background: var(--surface); color: var(--text-dim);
  border: 1px solid var(--border); border-radius: 20px;
  padding: 8px 15px; font-family: inherit; font-size: 13px; cursor: pointer;
  transition: all .15s ease;
}
.chip:hover { color: var(--text); }
.chip-active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ===== Table ===== */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
table.deals { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.deals thead th {
  background: var(--bg-soft);
  color: var(--text-dim);
  font-weight: 600;
  text-align: start;
  padding: 13px 12px;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
}
.deals th.num, .deals td.num { text-align: end; font-variant-numeric: tabular-nums; }
.deals tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.deals tbody tr:last-child td { border-bottom: none; }
.deals tbody tr:hover { background: var(--surface-2); }
.cell-name { font-weight: 600; }
.cell-car { color: var(--text-dim); }
.pos { color: var(--green); font-weight: 600; }
.neg { color: var(--red); font-weight: 600; }
.badge {
  display: inline-block; padding: 3px 9px; border-radius: 20px;
  font-size: 11.5px; font-weight: 600;
}
.badge-paid { background: var(--green-soft); color: var(--green); }
.badge-due { background: var(--red-soft); color: var(--red); }
.row-actions { display: flex; gap: 4px; }

/* ===== Empty ===== */
.empty { text-align: center; padding: 50px 20px; color: var(--text-dim); }
.empty-emoji { font-size: 40px; display: block; margin-bottom: 10px; }

/* ===== Modal ===== */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(6, 9, 16, .66);
  backdrop-filter: blur(3px);
  display: grid; place-items: center;
  padding: 16px;
}
.modal {
  width: min(680px, 100%);
  max-height: 92vh; overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 20px; }
.grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px;
}
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.field input {
  background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--text); border-radius: 9px; padding: 10px 12px;
  font-family: inherit; font-size: 14.5px;
}
.field input:focus { outline: none; border-color: var(--primary); }
.calc {
  margin-top: 16px; padding: 14px 16px;
  background: var(--bg-soft); border: 1px solid var(--border); border-radius: 10px;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px 18px;
  font-size: 13px;
}
.calc-item { display: flex; justify-content: space-between; gap: 10px; }
.calc-item span { color: var(--text-dim); }
.calc-item b { font-variant-numeric: tabular-nums; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ===== Responsive: table → cards ===== */
@media (max-width: 860px) {
  .deals thead { display: none; }
  .deals, .deals tbody, .deals tr, .deals td { display: block; width: 100%; }
  .deals tbody tr {
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 12px;
    padding: 6px 4px;
  }
  .deals tbody tr:hover { background: var(--bg-soft); }
  .deals tbody td {
    display: flex; align-items: center; justify-content: space-between;
    gap: 14px; padding: 9px 14px; border-bottom: 1px dashed var(--border);
    text-align: end;
  }
  .deals tbody td:last-child { border-bottom: none; }
  .deals tbody td::before {
    content: attr(data-label);
    color: var(--text-dim); font-size: 12.5px; font-weight: 600;
    text-align: start; flex: 0 0 auto;
  }
  .deals td.num { text-align: end; }
  .row-actions { justify-content: flex-end; }
  .table-wrap { background: transparent; border: none; }
  .grid { grid-template-columns: 1fr; }
}

@media (max-width: 520px) {
  .brand-sub { display: none; }
  .topbar-actions .btn span:not(.plus) { font-size: 13px; }
}

/* =========================================================
   Additions: tabs, status, lock, debts, reports, sorting
   ========================================================= */

/* ---- Tabs ---- */
.tabs {
  display: flex; gap: 6px; margin-bottom: 18px;
  border-bottom: 1px solid var(--border);
}
.tab {
  background: transparent; border: none; color: var(--text-dim);
  font-family: inherit; font-size: 14.5px; font-weight: 600;
  padding: 11px 18px; cursor: pointer; position: relative;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab-active { color: var(--text); border-bottom-color: var(--primary); }

/* ---- Sortable headers ---- */
.deals thead th[data-sort] { cursor: pointer; user-select: none; }
.deals thead th[data-sort]:hover { color: var(--text); }
.deals thead th .sort-ind { opacity: .9; font-size: 11px; margin-inline-start: 3px; }

/* ---- Status pill ---- */
.status-pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 20px; font-size: 11.5px; font-weight: 600;
  white-space: nowrap;
}
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.st-ordered   { color: #b9b6ad; background: rgba(185,182,173,.14); }
.st-shipped   { color: #fab219; background: rgba(250,178,25,.14); }
.st-arrived   { color: #3987e5; background: rgba(57,135,229,.15); }
.st-delivered { color: #0ca30c; background: rgba(12,163,12,.15); }
.cell-dates { display: block; color: var(--text-dim); font-size: 11px; margin-top: 4px; }

/* ---- Deal form select ---- */
.field select {
  background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--text); border-radius: 9px; padding: 10px 12px;
  font-family: inherit; font-size: 14.5px;
}
.field select:focus { outline: none; border-color: var(--primary); }

/* ---- Debts banner ---- */
.debt-banner {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  background: linear-gradient(160deg, rgba(208,59,59,.16), var(--bg-soft));
  border: 1px solid var(--border); border-inline-start: 4px solid var(--red);
  border-radius: var(--radius); padding: 16px 20px; margin-bottom: 16px;
}
.debt-banner .db-value { font-size: 24px; font-weight: 700; color: var(--red); }
.debt-banner .db-label { font-size: 13px; color: var(--text-dim); }

/* ---- Reports ---- */
.reports-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px;
}
.report-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 20px;
}
.report-card h3 { margin: 0 0 16px; font-size: 15px; }
.report-note { color: var(--text-dim); font-size: 13px; padding: 10px 0; }

/* Horizontal bars */
.bars { display: flex; flex-direction: column; gap: 11px; }
.bar-row { display: grid; grid-template-columns: 120px 1fr auto; align-items: center; gap: 10px; }
.bar-label { font-size: 12.5px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: var(--bg-soft); border-radius: 5px; height: 16px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 5px; min-width: 2px; }
.bar-val { font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Paid vs remaining progress */
.progress-wrap { margin-top: 6px; }
.progress-bar {
  display: flex; height: 26px; border-radius: 7px; overflow: hidden;
  background: var(--bg-soft); gap: 2px;
}
.progress-bar span { display: block; height: 100%; }
.progress-legend { display: flex; justify-content: space-between; margin-top: 12px; gap: 12px; flex-wrap: wrap; }
.legend-item { display: flex; align-items: center; gap: 7px; font-size: 13px; }
.legend-dot { width: 11px; height: 11px; border-radius: 3px; }

/* ---- DB connection badge ---- */
.db-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 600; padding: 4px 9px;
  border-radius: 999px; border: 1px solid var(--border);
  white-space: nowrap; user-select: none;
}
.db-badge.db-on  { color: var(--green); border-color: color-mix(in srgb, var(--green) 45%, var(--border)); }
.db-badge.db-off { color: var(--text-dim); }

/* ---- Login screen ---- */
.lock-screen {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; padding: 20px;
  overflow: hidden;
  background:
    radial-gradient(1100px 620px at 12% -12%, rgba(228, 50, 43, .22), transparent 60%),
    radial-gradient(900px 520px at 105% 112%, rgba(255, 207, 63, .12), transparent 55%),
    linear-gradient(160deg, #0f1730 0%, var(--bg) 62%);
}
.lock-bg { position: absolute; inset: 0; pointer-events: none; }
.lock-orb { position: absolute; border-radius: 50%; filter: blur(64px); }
.orb1 { width: 320px; height: 320px; background: var(--primary); top: -90px; inset-inline-start: -70px; opacity: .5; animation: float1 11s ease-in-out infinite; }
.orb2 { width: 280px; height: 280px; background: var(--accent); bottom: -80px; inset-inline-end: -50px; opacity: .3; animation: float2 13s ease-in-out infinite; }
.orb3 { width: 220px; height: 220px; background: #3987e5; top: 42%; inset-inline-start: 58%; opacity: .22; animation: float1 9s ease-in-out infinite; }
@keyframes float1 { 0%,100% { transform: translateY(0); } 50% { transform: translateY(26px); } }
@keyframes float2 { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-30px); } }

.lock-box {
  position: relative; z-index: 1;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: blur(16px) saturate(120%);
  -webkit-backdrop-filter: blur(16px) saturate(120%);
  border: 1px solid color-mix(in srgb, var(--border) 78%, #ffffff 7%);
  border-radius: 24px; padding: 40px 32px 26px; width: min(384px, 100%);
  text-align: center; box-shadow: 0 26px 64px rgba(0, 0, 0, .55);
  animation: lockIn .5s cubic-bezier(.2, .8, .2, 1) both;
}
@keyframes lockIn { from { opacity: 0; transform: translateY(16px) scale(.97); } to { opacity: 1; transform: none; } }

.lock-badge {
  width: 76px; height: 76px; margin: 0 auto 16px; border-radius: 22px;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--primary), var(--primary-600));
  box-shadow: 0 12px 28px rgba(228, 50, 43, .45), inset 0 1px 0 rgba(255, 255, 255, .28);
}
.lock-car { font-size: 40px; line-height: 1; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .3)); }
.lock-brand { margin: 2px 0 3px; font-size: 25px; font-weight: 800; letter-spacing: .3px; }
.lock-sub { margin: 0 0 22px; color: var(--text-dim); font-size: 13.5px; }

.lock-field { position: relative; display: flex; align-items: center; margin-bottom: 14px; }
.lock-field .lock-ic {
  position: absolute; inset-inline-start: 15px; font-size: 15px; opacity: .65; pointer-events: none;
}
.lock-box input {
  width: 100%; font-size: 16px; text-align: center;
  background: var(--bg-soft); border: 1px solid var(--border); color: var(--text);
  border-radius: 13px; padding: 14px 46px;
  transition: border-color .15s, box-shadow .15s;
}
.lock-box input::placeholder { color: var(--text-dim); }
.lock-box input:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(228, 50, 43, .16);
}
.lock-eye {
  position: absolute; inset-inline-end: 8px; background: none; border: 0; color: var(--text-dim);
  cursor: pointer; font-size: 16px; opacity: .7; padding: 7px; border-radius: 9px; line-height: 1;
}
.lock-eye:hover { opacity: 1; background: var(--surface-2); }
.lock-submit { width: 100%; justify-content: center; padding: 13px; font-size: 15px; font-weight: 700; margin-top: 2px; }
.lock-foot { margin: 18px 0 0; font-size: 11.5px; color: var(--text-dim); opacity: .85; }
.lock-error { color: var(--red); font-size: 13px; margin: 0 0 12px; }
.lock-shake { animation: shake .35s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

@media (max-width: 860px) {
  .bar-row { grid-template-columns: 100px 1fr auto; }
  .tabs { overflow-x: auto; }
}
