:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --gap: 10px;
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.theme-dark {
  --bg: #16191d;
  --card: rgba(28, 32, 37, 0.92);
  --card-border: rgba(255, 255, 255, 0.08);
  --text: #f1f3f5;
  --muted: #9aa3ad;
  --accent: #3fa9ff;
  --warn: #ffb020;
  --danger: #ff5a52;
  --btn: rgba(36, 41, 47, 0.94);
}
.theme-light {
  --bg: #e8e6df;
  --card: rgba(255, 255, 255, 0.94);
  --card-border: rgba(0, 0, 0, 0.08);
  --text: #1b1f23;
  --muted: #5f6b76;
  --accent: #0a7cff;
  --warn: #c77700;
  --danger: #d93025;
  --btn: rgba(255, 255, 255, 0.96);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  overscroll-behavior: none; touch-action: none;
  user-select: none; -webkit-user-select: none;
}
canvas#map { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; display: block; }

.hidden { display: none !important; }

.card {
  position: fixed;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* Job card ---------------------------------------------------------------- */
#job {
  top: calc(var(--safe-t) + var(--gap));
  left: calc(var(--safe-l) + var(--gap));
  right: calc(var(--safe-r) + var(--gap) + 58px);
  padding: 10px 14px 10px;
  max-width: 560px;
}
.job-line1 { font-size: 17px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.job-line2 { font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.job-line2:empty { display: none; }
.job-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px 10px; margin-top: 8px; }
.job-stats .v, .job-stats .r { overflow: hidden; text-overflow: ellipsis; }
/* Phones in portrait: 2 x 2, so values never run into each other. */
@media (max-width: 560px) { .job-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.job-stats > div { display: flex; flex-direction: column; min-width: 0; }
.job-stats .k { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.job-stats .v { font-size: 16px; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
#deadline.late { color: var(--danger); }
#deadline.tight { color: var(--warn); }

/* Banner ------------------------------------------------------------------ */
.banner {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 150px);
  padding: 8px 16px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--card-border);
  font-size: 14px; font-weight: 600; color: var(--warn);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
  pointer-events: none; white-space: nowrap;
}

/* Speed cluster ------------------------------------------------------------ */
#speed {
  left: calc(var(--safe-l) + var(--gap));
  bottom: calc(var(--safe-b) + var(--gap) + 52px);
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px 8px 14px;
}
.speed-main { display: flex; flex-direction: column; align-items: center; line-height: 1; }
#speed-val { font-size: 44px; font-weight: 750; font-variant-numeric: tabular-nums; min-width: 2ch; text-align: center; }
#speed-val.over { color: var(--danger); }
.speed-main .unit { font-size: 11px; color: var(--muted); margin-top: 2px; }
.speed-side { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.limit {
  width: 42px; height: 42px; border-radius: 50%;
  background: #fff; border: 5px solid #e3241b;
  display: flex; align-items: center; justify-content: center;
  color: #111; font-weight: 800; font-size: 16px; font-variant-numeric: tabular-nums;
}
.cruise { font-size: 12px; font-weight: 700; color: var(--accent); }
.gear { font-size: 12px; font-weight: 700; color: var(--muted); }

/* Info strip -------------------------------------------------------------- */
#info {
  left: calc(var(--safe-l) + var(--gap));
  right: calc(var(--safe-r) + var(--gap));
  bottom: calc(var(--safe-b) + var(--gap));
  height: 44px;
  display: flex; align-items: center; justify-content: space-around; gap: 8px;
  padding: 0 10px;
  font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.info-item { display: flex; align-items: center; gap: 6px; min-width: 0; white-space: nowrap; }
.ico { width: 18px; height: 18px; fill: var(--muted); flex: none; }
.fuel-wrap { width: 44px; height: 8px; border-radius: 4px; background: rgba(127, 127, 127, 0.3); overflow: hidden; }
.fuel-bar { height: 100%; width: 0%; background: var(--accent); border-radius: 4px; }
.fuel-bar.low { background: var(--danger); }
#rest-val.tired { color: var(--warn); }

/* Controls ---------------------------------------------------------------- */
#controls {
  position: fixed;
  top: calc(var(--safe-t) + var(--gap));
  right: calc(var(--safe-r) + var(--gap));
  display: flex; flex-direction: column; gap: 8px;
}
.btn {
  width: 48px; height: 48px; border-radius: 12px;
  border: 1px solid var(--card-border); background: var(--btn); color: var(--text);
  font-size: 26px; font-weight: 500; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
  padding: 0; cursor: pointer;
}
.btn svg { width: 24px; height: 24px; fill: var(--text); }
.btn:active { transform: scale(0.94); }
#compass { transition: transform 0.15s linear; }
#compass .n { fill: var(--danger); }
#compass .s { fill: var(--muted); }
.btn.on { outline: 2px solid var(--accent); }

.pill {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 66px);
  display: flex; align-items: center; gap: 8px;
  padding: 10px 18px; border-radius: 999px; border: none;
  background: var(--accent); color: #fff; font-size: 15px; font-weight: 650;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35); cursor: pointer;
}
.pill svg { width: 18px; height: 18px; fill: #fff; }

/* Settings ---------------------------------------------------------------- */
dialog#settings {
  border: 1px solid var(--card-border); border-radius: 18px;
  background: var(--card); color: var(--text);
  width: min(92vw, 420px); padding: 18px 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 19px; }
dialog label { display: flex; align-items: center; gap: 10px; padding: 9px 0; font-size: 15px; }
dialog input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
dialog .row { display: flex; gap: 8px; margin: 10px 0; }
dialog button {
  flex: 1; padding: 11px; border-radius: 10px; border: 1px solid var(--card-border);
  background: var(--btn); color: var(--text); font-size: 15px; font-weight: 600;
}
dialog button.primary { width: 100%; background: var(--accent); color: #fff; border: none; }
.about { font-size: 12px; color: var(--muted); line-height: 1.4; }

/* Landscape: stack job card left, give the map the middle --------------- */
@media (orientation: landscape) and (max-height: 520px) {
  #job { right: auto; width: min(46vw, 420px); }
  .job-stats { grid-template-columns: repeat(2, 1fr); }
  #info { left: auto; width: min(52vw, 460px); }
  #speed { bottom: calc(var(--safe-b) + var(--gap)); }
  .banner { bottom: calc(var(--safe-b) + 70px); }
  .pill { bottom: calc(var(--safe-b) + 66px); }
}

/* Toast ------------------------------------------------------------------ */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  top: calc(var(--safe-t) + 150px);
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--card-border); color: var(--text);
  font-size: 14px; font-weight: 600; box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
  white-space: nowrap; z-index: 5;
}
.toast button { background: none; border: none; color: var(--accent); font-weight: 700; font-size: 14px; padding: 0; cursor: pointer; }

/* Convoy ------------------------------------------------------------------ */
#btn-convoy { position: relative; }
.badge {
  position: absolute; top: -5px; right: -5px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
dialog#convoy-sheet {
  border: 1px solid var(--card-border); border-radius: 18px;
  background: var(--card); color: var(--text);
  width: min(94vw, 440px); max-height: 86vh; padding: 18px 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}
.field { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; }
.field span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.field input {
  font-size: 16px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--card-border); background: var(--bg); color: var(--text);
}
#convoy-list { list-style: none; margin: 8px 0 14px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#convoy-list li {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px;
  background: rgba(127, 127, 127, 0.12); cursor: pointer;
}
#convoy-list li.empty { cursor: default; font-size: 13px; color: var(--muted); }
#convoy-list .dot { width: 14px; height: 14px; border-radius: 50%; flex: none; border: 2px solid #fff; }
#convoy-list .who { display: flex; flex-direction: column; min-width: 0; flex: 1; }
#convoy-list .who small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#convoy-list .num { font-size: 13px; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Landscape phones: wide dialogs in two columns instead of a tall scroll. */
@media (orientation: landscape) and (max-height: 520px) {
  dialog#settings, dialog#convoy-sheet { width: min(94vw, 760px); max-height: 92vh; padding: 14px 18px; }
  dialog h2 { margin-bottom: 6px; }
  dialog#settings .opts { columns: 2; column-gap: 24px; }
  dialog#settings label { padding: 6px 0; break-inside: avoid; }
  .convoy-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 20px; align-items: start; }
  #convoy-list { margin-top: 8px; max-height: 52vh; overflow-y: auto; }
}

#convoy-list .me-btn {
  flex: none; padding: 6px 10px; border-radius: 8px; border: none;
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; cursor: pointer;
}

/* Real-time equivalents under game times */
.job-stats .r { font-size: 11px; font-weight: 600; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.job-stats .r:empty { display: none; }
.tv { display: flex; flex-direction: column; line-height: 1.12; }
.tv small { font-size: 10.5px; font-weight: 600; color: var(--muted); }
.tv small:empty { display: none; }

/* Convoy rows: name, trip country to country, stats, actions */
#convoy-list li { align-items: flex-start; }
#convoy-list .dot { margin-top: 4px; }
#convoy-list .who small.trip { color: var(--text); opacity: 0.85; white-space: normal; }
#convoy-list .acts { display: flex; flex-direction: column; gap: 6px; flex: none; }
#convoy-list .acts button.act {
  padding: 6px 10px; border-radius: 8px; border: 1px solid var(--card-border);
  background: var(--btn); color: var(--text); font-size: 12px; font-weight: 700; cursor: pointer; white-space: nowrap;
}
#convoy-list .acts button.act.on { background: var(--accent); color: #fff; border-color: transparent; }
