/* ============ STAR AGENTS - STYLE ============ */
:root {
  --bg: #05070d;
  --panel: #0b101c;
  --panel2: #101828;
  --line: #1c2a44;
  --accent: #ff6b00;
  --accent2: #ffb25e;
  --cyan: #37d5ff;
  --green: #4ade80;
  --red: #ff4444;
  --text: #dbe4f3;
  --dim: #7a8aa8;
  --gold: #ffd166;
}
* { margin:0; padding:0; box-sizing:border-box; }
html, body { height:100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  overflow: hidden;
}
.screen { display:none; width:100%; height:100%; }
.screen.active { display:flex; flex-direction:column; }
.hidden { display:none !important; }
button { font-family: inherit; cursor:pointer; }
.btn {
  background: var(--panel2); color: var(--text); border:1px solid var(--line);
  padding:8px 16px; border-radius:6px; font-size:13px; transition: all .15s;
}
.btn:hover { border-color: var(--accent); color: var(--accent2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color:#fff; font-weight:700; }
.btn-primary:hover { background: var(--accent2); color:#000; }
.btn-small { padding:4px 10px; font-size:12px; }
.btn:disabled { opacity:.4; cursor:not-allowed; }
.btn-endturn {
  background: var(--accent); border:none; color:#fff; font-weight:800;
  font-size:15px; padding:10px 22px; letter-spacing:1px; border-radius:8px;
  box-shadow: 0 0 18px rgba(255,107,0,.45);
}
.btn-endturn:hover { background: var(--accent2); color:#000; }
.btn-endturn:disabled { background:#3a2a18; color:#8a6a4a; box-shadow:none; }

/* ===== RACE SELECT ===== */
#screen-race {
  background: radial-gradient(ellipse at 50% 30%, #0d1626 0%, #05070d 70%);
  align-items: center; justify-content: flex-start; overflow-y: auto;
  padding: 2vh 0;
}
.race-hero { text-align: center; padding: 2vh 20px 1vh; }
.title-glow {
  font-size: clamp(30px, 5vw, 64px); font-weight: 900; letter-spacing: clamp(4px, 1vw, 12px);
  color: #fff; text-shadow: 0 0 24px rgba(255,107,0,.8), 0 0 60px rgba(255,107,0,.4);
}
.tagline { color: var(--dim); font-size: clamp(12px, 1.2vw, 15px); margin-top: 8px; line-height: 1.6; }
.race-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(min(230px, 88vw), 1fr));
  gap: clamp(8px, 1vw, 14px); max-width: 1200px; width: 94%;
  padding: 1.5vh 0; margin: 0 auto;
}
.race-card {
  background: linear-gradient(160deg, var(--panel) 0%, #0a0f1a 100%);
  border: 1px solid var(--line); border-radius: 12px; padding: clamp(10px, 1.2vw, 16px);
  cursor: pointer; transition: all .18s; position: relative;
}
.race-card:hover { border-color: var(--accent); transform: translateY(-3px); }
.race-card.selected { border-color: var(--accent); box-shadow: 0 0 22px rgba(255,107,0,.35); }
.race-card.locked { opacity: .45; cursor: not-allowed; }
.race-card.locked:hover { border-color: var(--line); transform: none; }
.race-ico { font-size: clamp(24px, 2.4vw, 34px); }
.race-imgwrap {
  width: 100%; height: clamp(90px, 14vw, 150px); overflow: hidden; border-radius: 8px; margin-bottom: 10px;
  border: 1px solid var(--line); background: #0a0f1a;
}
.race-imgwrap img { width: 100%; height: 100%; object-fit: cover; display: block; }
.race-name { font-size: clamp(16px, 1.5vw, 20px); font-weight: 800; margin: 8px 0 4px; }
.race-type { color: var(--accent2); font-size: 11px; text-transform: uppercase; letter-spacing: 1px; }
.race-lore { color: var(--dim); font-size: 11px; line-height: 1.5; margin: 8px 0; }
.race-mech { font-size: 11px; line-height: 1.5; color: var(--text); }
.race-mech b { color: var(--gold); }
.race-badge { position:absolute; top:10px; right:10px; font-size: 10px; background: var(--panel2); border:1px solid var(--line); padding:2px 8px; border-radius: 10px; color: var(--dim); }
.race-footer { padding: 12px; text-align: center; }

/* ===== MAIN LAYOUT ===== */
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.empire-id { display: flex; flex-direction: column; min-width: 140px; }
.empire-name { font-weight: 800; font-size: 16px; }
.empire-race { color: var(--accent2); font-size: 11px; text-transform: uppercase; letter-spacing: 1px; }
.res-bar { display: flex; gap: 18px; }
.res { display: flex; align-items: baseline; gap: 6px; font-size: 13px; }
.res-ico { color: var(--gold); font-size: 10px; }
.ico-min { color: var(--cyan); }
.ico-res { color: var(--green); }
.res b { font-size: 16px; font-variant-numeric: tabular-nums; }
.res-lbl { color: var(--dim); font-size: 11px; }
.year-box { display: flex; align-items: center; gap: 12px; }
.year { font-size: 18px; font-weight: 800; color: var(--gold); }

#main { flex: 1; display: flex; min-height: 0; }
#map-wrap { flex: 1; position: relative; min-width: 0; }
#map-canvas { width: 100%; height: 100%; display: block; }
#map-hint { position: absolute; top: 8px; left: 12px; color: var(--dim); font-size: 12px; pointer-events: none; }
#map-controls {
  position: absolute; top: 8px; right: 12px; display: flex; gap: 6px; z-index: 5;
}
#map-controls .btn {
  min-width: 30px; padding: 4px 8px; font-size: 13px; text-align: center;
  background: rgba(16, 24, 40, 0.9); backdrop-filter: blur(4px);
}
#map-controls .btn:hover { border-color: var(--accent); color: var(--accent2); }
#legend { position: absolute; bottom: 8px; left: 12px; font-size: 11px; color: var(--dim); pointer-events: none; }

#side-panel {
  width: clamp(240px, 24vw, 340px); border-left: 1px solid var(--line); background: var(--panel);
  overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; flex-shrink: 0;
}
.panel { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; }
.panel-head { font-weight: 800; font-size: 13px; color: var(--accent2); margin-bottom: 8px; letter-spacing: 1px; }
.panel-head .sub { color: var(--dim); font-weight: 400; font-size: 11px; }

/* ===== SYSTEM PANEL ===== */
.sys-row { display: flex; justify-content: space-between; font-size: 12px; padding: 3px 0; border-bottom: 1px dashed #16233c; }
.sys-row:last-child { border-bottom: none; }
.sys-row b { color: var(--text); }
.sys-row .dim { color: var(--dim); }
.progress { height: 6px; background: #16233c; border-radius: 3px; overflow: hidden; margin: 4px 0; }
.progress > div { height: 100%; background: var(--accent); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 8px 0; }
.stat-box { background: #0c1424; border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; text-align: center; }
.stat-box .v { font-size: 15px; font-weight: 800; }
.stat-box .l { font-size: 10px; color: var(--dim); }
.section-lbl { font-size: 11px; color: var(--accent2); text-transform: uppercase; letter-spacing: 1px; margin: 10px 0 6px; font-weight: 700; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.btn-row .btn { flex: 1; min-width: 90px; font-size: 11px; padding: 6px 8px; }

/* ===== FLEETS ===== */
.fleet-item { border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-bottom: 8px; background: #0c1424; }
.fleet-item .fname { font-weight: 700; font-size: 12px; display: flex; justify-content: space-between; }
.fleet-item .fmeta { font-size: 11px; color: var(--dim); margin: 4px 0; }
.fleet-item select, .fleet-item input { width: 100%; margin-top: 4px; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 4px; padding: 4px; font-size: 11px; }
.fleet-item .btn { width: 100%; margin-top: 4px; font-size: 11px; padding: 5px; }

/* ===== AGENTS STRIP ===== */
#agents-strip {
  border-top: 1px solid var(--line); background: var(--panel); padding: 6px 12px 10px;
}
.strip-head { display: flex; align-items: center; gap: 10px; padding: 4px 0 8px; }
.strip-head span { font-weight: 800; font-size: 12px; color: var(--accent2); letter-spacing: 2px; flex: 1; }
#agent-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(140px, 40vw), 1fr)); gap: 8px; }
.agent-card {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; cursor: pointer; transition: all .15s; position: relative;
}
.agent-card:hover { border-color: var(--accent); }
.agent-card.warn { border-color: #8a6a2a; }
.agent-card.error { border-color: var(--red); }
.agent-top { display: flex; gap: 8px; align-items: center; }
.agent-portrait {
  width: 44px; height: 44px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 20px; border: 2px solid var(--line); background: #0c1424; flex-shrink: 0; overflow: hidden;
}
.agent-portrait img { width: 100%; height: 100%; object-fit: cover; display: block; }
.agent-edit-head { display: flex; align-items: center; gap: 12px; }
.agent-edit-portrait { width: 56px; height: 56px; }
.explorer-art { width: 100%; max-height: 180px; overflow: hidden; border-radius: 8px; margin-bottom: 8px; border: 1px solid var(--line); }
.explorer-art img { width: 100%; height: 180px; object-fit: cover; display: block; }
.agent-card .aname { font-weight: 700; font-size: 12px; }
.agent-card .arole { font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .5px; }
.agent-card .adirective { font-size: 10px; color: var(--accent2); margin-top: 4px; font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
.agent-card .amodel { font-size: 9px; color: var(--dim); position: absolute; bottom: 6px; right: 8px; }
.agent-card .astatus { font-size: 10px; color: var(--green); margin-top: 2px; }
.agent-status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 4px; }
.dot-ok { background: var(--green); }
.dot-warn { background: var(--gold); }
.dot-err { background: var(--red); }

/* ===== MODALS ===== */
.modal {
  position: fixed; inset: 0; background: rgba(2,4,8,.8); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  width: 100%; max-width: 640px; max-height: 86vh; overflow-y: auto; padding: 18px;
  box-shadow: 0 0 60px rgba(0,0,0,.8);
}
.modal-head { font-size: 16px; font-weight: 800; color: var(--accent2); margin-bottom: 12px; letter-spacing: 1px; }
.report-body { max-height: 55vh; overflow-y: auto; }
.report-entry { padding: 8px 0; border-bottom: 1px solid #152238; font-size: 13px; line-height: 1.55; }
.report-entry:last-child { border-bottom: none; }
.report-entry .agent-tag { font-weight: 800; color: var(--gold); }
.report-entry.battle { border-left: 3px solid var(--red); padding-left: 8px; }
.report-entry.good { border-left: 3px solid var(--green); padding-left: 8px; }
.report-entry.warn { border-left: 3px solid var(--gold); padding-left: 8px; }
.report-year { color: var(--dim); font-size: 12px; }

/* ===== DESIGNER ===== */
.design-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.design-col { display: flex; flex-direction: column; gap: 8px; }
.design-col label { font-size: 11px; color: var(--dim); }
.design-col select, .design-col input {
  background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  border-radius: 5px; padding: 6px; font-size: 13px; width: 100%;
}
.module-row { display: flex; justify-content: space-between; font-size: 12px; padding: 4px 6px; background: #0c1424; border-radius: 4px; margin-bottom: 4px; }
.module-row .mcost { color: var(--dim); }
.design-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px; }
.design-stats .stat-box .v { font-size: 13px; }
.goal-btns { display: flex; flex-wrap: wrap; gap: 6px; }
.goal-btns .btn { flex: 1; min-width: 88px; font-size: 11px; }
.design-name-input { font-size: 15px; font-weight: 700; }
.design-actions { margin-top: 14px; display: flex; gap: 8px; }

/* ===== SETTINGS ===== */
.set-row { margin-bottom: 14px; }
.set-row label { display: block; font-size: 11px; color: var(--dim); margin-bottom: 4px; }
.set-row input, .set-row select, .set-row textarea {
  width: 100%; background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  border-radius: 5px; padding: 7px; font-size: 13px;
}
.set-row textarea { min-height: 60px; resize: vertical; font-family: monospace; font-size: 11px; }
.set-row .hint { font-size: 10px; color: var(--dim); margin-top: 3px; }
.token-stat { font-size: 12px; color: var(--gold); }

/* ===== GOVERNANCE ===== */
.gov-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px dashed #16233c; font-size: 13px; }
.gov-row .up, .gov-row .down { cursor: pointer; color: var(--accent2); font-weight: 800; padding: 0 6px; }
.gov-row .up:hover, .gov-row .down:hover { color: #fff; }

/* ===== EVENT MODAL ===== */
.event-box { text-align: center; }
.event-ico { font-size: 52px; margin-bottom: 10px; }
.event-title { font-size: 20px; font-weight: 800; margin-bottom: 8px; }
.event-desc { font-size: 14px; color: var(--text); line-height: 1.6; margin-bottom: 16px; }
.event-opts { display: flex; flex-direction: column; gap: 8px; }

/* ===== LEADERBOARD + GAMEOVER + MENU ===== */
.leaderboard-box { max-width: 560px; }
.lb-current { font-size: 13px; color: var(--gold); margin-bottom: 10px; }
.lb-list { max-height: 40vh; overflow-y: auto; margin-bottom: 10px; }
.lb-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-bottom: 1px dashed #16233c; font-size: 13px;
}
.lb-row.lb-top { background: rgba(255, 209, 102, .08); }
.lb-rank { width: 30px; font-weight: 800; }
.lb-name { flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-race { color: var(--dim); font-size: 11px; width: 90px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-meta { color: var(--dim); font-size: 10px; width: 60px; }
.lb-score { font-weight: 800; color: var(--accent2); font-variant-numeric: tabular-nums; }
.lb-submit { display: flex; gap: 8px; margin-bottom: 10px; }
.lb-submit input {
  flex: 1; background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  border-radius: 5px; padding: 8px; font-size: 13px;
}
.gameover-box {
  margin-top: 14px; padding: 14px; border: 1px solid var(--gold);
  border-radius: 10px; background: rgba(255, 209, 102, .06); text-align: center;
}
.gameover-title { font-size: 22px; font-weight: 900; color: var(--gold); letter-spacing: 2px; }
.gameover-score { font-size: 15px; margin: 8px 0 12px; }
.gameover-score b { color: var(--accent2); font-size: 20px; }
.btn-resume {
  background: var(--green); border-color: var(--green); color: #04120a;
  font-weight: 800; padding: 12px 28px; font-size: 15px; margin-bottom: 8px;
}
.btn-resume:hover { background: #7cefa5; color: #04120a; }
.btn-leaderboard {
  background: var(--panel2); border: 1px solid var(--gold); color: var(--gold);
  padding: 10px 24px; font-size: 14px; margin-top: 8px;
}
.btn-leaderboard:hover { background: rgba(255, 209, 102, .15); }

/* ===== TUTORIAL ===== */
.tutorial-overlay {
  position: fixed; inset: 0; z-index: 250;
  background: rgba(2, 4, 10, 0.55);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0 20px 6vh;
  pointer-events: none;
}
.tutorial-card {
  pointer-events: auto;
  background: linear-gradient(170deg, var(--panel) 0%, #0a0f1c 100%);
  border: 1px solid var(--accent); border-radius: 14px;
  width: 100%; max-width: 620px; padding: 18px 20px 14px;
  box-shadow: 0 0 50px rgba(255, 107, 0, .25), 0 8px 40px rgba(0,0,0,.8);
  position: relative;
}
.tutorial-skip {
  position: absolute; top: 10px; right: 12px;
  background: none; border: 1px solid var(--line); color: var(--dim);
  padding: 4px 10px; border-radius: 6px; font-size: 11px; cursor: pointer;
}
.tutorial-skip:hover { color: var(--red); border-color: var(--red); }
.tutorial-body { display: flex; gap: 16px; align-items: flex-start; margin-top: 8px; }
.tutorial-portrait {
  flex-shrink: 0; width: 110px; display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.tutorial-portrait img {
  width: 100px; height: 100px; border-radius: 50%; object-fit: cover;
  border: 2px solid var(--gold); box-shadow: 0 0 18px rgba(255, 209, 102, .4);
}
.tutorial-portrait-name { font-size: 11px; color: var(--gold); text-align: center; font-weight: 700; }
.tutorial-content { flex: 1; min-width: 0; }
.tutorial-title { font-size: 18px; font-weight: 800; color: var(--accent2); margin-bottom: 8px; }
.tutorial-text { font-size: 13px; line-height: 1.65; color: var(--text); }
.tutorial-footer {
  display: flex; align-items: center; justify-content: space-between; margin-top: 14px;
}
.tutorial-dots { display: flex; gap: 6px; }
.tut-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.tut-dot.active { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.tutorial-actions { display: flex; gap: 8px; }

/* Highlight target element */
.tut-highlight {
  outline: 2px solid var(--gold) !important;
  outline-offset: 3px;
  animation: tutPulse 1.2s infinite;
  border-radius: 8px;
}
@keyframes tutPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 209, 102, .5); }
  50% { box-shadow: 0 0 0 12px rgba(255, 209, 102, 0); }
}

/* ===== RED ALERT ===== */
#red-alert {
  position: fixed; inset: 0; z-index: 200; background: rgba(120,0,0,.35);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  animation: alertfade 4s forwards;
}
.alert-pulse {
  font-size: 52px; font-weight: 900; color: #fff; letter-spacing: 8px;
  animation: pulse 1s infinite; text-shadow: 0 0 30px var(--red);
}
#alert-text { color: #ffd0d0; font-size: 16px; text-align: center; padding: 0 30px; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }
@keyframes alertfade { 0% { opacity: 0; } 10% { opacity: 1; } 80% { opacity: 1; } 100% { opacity: 0; visibility: hidden; } }

/* ===== MOBILE TABS ===== */
#mobile-tabs { display: none; }
@media (max-width: 1100px) {
  #side-panel { width: clamp(220px, 26vw, 300px); }
  .res-bar { gap: 12px; }
  .res-lbl { display: none; }
  .empire-id { min-width: 100px; }
}
@media (max-width: 860px) {
  body { overflow: auto; }
  .screen.active { overflow-y: auto; }
  /* compact topbar: empire left, resources + endturn right, single row */
  #topbar {
    flex-wrap: nowrap; padding: 6px 8px; gap: 6px;
    position: sticky; top: 0; z-index: 80;
  }
  .empire-id { min-width: 0; flex: 0 1 auto; }
  .empire-name { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 90px; }
  .empire-race { display: none; }
  .res-bar { gap: 6px; margin-left: auto; }
  .res { gap: 3px; }
  .res-ico { font-size: 8px; }
  .res b { font-size: 12px; }
  .res-lbl { display: none; }
  .year-box { flex-direction: row; align-items: center; gap: 6px; }
  .year { font-size: 12px; }
  .btn-endturn { padding: 6px 10px; font-size: 11px; border-radius: 6px; }

  #mobile-tabs {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 90;
    background: var(--panel); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .mtab { flex: 1; padding: 12px 4px; background: none; border: none; color: var(--dim); font-size: 12px; font-weight: 700; }
  .mtab.active { color: var(--accent); border-top: 2px solid var(--accent); }
  #main { flex-direction: column; }
  /* map fills the visible space; panels scroll below */
  #map-wrap { height: 46vh; min-height: 300px; }
  #side-panel { width: 100%; border-left: none; border-top: 1px solid var(--line); max-height: none; }
  #agents-strip { display: none; }
  #agents-strip.mobile-show { display: block; }
  .title-glow { font-size: clamp(26px, 8vw, 40px); letter-spacing: 4px; }
  .race-grid { grid-template-columns: 1fr 1fr; }
  .race-lore { display: none; }
  .race-imgwrap { height: 90px; }

  /* full-width modals on mobile */
  .modal { padding: 8px; align-items: flex-end; }
  .modal-box { max-width: none; max-height: 88vh; border-radius: 14px 14px 0 0; }
  .design-grid { grid-template-columns: 1fr; }

  /* tutorial */
  .tutorial-overlay { padding: 0 8px 60px; }
  .tutorial-card { padding: 14px 14px 10px; }
  .tutorial-portrait { width: 72px; }
  .tutorial-portrait img { width: 64px; height: 64px; }
  .tutorial-title { font-size: 15px; }
  .tutorial-text { font-size: 12px; }

  /* fleet/action buttons touch-friendly */
  .btn { padding: 9px 14px; font-size: 13px; }
  .btn-row .btn { min-width: 0; padding: 9px 6px; }
  .agent-card { padding: 10px; }
  .agent-portrait { width: 52px; height: 52px; }
  .adirective { white-space: normal; max-width: none; }
}
@media (max-width: 480px) {
  .race-grid { grid-template-columns: 1fr; }
  .race-imgwrap { height: 130px; }
  .modal-box { padding: 12px; }
  .design-grid { grid-template-columns: 1fr; }
  .empire-name { max-width: 70px; }
  .res b { font-size: 11px; }
  #map-wrap { height: 40vh; min-height: 240px; }
}
