:root {
  --bg: #0f1419;
  --surface: #1a212b;
  --surface-2: #222b38;
  --border: #2e3947;
  --text: #e8edf2;
  --text-dim: #9aa7b5;
  --accent: #4da3ff;
  --green: #3ecf8e;
  --red: #ff6b6b;
  --amber: #ffb454;
  --purple: #b48cff;
  --radius: 10px;
  font-size: 15px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

.hidden { display: none; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { height: 30px; width: auto; display: block; }
.brand-sub { color: var(--text-dim); font-size: .95rem; }
.meta-line { padding: 6px 24px; color: var(--text-dim); font-size: .8rem; }

/* ---------- tabs ---------- */
.tabs {
  display: flex; gap: 4px; padding: 0 24px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  overflow-x: auto;
}
.tabs button {
  background: transparent; border: none; color: var(--text-dim);
  padding: 12px 16px; cursor: pointer; font-size: .95rem;
  border-bottom: 2px solid transparent; white-space: nowrap; font-family: inherit;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tab { display: none; }
.tab.active { display: block; }

main { flex: 1; padding: 20px 24px; max-width: 1500px; width: 100%; margin: 0 auto; }

/* ---------- inputs ---------- */
select, .search, input[type="text"], input[type="password"] {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 10px; font-size: .9rem; color-scheme: dark;
  font-family: inherit;
}
.search { width: 100%; max-width: 280px; }

/* ---------- cards & grids ---------- */
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; margin-bottom: 14px; }
@media (max-width: 1000px) { .grid-3 { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
}
.card h3 { font-size: 1rem; margin-bottom: 12px; }
.card .hint { color: var(--text-dim); font-weight: 400; font-size: .8rem; }

.hint { color: var(--text-dim); font-size: .85rem; }

.filter-row { display: flex; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }

/* ---------- column filter popup ---------- */
th .col-filter-btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 1px solid transparent; border-radius: 4px;
  color: var(--text-dim); cursor: pointer; font-size: .68rem;
  padding: 0 3px; margin-left: 3px; height: 14px; line-height: 1; vertical-align: middle;
  font-family: inherit;
}
th .col-filter-btn:hover { border-color: var(--border); color: var(--text); }
th .col-filter-btn.col-filter-active { color: var(--accent); border-color: var(--accent); }

.col-filter-popup {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 8px 24px #00000066;
  min-width: 210px; max-width: 280px; font-size: .82rem; z-index: 2000; overflow: hidden;
}
.col-filter-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 10px; background: var(--surface-2);
  border-bottom: 1px solid var(--border); font-weight: 600; font-size: .75rem;
  color: var(--text-dim); text-transform: uppercase; letter-spacing: .4px;
}
.col-filter-search {
  display: block; width: 100%; border: none; border-bottom: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); padding: 6px 10px; font-size: .82rem; outline: none;
}
.col-filter-items { max-height: 200px; overflow-y: auto; padding: 4px 0; }
.col-filter-item {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; cursor: pointer; color: var(--text);
}
.col-filter-item:hover { background: var(--surface-2); }
.col-filter-item input { cursor: pointer; accent-color: var(--accent); flex-shrink: 0; }
.col-filter-cnt { color: var(--text-dim); font-size: .72rem; margin-left: auto; }
.col-filter-actions {
  display: flex; gap: 6px; padding: 7px 10px;
  border-top: 1px solid var(--border); background: var(--surface-2);
}
.col-filter-apply {
  background: var(--accent); color: #06121f; border: none; border-radius: 6px;
  padding: 4px 10px; font-size: .78rem; font-weight: 600; cursor: pointer; font-family: inherit;
}
.col-filter-clear, .col-filter-btn-sm {
  background: transparent; color: var(--text-dim); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 10px; font-size: .78rem; cursor: pointer; font-family: inherit;
}
.col-filter-clear:hover, .col-filter-btn-sm:hover { color: var(--text); border-color: var(--text-dim); }
.col-filter-btn-sm { padding: 2px 6px; font-size: .72rem; }

/* ---------- buttons ---------- */
.btn, .btn-inline {
  display: inline-block; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 14px; font-size: .85rem; color: var(--accent);
  cursor: pointer; font-family: inherit; text-decoration: none;
}
.btn:hover, .btn-inline:hover { border-color: var(--accent); }

/* ---------- table ---------- */
.table-wrap { overflow: auto; max-height: 320px; border: 1px solid var(--border); border-radius: var(--radius); }
.table-wrap.tall { max-height: 480px; }
table { width: 100%; border-collapse: collapse; font-size: .87rem; }
th {
  text-align: left; color: var(--text-dim); font-weight: 600;
  padding: 8px 10px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface);
  white-space: nowrap;
}
td { padding: 7px 10px; border-bottom: 1px solid #232c38; vertical-align: top; }
tr:hover td { background: #ffffff08; }
td:nth-child(5), td:nth-child(6) { white-space: normal; }

/* ---------- sort controls ---------- */
.table-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; flex-wrap: wrap; gap: 10px; }
.sort-controls { display: flex; align-items: center; gap: 10px; }
.asc-toggle { display: flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--text-dim); }
.asc-toggle input { accent-color: var(--accent); }

/* ---------- preview panel ---------- */
.preview-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
#previewPanel select { width: 100%; max-width: 480px; }
.download-link { white-space: nowrap; }
.preview-image-wrap { text-align: center; }
#previewImage {
  max-width: 70%; border: 1px solid var(--border); border-radius: var(--radius);
  background: #ffffff;
}

/* ---------- legend ---------- */
.legend-lookup input { margin-bottom: 12px; }
.legend-lookup table { max-width: 400px; }

footer {
  padding: 14px 24px; color: var(--text-dim); font-size: .8rem;
  border-top: 1px solid var(--border); text-align: center;
}

/* ---------- password gate ---------- */
#passwordGate {
  position: fixed; inset: 0; z-index: 1200;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
.gate-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 40px 48px; text-align: center;
  width: min(380px, 90vw); box-shadow: 0 20px 60px #000a;
}
.gate-logo { height: 56px; width: auto; margin-bottom: 8px; }
.gate-sub { color: var(--text-dim); font-size: .95rem; margin-bottom: 32px; }
#gateForm input[type="password"] {
  width: 100%; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 11px 14px; font-size: 1rem; margin-bottom: 12px; outline: none;
}
#gateInput:focus { border-color: var(--accent); }
.gate-box button[type=submit] {
  width: 100%; background: var(--accent); color: #06121f;
  border: none; border-radius: 8px; padding: 11px;
  font-size: 1rem; font-weight: 600; cursor: pointer; font-family: inherit;
}
.gate-box button[type=submit]:hover { filter: brightness(1.1); }
#gateError { color: var(--red); font-size: .85rem; margin-top: 10px; min-height: 1.2em; }
@keyframes gate-shake {
  0%,100% { transform: translateX(0); }
  20%,60% { transform: translateX(-8px); }
  40%,80% { transform: translateX(8px); }
}
.gate-shake { animation: gate-shake .4s ease; }

/* ---------- light mode (follows system preference) ---------- */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #edf0f4;
    --border: #d5dae2;
    --text: #1a212b;
    --text-dim: #637080;
    --accent: #1a72cc;
    --green: #18905e;
    --red: #cc2e2e;
    --amber: #b86c00;
    --purple: #6830c0;
  }

  #passwordGate { background: var(--bg); }
  .gate-box { box-shadow: 0 8px 32px #0002; }
  th { background: var(--surface); }
  tr:hover td { background: #00000010; }
}

@media (max-width: 800px) {
  .filter-row { flex-direction: column; align-items: stretch; }
  .search { max-width: none; }
}
