:root {
  --bg: #050811;
  --panel: #0c1324;
  --panel-alt: #0f172a;
  --border: #1e293b;
  --accent: #38bdf8;
  --accent-2: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --text: #e2e8f0;
  --muted: #94a3b8;
}

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

html, body {
  overflow-x: hidden;
  max-width: 100%;
}

body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: radial-gradient(circle at 20% 0%, #0f172a 0%, var(--bg) 70%);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

header.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.4rem;
  background: rgba(12, 19, 36, 0.94);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(10px);
}

.brand { display: flex; align-items: center; gap: 0.85rem; }
.brand img { width: 44px; height: 44px; border-radius: 10px; object-fit: cover; border: 1px solid var(--border); }
.brand h1 { font-size: 1.12rem; font-weight: 700; }
.brand p { font-size: 0.76rem; color: var(--muted); }

.nav-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }

.btn {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-alt);
  color: var(--text);
  padding: 0.42rem 0.8rem;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.15s ease;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); color: #020617; border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); color: #020617; }
.btn.danger { background: rgba(239, 68, 68, 0.14); border-color: rgba(239, 68, 68, 0.45); color: #fca5a5; }
.btn.warn { background: rgba(245, 158, 11, 0.14); border-color: rgba(245, 158, 11, 0.45); color: #fcd34d; }
.btn.active { background: rgba(56, 189, 248, 0.18); border-color: var(--accent); color: var(--accent); }

main.layout {
  display: grid;
  grid-template-columns: 325px minmax(0, 1fr) 355px;
  gap: 1rem;
  padding: 1rem 1.4rem 2rem;
  max-width: 1640px;
  margin: 0 auto;
}

@media (max-width: 1200px) {
  main.layout { grid-template-columns: minmax(0, 1fr); }
}

.panel {
  background: rgba(12, 19, 36, 0.88);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-width: 0;
  max-width: 100%;
}

.panel h2 {
  font-size: 0.88rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.4rem;
}

.opt-group { display: flex; flex-direction: column; gap: 0.42rem; }
.opt-label { font-size: 0.78rem; font-weight: 600; color: var(--muted); }

.opt-card {
  text-align: left;
  padding: 0.6rem 0.72rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.6);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s ease;
}
.opt-card:hover { border-color: #475569; }
.opt-card.selected { border-color: var(--accent); background: rgba(56, 189, 248, 0.12); }
.opt-card .title { font-size: 0.83rem; font-weight: 600; display: block; }
.opt-card .sub { font-size: 0.72rem; color: var(--muted); display: block; margin-top: 0.12rem; }

.viewport-wrap {
  position: relative;
  height: 410px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: radial-gradient(circle at center, #0b132b 0%, #02040a 100%);
  cursor: grab;
}
.viewport-wrap:active { cursor: grabbing; }
#cube-canvas { width: 100%; height: 100%; display: block; touch-action: none; }

.hud-overlay {
  position: absolute;
  top: 0.7rem;
  left: 0.7rem;
  right: 0.7rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  pointer-events: none;
  gap: 0.45rem;
}
.hud-bottom {
  position: absolute;
  bottom: 0.65rem;
  left: 0.7rem;
  right: 0.7rem;
  display: flex;
  justify-content: space-between;
  pointer-events: none;
}
.hud-pill {
  background: rgba(2, 6, 23, 0.8);
  border: 1px solid var(--border);
  padding: 0.28rem 0.62rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
}
.kpi {
  background: rgba(2, 6, 23, 0.65);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.58rem 0.7rem;
  min-width: 0;
}
.kpi .k-label { font-size: 0.7rem; color: var(--muted); }
.kpi .k-val { font-size: 1.05rem; font-weight: 700; margin-top: 0.12rem; font-family: ui-monospace, monospace; }
.k-good { color: var(--accent-2); }
.k-warn { color: var(--warn); }
.k-bad { color: var(--danger); }

.hex-box {
  background: #020617;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.74rem;
  word-break: break-all;
  color: #7dd3fc;
}
.seu-alert {
  margin-top: 0.4rem;
  font-size: 0.75rem;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.75);
}

.badges-wrap { display: flex; flex-wrap: wrap; gap: 0.38rem; }
.badge-chip {
  font-size: 0.71rem;
  font-weight: 600;
  padding: 0.22rem 0.55rem;
  border-radius: 999px;
  background: rgba(34, 197, 94, 0.14);
  color: #86efac;
  border: 1px solid rgba(34, 197, 94, 0.35);
}

.sw-box {
  background: rgba(2, 6, 23, 0.6);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  font-size: 0.77rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.charts-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem;
}
@media (max-width: 720px) { .charts-row { grid-template-columns: minmax(0, 1fr); } }

.chart-card {
  background: rgba(2, 6, 23, 0.6);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.68rem;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.chart-card h3 { font-size: 0.76rem; color: var(--muted); margin-bottom: 0.4rem; }
canvas.mini-chart { width: 100%; max-width: 100%; height: 155px; display: block; }

.heritage-list { list-style: none; display: flex; flex-direction: column; gap: 0.4rem; font-size: 0.76rem; color: var(--muted); }
.heritage-list a { color: var(--accent); text-decoration: none; }
.heritage-list a:hover { text-decoration: underline; }

.btn.success { border-color: rgba(34, 197, 94, 0.55); background: linear-gradient(135deg, #22c55e, #0ea5e9); color: #020617; font-weight: 800; }
.btn.success:hover { filter: brightness(1.08); }

.game-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.7rem;
  padding: 0.6rem 1.25rem;
  background: rgba(10, 17, 34, 0.9);
  border-bottom: 1px solid var(--border);
}
.scenario-tabs, .cml-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.42rem; }
.tab-pill {
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.25);
  color: var(--muted);
  padding: 0.34rem 0.68rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}
.tab-pill.active { border-color: var(--accent); color: #020617; background: var(--accent); }
.cml-pill {
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.26rem 0.56rem;
  border-radius: 6px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  color: var(--muted);
  background: rgba(2, 6, 23, 0.6);
}
.cml-pill.done { border-color: rgba(34, 197, 94, 0.5); color: #86efac; background: rgba(34, 197, 94, 0.14); }

.coop-dock {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.65rem;
  padding: 0.65rem 1.25rem;
  background: rgba(14, 165, 233, 0.06);
  border-bottom: 1px solid var(--border);
}
@media (max-width: 980px) { .coop-dock { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 580px) { .coop-dock { grid-template-columns: 1fr; } }
.role-card {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.32rem;
}
.role-card.signed { border-color: rgba(34, 197, 94, 0.55); background: rgba(34, 197, 94, 0.08); }
.role-card .r-head { display: flex; justify-content: space-between; align-items: center; font-size: 0.76rem; font-weight: 700; color: var(--accent); }
.role-card .r-desc { font-size: 0.71rem; color: var(--muted); }
.role-card input {
  background: #020617;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 0.28rem 0.48rem;
  font-size: 0.74rem;
}

.viz-switch { display: flex; gap: 0.45rem; flex-wrap: wrap; align-items: center; justify-content: space-between; }
.res-bars {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.5rem;
}
@media (max-width: 900px) { .res-bars { grid-template-columns: repeat(2, 1fr); } }
.res-meter {
  background: rgba(2, 6, 23, 0.68);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.55rem;
}
.res-meter .rm-top { display: flex; justify-content: space-between; font-size: 0.69rem; color: var(--muted); }
.res-meter .rm-val { font-size: 0.78rem; font-weight: 700; font-family: ui-monospace, monospace; margin: 0.16rem 0; }
.rm-track { width: 100%; height: 6px; background: #1e293b; border-radius: 999px; overflow: hidden; }
.rm-fill { height: 100%; border-radius: 999px; transition: width 0.25s ease; background: var(--accent-2); }
.rm-fill.warn { background: var(--warn); }
.rm-fill.bad { background: var(--danger); }

.score-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(14,165,233,0.16), rgba(34,197,94,0.12));
  border: 1px solid var(--border);
}
.gates-list { display: flex; flex-direction: column; gap: 0.34rem; }
.gate-row {
  display: flex;
  flex-direction: column;
  padding: 0.38rem 0.55rem;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.2);
  background: rgba(2, 6, 23, 0.65);
  font-size: 0.74rem;
}
.gate-row.pass { border-color: rgba(34, 197, 94, 0.38); }
.gate-row.fail { border-color: rgba(239, 68, 68, 0.55); background: rgba(239, 68, 68, 0.08); }
.gate-row .g-head { display: flex; justify-content: space-between; font-weight: 700; }
.gate-row.pass .g-head { color: #86efac; }
.gate-row.fail .g-head { color: #fca5a5; }
.gate-row .g-why { font-size: 0.7rem; color: var(--muted); margin-top: 0.12rem; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.82);
  backdrop-filter: blur(6px);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1.2rem;
}
.modal-overlay.open { display: flex; }
.modal-card {
  width: min(860px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: #0b1326;
  border: 1px solid var(--accent);
  border-radius: 16px;
  padding: 1.35rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 0.8rem; border-bottom: 1px solid var(--border); padding-bottom: 0.65rem; }
.timeline-list { display: flex; flex-direction: column; gap: 0.45rem; }
.tl-step {
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: rgba(15, 23, 42, 0.85);
  font-size: 0.82rem;
}
.tl-step.ok { border-color: rgba(34, 197, 94, 0.55); color: #bbf7d0; }
.tl-step.bad { border-color: rgba(239, 68, 68, 0.6); color: #fecaca; }
#modal-lb-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
.lb-table { width: 100%; border-collapse: collapse; font-size: 0.78rem; }
.lb-table th, .lb-table td { border: 1px solid rgba(148, 163, 184, 0.22); padding: 0.42rem 0.6rem; text-align: left; white-space: nowrap; }
.lb-table th { background: rgba(56, 189, 248, 0.12); color: var(--accent); }
.hidden { display: none !important; }

/* Mobile & Tablet Responsive Ergonomics */
.opt-group, .opt-card, .res-meter, .sw-box, .heritage-list li {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

@media (max-width: 768px) {
  header.topbar {
    position: static;
    padding: 0.7rem 0.85rem;
    gap: 0.65rem;
  }
  .brand img { width: 38px; height: 38px; }
  .brand h1 { font-size: 0.98rem; }
  .brand p { font-size: 0.72rem; }
  .nav-actions { gap: 0.4rem; width: 100%; }
  .btn, .tab-pill { min-height: 38px; display: inline-flex; align-items: center; justify-content: center; padding: 0.38rem 0.68rem; font-size: 0.76rem; }
  .game-bar { padding: 0.55rem 0.8rem; gap: 0.5rem; }
  .coop-dock { padding: 0.55rem 0.8rem; grid-template-columns: minmax(0, 1fr); }
  main.layout { padding: 0.65rem 0.65rem 1.4rem; gap: 0.7rem; }
  .panel { padding: 0.78rem; gap: 0.72rem; }
  .viewport-wrap { height: 295px; }
  .hud-pill { font-size: 0.66rem; padding: 0.22rem 0.48rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .hud-bottom { flex-wrap: wrap; gap: 0.3rem; }
  .res-bars { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.42rem; }
  .modal-overlay { padding: 0.6rem; }
  .modal-card { padding: 0.95rem; max-height: 92vh; }
}

@media (max-width: 420px) {
  .kpi .k-val { font-size: 0.92rem; }
  .score-banner { flex-direction: column; align-items: flex-start; }
  .score-banner button { width: 100%; }
}
