:root {
  --bg: #070a12;
  --bg-2: #0d1220;
  --panel: #111829;
  --panel-2: #172036;
  --line: #24304a;
  --line-2: #33415f;
  --text: #e6ebf5;
  --muted: #8d98b3;
  --faint: #5d6886;
  --accent: #7cc4ff;
  --accent-2: #4a9be0;
  --good: #6fd08c;
  --bad: #ff7a7a;
  --warn: #f2c060;
  --radius: 10px;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 30% 0%, #111a33 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn { color: var(--warn); }

#app { display: flex; flex-direction: column; height: 100%; }

/* Top bar */
.top {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; border-bottom: 1px solid var(--line);
  background: rgba(7, 10, 18, 0.85); backdrop-filter: blur(6px);
}
.brand { font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; font-size: 13px; color: var(--accent); }
.stats { display: flex; gap: 18px; flex: 1; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; line-height: 1.15; }
.stat .k { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--faint); }
.stat .v { font-family: var(--mono); font-size: 14px; }

.ship-strip {
  display: flex; gap: 18px; align-items: center; flex-wrap: wrap;
  padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--bg-2); font-size: 13px;
}
.meter { display: flex; align-items: center; gap: 8px; min-width: 150px; }
.meter .label { color: var(--muted); font-size: 12px; width: 62px; }
.bar { flex: 1; height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; min-width: 60px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.bar.fuel > i { background: #e3a35b; }
.bar.cond > i { background: var(--good); }
.bar.cond.low > i { background: var(--warn); }
.bar.cond.crit > i { background: var(--bad); }
.where { margin-left: auto; color: var(--muted); }
.where b { color: var(--text); font-weight: 600; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(380px, 1fr); min-height: 0; }
.layout > * { min-width: 0; }
html, body { overflow-x: clip; }
.map-pane { position: relative; border-right: 1px solid var(--line); min-height: 0; display: flex; flex-direction: column; }
#map { width: 100%; flex: 1; min-height: 0; display: block; touch-action: manipulation; }
.map-legend {
  font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); background: rgba(13, 18, 32, 0.85);
  padding: 8px 12px; display: flex; gap: 6px 12px; flex-wrap: wrap;
}
.map-legend span { display: inline-flex; align-items: center; gap: 5px; }
.map-legend i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.side { display: flex; flex-direction: column; min-height: 0; background: rgba(13, 18, 32, 0.6); }
.tabs { display: flex; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs button {
  background: none; border: 0; padding: 11px 14px; color: var(--muted); cursor: pointer;
  border-bottom: 2px solid transparent; white-space: nowrap; font-size: 13px;
}
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); }
.tabs .tab-menu { margin-left: auto; color: var(--faint); }
.tabs button .dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--warn); margin-left: 5px; vertical-align: middle; }
.panel { flex: 1; overflow-y: auto; padding: 14px 16px 90px; }

/* Map */
.sys-label { font-size: 11px; fill: var(--muted); pointer-events: none; paint-order: stroke; stroke: var(--bg); stroke-width: 3px; }
.sys-label.here { fill: var(--text); font-weight: 600; }
.sys { cursor: pointer; }
.lane { stroke: #3a4a6e; stroke-width: 1.5; }
.lane.hazard { stroke: #b5654a; stroke-width: 2; }
.lane.route { stroke: var(--accent); stroke-width: 3; }
.here-ring { fill: none; stroke: var(--text); stroke-width: 1.5; animation: pulse 2.4s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes pulse { 0%, 100% { opacity: 0.9; } 50% { opacity: 0.25; } }
.sel-ring { fill: none; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 3 2; }
.reach-ring { fill: none; stroke: #e3a35b; stroke-width: 1.5; }
.reach-ring.full { stroke: rgba(227, 163, 91, 0.6); stroke-dasharray: 3 2; }
.map-legend .legend-row { display: flex; gap: 12px; flex-wrap: wrap; flex-basis: 100%; }
.map-legend i.key-line { width: 16px; height: 0; border-radius: 0; border-top: 2px solid #3a4a6e; }
.map-legend i.key-line.hazard { border-top-color: #b5654a; }
.map-legend i.key-line.route { border-top: 3px solid var(--accent); }
.map-legend i.key-ring { width: 10px; height: 10px; border-radius: 50%; border: 1.5px solid #e3a35b; background: transparent; }
.map-legend i.key-ring.full { border-style: dashed; border-color: rgba(227, 163, 91, 0.7); }
.map-legend i.key-ring.unvisited { border: 1.2px dashed var(--muted); }

/* Panels */
h2 { font-size: 17px; margin: 0 0 2px; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--faint); margin: 20px 0 8px; font-weight: 600; }
.sub { color: var(--muted); font-size: 13px; margin-bottom: 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.chip { font-size: 11px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--line-2); color: var(--muted); }
.chip.on { border-color: var(--accent-2); color: var(--accent); }
button.chip { background: none; cursor: pointer; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.fight-log { margin: 0 0 10px; padding-left: 18px; font-size: 13px; color: var(--muted); }
.fight-log li:last-child { color: var(--text, inherit); }
.note { font-size: 12px; color: var(--muted); }

.btn {
  background: var(--accent-2); color: #04101d; border: 0; border-radius: 8px; padding: 7px 12px;
  font-weight: 600; cursor: pointer; font-size: 13px;
}
.btn:hover { filter: brightness(1.1); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; filter: none; }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--line-2); font-weight: 500; }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.danger { background: #c04a4a; color: #fff; }

/* Market table */
table.mkt { width: 100%; border-collapse: collapse; font-size: 13px; }
table.mkt th { text-align: right; font-weight: 500; color: var(--faint); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; padding: 6px 4px; border-bottom: 1px solid var(--line); position: sticky; top: -14px; background: var(--bg-2); }
table.mkt th:first-child, table.mkt td:first-child { text-align: left; }
table.mkt td { padding: 6px 4px; border-bottom: 1px solid rgba(36, 48, 74, 0.6); text-align: right; white-space: nowrap; }
table.mkt tr.cat td { color: var(--accent); font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; padding-top: 14px; border-bottom: 1px solid var(--line); }
table.mkt tr.sel td { background: rgba(74, 155, 224, 0.12); }
table.mkt td.name { white-space: normal; }
table.mkt tr.pick { cursor: pointer; }
table.mkt tr.pick:hover td { background: rgba(124, 196, 255, 0.06); }
table.mkt .small, .small { font-size: 11px; }
.show-sm { display: none; }
table.mkt td.best { white-space: normal; max-width: 130px; }
.seg button:disabled { opacity: 0.4; cursor: not-allowed; }
table.mkt .tag { font-size: 10px; color: var(--faint); margin-left: 4px; }
.lvl { display: inline-block; width: 34px; height: 5px; border-radius: 3px; background: var(--line); vertical-align: middle; overflow: hidden; }
.lvl > i { display: block; height: 100%; }
.trade-bar {
  position: sticky; bottom: -90px; margin: 12px -16px -90px; padding: 12px 16px 16px;
  background: var(--panel-2); border-top: 1px solid var(--line-2); box-shadow: 0 -8px 20px rgba(0, 0, 0, 0.35);
}
.trade-bar input[type=range] { width: 100%; accent-color: var(--accent); }
.trade-bar .trade-go { min-width: 96px; padding: 9px 16px; font-size: 14px; }
.trade-bar .trade-detail { margin-top: 8px; }
.qty-input { width: 72px; background: var(--bg); border: 1px solid var(--line-2); color: var(--text); border-radius: 6px; padding: 4px 6px; font-family: var(--mono); }

/* Log */
.log-item { padding: 7px 0; border-bottom: 1px solid rgba(36, 48, 74, 0.6); font-size: 13px; display: flex; gap: 10px; }
.log-item .d { color: var(--faint); font-family: var(--mono); font-size: 12px; min-width: 52px; }
.log-item.good .t { color: var(--good); }
.log-item.bad .t { color: var(--bad); }
.log-item.rumor .t { color: var(--muted); font-style: italic; }
.log-item.warn .t { color: var(--warn); }
.card.talk { border-style: dashed; }
.ship-strip .due { font-size: 12px; font-weight: 600; }

/* Modal */
/* The backdrop scrolls, so a dialog taller than a phone screen can always be scrolled to its end. */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(3, 5, 10, 0.7); display: flex; align-items: flex-start; justify-content: center;
  z-index: 20; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.modal-backdrop[hidden] { display: none; }
.modal { background: var(--panel); border: 1px solid var(--line-2); border-radius: 14px; width: min(560px, 100%); margin: auto; padding: 20px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); }
.modal h2 { margin-bottom: 6px; }
.modal .actions {
  display: flex; gap: 8px; justify-content: flex-end; margin: 16px -20px -20px; flex-wrap: wrap;
  position: sticky; bottom: calc(-16px - env(safe-area-inset-bottom)); padding: 12px 20px calc(16px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 0 0 14px 14px;
}
.field { margin: 12px 0; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input[type=text], .field input[type=number], .field select {
  width: 100%; background: var(--bg); border: 1px solid var(--line-2); color: var(--text); border-radius: 8px; padding: 8px 10px; font: inherit;
}
.ship-pick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ship-pick button { text-align: left; background: var(--bg-2); border: 1px solid var(--line-2); border-radius: 10px; padding: 10px; cursor: pointer; }
.ship-pick button.on { border-color: var(--accent); background: rgba(74, 155, 224, 0.12); }
.ship-pick b { display: block; }
.ship-pick small { color: var(--muted); display: block; font-size: 11px; margin-top: 2px; }
.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: 8px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 6px 12px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--accent-2); color: #04101d; font-weight: 600; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 10px; padding: 10px 16px; z-index: 30; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5); max-width: calc(100% - 32px); }
.toast.bad { border-color: var(--bad); }

/* Law */
.tag.contraband { color: #ff9a6a; }
.chip.bm { border-color: #ff9a6a; color: #ff9a6a; }
.chip.untapped { border-color: var(--good); color: var(--good); }
.chip.bad-chip { border-color: var(--bad); color: var(--bad); }
.chip.law-0 { border-color: #ff9a6a; color: #ff9a6a; }
.chip.law-3 { border-color: var(--accent); color: var(--accent); }
h3.bm-head { color: #ff9a6a; }
table.bm-table { border-left: 2px solid rgba(255, 154, 106, 0.4); }
.bar.heat > i, .repbar.heat > i { background: #ff7a4a; }
.standing dd { display: flex; align-items: center; gap: 8px; }
.repbar { position: relative; flex: 1; height: 6px; background: var(--line); border-radius: 3px; min-width: 80px; }
.repbar > i { position: absolute; top: 0; bottom: 0; border-radius: 3px; }
.repbar > i.pos { background: var(--good); }
.repbar > i.neg { background: var(--bad); }

/* Analyst */

.an-views { margin: 6px 0 4px; flex-wrap: wrap; }
.an-line { margin-top: 4px; }
.an-actions { margin-top: 10px; }
.advice.warn { border-color: rgba(242, 192, 96, 0.5); }
.advice.bad { border-color: rgba(255, 122, 122, 0.6); }
.chip.conf-high { border-color: var(--good); color: var(--good); }
.chip.conf-medium { border-color: var(--warn); color: var(--warn); }
.chip.conf-low { border-color: var(--bad); color: var(--bad); }
.board { overflow-x: auto; margin: 0 -4px; }
.board table.mkt th, .board table.mkt td { padding: 5px 6px; }
.board .sticky { position: sticky; left: 0; background: var(--bg-2); z-index: 1; }
.board thead .sticky { z-index: 2; }
.linklike { background: none; border: 0; padding: 0; color: var(--text); cursor: pointer; text-align: left; font: inherit; }
.linklike:hover { color: var(--accent); }
.qty-input.wide { width: auto; min-width: 200px; padding: 6px 8px; font-family: inherit; }
.map-legend button.chip { padding: 2px 8px; }

.bug-text { width: 100%; background: var(--bg); border: 1px solid var(--line-2); color: var(--text); border-radius: 8px; padding: 8px 10px; font: inherit; resize: vertical; }
.bug-text.mono { font-family: var(--mono); font-size: 11px; color: var(--muted); }

/* Guided tour */
.tut-card {
  position: fixed; left: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); width: min(400px, calc(100% - 32px)); z-index: 15;
  background: var(--panel-2); border: 1px solid var(--accent-2); border-radius: 12px; padding: 14px 16px 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  display: flex; flex-direction: column; max-height: 60vh; max-height: 60dvh;
}
.tut-card[hidden] { display: none; }
/* The text scrolls inside the card; the buttons below it always stay visible. */
.tut-body { overflow-y: auto; -webkit-overflow-scrolling: touch; min-height: 0; flex: 1 1 auto; }
.tut-buttons { flex: none; padding-top: 8px; border-top: 1px solid var(--line); margin-top: 4px; }
.tut-card[hidden] { display: none; }
.tut-card h3 { margin: 2px 0 6px; font-size: 15px; color: var(--text); text-transform: none; letter-spacing: 0; }
.tut-card p { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.tut-step { font-size: 11px; color: var(--accent); text-transform: uppercase; letter-spacing: 0.08em; }
.tut-do { color: var(--warn) !important; font-weight: 600; }
.tut-do.done { color: var(--good) !important; }
.tut-focus { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 8px; animation: tutpulse 1.6s ease-in-out infinite; }
.map-pane.tut-focus, #shipStrip.tut-focus, table.mkt.tut-focus { outline-offset: -3px; }
@keyframes tutpulse { 0%, 100% { outline-color: var(--accent); } 50% { outline-color: rgba(124, 196, 255, 0.25); } }

/* Phones and narrow windows */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  #map { flex: none; height: 42vh; }
  #app { height: auto; min-height: 100%; }
  .map-pane { border-right: 0; border-bottom: 1px solid var(--line); }
  .side { min-height: 56vh; }
  .panel { overflow: visible; }
  .stats { gap: 12px; }
  /* The finances stay pinned at the top while the page scrolls. */
  .top { position: sticky; top: env(safe-area-inset-top, 0px); z-index: 12; padding: 8px 12px; gap: 12px; flex-wrap: nowrap; background: rgba(7, 10, 18, 0.96); }
  .stats { flex-wrap: nowrap; justify-content: space-between; }
  table.mkt th { top: 44px; }
  table.mkt { font-size: 12px; }
  table.mkt th, table.mkt td { padding: 6px 3px; }
  table.mkt th { font-size: 10px; letter-spacing: 0.02em; }
  .ship-strip { padding: 8px 12px; gap: 10px; }
  .where { margin-left: 0; width: 100%; }
  .meter { min-width: 0; flex: 1 1 140px; }
  .panel { padding: 12px 12px 90px; }
  /* A bottom sheet that clears the iPhone home bar and Safari toolbar; the Buy/Sell button sits mid-sheet. */
  .trade-bar {
    position: fixed; left: 0; right: 0; bottom: 0; margin: 0; z-index: 14;
    padding: 12px 14px calc(28px + env(safe-area-inset-bottom)); border-radius: 14px 14px 0 0;
  }
  .ship-pick { grid-template-columns: 1fr; }
  table.mkt .hide-sm { display: none; }
  .show-sm { display: block; }
  .tut-card { left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); width: auto; max-height: 42vh; max-height: 42dvh; }
  /* While the trade bar is open at the bottom, the tour card moves up under the header. */
  body.trading .tut-card { bottom: auto; top: calc(52px + env(safe-area-inset-top)); max-height: 34vh; max-height: 34dvh; }
  .panel { padding-bottom: 260px; }
  .card .row { gap: 8px; }
}

/* Keep toasts clear of dialog buttons */
.modal-backdrop:not([hidden]) ~ .toast { bottom: auto; top: 16px; }

/* Port charters */
.bar.gw > i { background: #5fd38d; }
.bar.gw.low > i { background: #ff7a4a; }
.charter-flag { stroke: #070a12; stroke-width: 1; pointer-events: none; }
.key-flag { display: inline-block; width: 7px; height: 7px; background: #fff; transform: rotate(45deg); margin-right: 5px; }
.tag.need { background: rgba(240, 190, 60, 0.18); color: #f0be3c; padding: 1px 6px; border-radius: 8px; white-space: nowrap; }
.tag.need.req { background: #f0be3c; color: #1a1406; font-weight: 600; }
table.mkt tr.needed td.name { box-shadow: inset 3px 0 0 #f0be3c; }
.card.advice.need { border-left: 3px solid #f0be3c; }
.need-line { color: #f0be3c; margin-top: 4px; }
ul.tight { margin: 6px 0; padding-left: 18px; }

/* Your cargo box on the Port tab */
.cargo-box table.mkt td:nth-child(2) { white-space: normal; text-align: left; }
.cargo-box table.mkt { margin-top: 6px; }
