:root {
  --bg: #0b1220;
  --panel: #121a2b;
  --panel-2: #161f33;
  --border: #263351;
  --text: #e8edf7;
  --text-dim: #96a2ba;
  --accent: #4fd1ff;
  --green: #2ecc71;
  --amber: #f5a623;
  --red: #e74c3c;
  --grey: #9aa3ad;
  font-family: 'Segoe UI', system-ui, -apple-system, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); height: 100%; }
body { display: flex; flex-direction: column; min-height: 100vh; }

/* ---- Loading screen ---- */
.loading-screen {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px;
  transition: opacity .45s ease, visibility .45s ease;
}
.loading-screen.is-hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.loading-mark { position: relative; width: 84px; height: 84px; display: flex; align-items: center; justify-content: center; }
.loading-mark img {
  width: 52px; height: 52px; object-fit: contain;
  animation: loading-pulse 1.6s ease-in-out infinite;
  filter: drop-shadow(0 0 10px rgba(79,209,255,.45));
}
.loading-ring {
  position: absolute; inset: 0; border-radius: 50%;
  border: 2px solid transparent; border-top-color: var(--accent); border-right-color: var(--accent);
  animation: loading-spin 1s linear infinite;
}
.loading-text { color: var(--text-dim); font-size: 13px; letter-spacing: .2px; }
.loading-bar { width: 180px; height: 3px; border-radius: 999px; background: var(--panel-2); overflow: hidden; }
.loading-bar-fill {
  width: 40%; height: 100%; border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), #8fe6ff);
  animation: loading-bar 1.3s ease-in-out infinite;
}
@keyframes loading-pulse {
  0%, 100% { transform: scale(1); opacity: .9; }
  50% { transform: scale(1.12); opacity: 1; }
}
@keyframes loading-spin { to { transform: rotate(360deg); } }
@keyframes loading-bar {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(250%); }
}

.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 22px; border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #101a2e, #0d1524);
  position: sticky; top: 0; z-index: 20;
}
.topbar img.logo { width: 34px; height: 34px; object-fit: contain; filter: drop-shadow(0 0 6px rgba(79,209,255,.5)); }
.topbar h1 { font-size: 17px; margin: 0; font-weight: 600; letter-spacing: .2px; }
.topbar .subtitle { color: var(--text-dim); font-size: 12.5px; margin-left: 2px; }
.topbar .spacer { flex: 1; }
.data-pill {
  font-size: 11.5px; padding: 5px 10px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--text-dim); background: var(--panel-2);
}
.data-pill.live { color: #063; background: rgba(46,204,113,.15); border-color: rgba(46,204,113,.4); color: #6fe3a1; }

.filter-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 22px; border-bottom: 1px solid var(--border); background: #101a2e;
}
.filter-presets { display: flex; gap: 6px; flex-wrap: wrap; }
.filter-presets button {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text-dim);
  font-size: 11.5px; padding: 5px 12px; border-radius: 999px; cursor: pointer;
  transition: transform .12s ease, background .15s ease, border-color .15s ease, color .15s ease;
}
.filter-presets button:hover { border-color: var(--accent); color: var(--text); transform: translateY(-1px); }
.filter-presets button.active { background: var(--accent); border-color: var(--accent); color: #04202b; font-weight: 700; }
.filter-divider { width: 1px; height: 20px; background: var(--border); }
.filter-date { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--text-dim); }
.filter-date input[type="date"] {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 4px 8px; font-size: 12px; font-family: inherit;
  color-scheme: dark;
}
.filter-date input[type="date"]:focus { outline: none; border-color: var(--accent); }
.filter-summary { margin-left: auto; font-size: 11.5px; color: var(--text-dim); white-space: nowrap; }

.layout {
  flex: 1; display: grid; gap: 16px; padding: 16px 22px 28px;
  grid-template-columns: 1.15fr 1fr;
  grid-template-rows: auto 1fr 260px;
  grid-template-areas:
    "kpis      kpis"
    "matrix    globe"
    "matrix    attention";
}
@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto; grid-template-areas: "kpis" "globe" "attention" "matrix"; }
}

.kpis { grid-area: kpis; display: flex; gap: 12px; flex-wrap: wrap; }
.kpi {
  flex: 1; min-width: 150px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; position: relative;
  opacity: 0;
  animation-duration: .5s; animation-timing-function: cubic-bezier(.16,1,.3,1); animation-fill-mode: both;
  transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
}
/* Entrance animation only starts once app.js marks the page revealed (loading screen gone) —
   otherwise it would play out hidden behind the loading screen and never be seen. */
body.revealed .kpi { animation-name: card-in; }
.kpi:hover { transform: translateY(-3px); box-shadow: 0 10px 22px rgba(0,0,0,.35); border-color: var(--accent); }
.kpi-info {
  position: absolute; top: 10px; right: 10px; width: 15px; height: 15px; border-radius: 50%;
  border: 1px solid var(--border); color: var(--text-dim); font-size: 10px; font-style: italic;
  font-family: Georgia, 'Times New Roman', serif; line-height: 1;
  display: flex; align-items: center; justify-content: center; cursor: help;
  transition: color .15s ease, border-color .15s ease;
}
.kpi-info:hover { color: var(--accent); border-color: var(--accent); }
.kpi .num { font-size: 26px; font-weight: 700; }
.kpi .label { color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.kpi.green .num { color: var(--green); }
.kpi.amber .num { color: var(--amber); }
.kpi.red .num { color: var(--red); }
.kpi:nth-child(1) { animation-delay: 0ms; }
.kpi:nth-child(2) { animation-delay: 50ms; }
.kpi:nth-child(3) { animation-delay: 100ms; }
.kpi:nth-child(4) { animation-delay: 150ms; }
.kpi:nth-child(5) { animation-delay: 200ms; }
.kpi:nth-child(6) { animation-delay: 250ms; }

@keyframes card-in {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  display: flex; flex-direction: column; overflow: hidden;
  opacity: 0;
  animation-duration: .55s; animation-timing-function: cubic-bezier(.16,1,.3,1); animation-fill-mode: both;
}
body.revealed .panel { animation-name: card-in; }
.panel-header {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.panel-header h2 { font-size: 14px; margin: 0; font-weight: 600; }
.panel-header .hint { color: var(--text-dim); font-size: 11.5px; margin-left: auto; }

/* ---- Matrix / heatmap ---- */
#matrixPanel { grid-area: matrix; animation-delay: 80ms; }
.matrix-scroll { overflow: auto; max-height: 560px; }
table.matrix { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 12px; }
table.matrix thead th {
  position: sticky; top: 0; background: var(--panel-2); z-index: 2;
  padding: 8px 6px; font-weight: 500; color: var(--text-dim); text-align: center;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.matrix th.station-col, table.matrix td.station-col {
  position: sticky; left: 0; background: var(--panel-2); z-index: 3;
  text-align: left; padding: 8px 12px; border-right: 1px solid var(--border);
  min-width: 190px; cursor: pointer;
}
table.matrix td.station-col:hover { background: #1c2740; }
.station-name { font-weight: 600; font-size: 12.5px; }
.station-code { color: var(--text-dim); font-size: 10.5px; }
.rag-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 7px; }
table.matrix td.cell {
  width: 30px; height: 30px; text-align: center; padding: 0; border-bottom: 1px solid rgba(255,255,255,.03);
}
.cell-swatch {
  width: 22px; height: 22px; margin: auto; border-radius: 6px; cursor: pointer;
  transition: transform .12s ease;
}
.cell-swatch:hover { transform: scale(1.25); box-shadow: 0 0 0 2px rgba(255,255,255,.25); }
.matrix-legend { display: flex; gap: 16px; padding: 10px 16px; border-top: 1px solid var(--border); font-size: 11.5px; color: var(--text-dim); flex-wrap: wrap; }
.matrix-legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

/* ---- Globe ---- */
#globePanel { grid-area: globe; animation-delay: 160ms; }
.globe-stage { position: relative; flex: 1; min-height: 420px; background: radial-gradient(ellipse at 50% 40%, #16233d, #0a1120 70%); }
.globe-stage canvas { width: 100%; height: 100%; display: block; }
.globe-pin {
  position: absolute; top: 0; left: 0; transform: translate(-9999px,-9999px);
  background: none; border: none; padding: 0; cursor: pointer;
  display: flex; flex-direction: column; align-items: center;
  pointer-events: auto; --pin-color: var(--grey);
}
.globe-pin-anchor { position: relative; width: 0; height: 0; }
.globe-pin-halo {
  position: absolute; left: 0; top: 0; width: 9px; height: 9px; margin: -4.5px 0 0 -4.5px;
  border-radius: 50%; background: var(--pin-color);
  animation: pin-halo 2s ease-out infinite;
}
.globe-pin-dot {
  position: absolute; left: 0; top: -13px; width: 13px; height: 13px; margin-left: -6.5px;
  border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--pin-color); box-shadow: 0 1px 3px rgba(0,0,0,.6);
}
.globe-pin-label {
  font-size: 9.5px; color: var(--text-dim); background: rgba(10,16,28,.7);
  padding: 1px 5px; border-radius: 4px; white-space: nowrap; margin-top: 6px;
}
.globe-pin.is-hidden { opacity: 0; pointer-events: none; }
.globe-pin.is-active .globe-pin-label { color: #fff; background: rgba(79,209,255,.35); font-weight: 600; }
.globe-pin.is-active .globe-pin-dot {
  width: 16px; height: 16px; margin-left: -8px; top: -16px;
  box-shadow: 0 1px 4px rgba(0,0,0,.7), 0 0 0 2px rgba(255,255,255,.85);
  animation: pin-drop .45s ease;
}
.globe-pin.is-active .globe-pin-halo { animation: pin-halo 1.1s ease-out infinite; }
@keyframes pin-halo {
  0% { transform: scale(0.7); opacity: .65; }
  100% { transform: scale(2.6); opacity: 0; }
}
@keyframes pin-drop {
  0% { transform: translateY(-16px) rotate(-45deg) scale(0.3); opacity: 0; }
  60% { transform: translateY(3px) rotate(-45deg) scale(1.1); }
  100% { transform: translateY(0) rotate(-45deg) scale(1); }
}

.station-detail {
  padding: 14px 16px; border-top: 1px solid var(--border); font-size: 12.5px;
}
.station-detail .row { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.station-detail h3 { margin: 0 0 2px; font-size: 15px; }
.station-detail .rag-chip {
  font-size: 11px; padding: 2px 9px; border-radius: 999px; font-weight: 600;
}
.station-detail .stat-row { display: flex; gap: 18px; margin: 10px 0; color: var(--text-dim); font-size: 11.5px; }
.station-detail .stat-row b { color: var(--text); font-size: 14px; display: block; }
.station-detail .flags { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; max-height: 120px; overflow: auto; }
.flag-item { font-size: 11.5px; padding: 6px 8px; border-radius: 8px; background: rgba(231,76,60,.1); border: 1px solid rgba(231,76,60,.25); }
.flag-item .wk { color: var(--text-dim); margin-right: 6px; }
.empty-hint { color: var(--text-dim); font-size: 12px; padding: 20px; text-align: center; }

/* ---- Attention ranking ---- */
#attentionPanel { grid-area: attention; animation-delay: 240ms; }

.tooltip {
  position: fixed; z-index: 50; background: #0d1526; border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; font-size: 12px; max-width: 280px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4); pointer-events: none; opacity: 0; transition: opacity .1s;
}
.tooltip.show { opacity: 1; }
.tooltip .t-status { font-weight: 600; margin-bottom: 3px; }
.tooltip .t-notes { color: var(--text-dim); }

.attention-list { display: flex; flex-direction: column; gap: 8px; padding: 12px 16px; overflow: auto; }
.attention-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px;
  background: var(--panel-2); border: 1px solid var(--border); cursor: pointer;
  transition: transform .15s ease, border-color .15s ease, background .15s ease;
}
.attention-item:hover { border-color: var(--accent); background: #1c2740; transform: translateX(3px); }
.attention-item .rag-dot { flex-shrink: 0; }
.attention-item .meta { flex: 1; }
.attention-item .meta .name { font-weight: 600; font-size: 12.5px; }
.attention-item .meta .why { color: var(--text-dim); font-size: 11px; }
.attention-item .score { font-weight: 700; font-size: 13px; }
