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

:root {
  --bg: #f4f5f7;
  --card: #fff;
  --border: #e0e2e6;
  --text: #1a1d23;
  --text-dim: #6b7280;
  --accent: #0d9488;
  --accent-light: #ccfbf1;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.12);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Top bar ─────────────────────────────────── */
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
}
.topbar h1 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.topbar-right {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ── BEW fields in topbar ──────────────────────── */
.bew-field {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 12px;
  color: var(--text-dim);
}
.bew-field span {
  font-weight: 600;
}
.bew-field input {
  width: 62px;
  font-size: 13px;
  padding: 2px 5px;
  border: 1px solid var(--border);
  border-radius: 5px;
  text-align: right;
  background: transparent;
}

.btn-pax { background: #dbeafe; color: #1d4ed8; }
.btn-bag { background: #fef3c7; color: #92400e; }
.btn-fill {
  width: 100%;
  margin-top: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 0;
  border: none;
  border-radius: 5px;
  background: #e0f2fe;
  color: #0369a1;
  cursor: pointer;
}

/* ── Plan view ───────────────────────────────── */
.plan-container {
  position: relative;
  width: 100%;
}
.plan-view {
  position: relative;
  width: 100%;
  background: center / contain no-repeat;
}

/* Station drop zones */
.station {
  position: absolute;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: flex-start;
  gap: 6px;
  padding: 6px;
  min-height: 52px;
  z-index: 5;
  overflow: visible;
  background: rgba(244,245,247,.55);
  transition: border-color .15s, background .15s;
}
.station.drag-over {
  border-color: var(--accent);
  background: var(--accent-light);
}
.station.overweight {
  border-color: var(--danger);
  background: var(--danger-light);
}
.station.overweight .station-label,
.station.overweight .station-arm {
  color: var(--danger);
}
.station-label {
  position: absolute;
  top: -9px;
  left: 10px;
  background: rgba(244,245,247,.85);
  padding: 0 4px;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}
.station-arm {
  position: absolute;
  bottom: -8px;
  right: 8px;
  background: rgba(244,245,247,.85);
  padding: 0 4px;
  font-size: 9px;
  color: var(--text-dim);
}

/* Fuel box — top right */
.fuel-stack {
  position: absolute;
  right: 16px;
  top: 8%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 136px;
  z-index: 10;
}
.fuel-box {
  background: var(--card);
  border: 2px solid #cbd5e1;
  border-radius: var(--radius);
  padding: 6px 8px;
  font-size: 12px;
  position: relative;
}
.fuel-box .fuel-label {
  position: absolute;
  top: -9px;
  left: 10px;
  background: var(--bg);
  padding: 0 4px;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}
.fuel-box .fuel-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
}
.fuel-box .fuel-row:first-of-type { margin-top: 2px; }
.fuel-box .fuel-row label {
  font-size: 11px;
  color: var(--text-dim);
  width: 28px;
  text-align: right;
  flex-shrink: 0;
}
.fuel-box .fuel-row input {
  width: 100%;
  font-size: 13px;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
}

/* ── Tokens ──────────────────────────────────── */
.token {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: var(--shadow);
  position: relative;
  white-space: nowrap;
}
.token.dragging {
  opacity: 0.9;
  box-shadow: var(--shadow-lg);
  z-index: 200;
  position: fixed;
  pointer-events: none;
  cursor: grabbing;
}
.token.pax {
  background: #dbeafe;
  color: #1e40af;
  border: 1px solid #93c5fd;
}
.token.bag {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fcd34d;
}
.token .weight {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.7;
}
.token .remove {
  font-size: 14px;
  line-height: 1;
  margin-left: 2px;
  opacity: 0.4;
  cursor: pointer;
}
.token .remove:hover { opacity: 1; }

/* ── Add box — upper left ────────────────── */
.add-box {
  position: absolute;
  left: 16px;
  top: 4%;
  width: 136px;
  z-index: 10;
  background: var(--card);
  border: 2px solid #cbd5e1;
  border-radius: var(--radius);
  padding: 6px 8px;
}
.ramp-add {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ramp-add input {
  font-size: 12px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  width: 100%;
}
.ramp-add .ramp-btns {
  display: flex;
  gap: 4px;
}
.ramp-add .ramp-btns button {
  flex: 1;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 0;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

/* ── Left stack (ramp + hangar) — lower left ── */
.left-stack {
  position: absolute;
  left: 16px;
  bottom: 4%;
  width: 136px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── Shared drop-zone style (ramp & hangar) ── */
.ramp, .hangar {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
  min-height: 100px;
  padding: 10px 6px 6px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  overflow: visible;
  position: relative;
}
.ramp-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  text-align: center;
}
.ramp > .ramp-label,
.hangar > .ramp-label {
  position: absolute;
  top: -9px;
  left: 10px;
  background: rgba(244,245,247,.85);
  padding: 0 4px;
  z-index: 1;
}
.ramp.drag-over,
.hangar.drag-over {
  border-color: var(--accent);
  background: var(--accent-light);
}

/* ── CG panel — bottom right ───────────────── */
.cg-panel {
  position: absolute;
  right: 4px;
  bottom: 1%;
  width: 320px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  z-index: 1;
  cursor: pointer;
  transition: all .25s ease;
}
.cg-panel.expanded {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: auto;
  border-radius: 0;
  z-index: 300;
  cursor: default;
  padding: 16px;
}
.cg-collapse {
  display: none;
  float: right;
  margin-left: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 13px;
  cursor: pointer;
}
.cg-panel.expanded .cg-collapse { display: block; }
.cg-toggle-axis {
  display: none;
  float: right;
  margin-left: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
}
.cg-panel.expanded .cg-toggle-axis { display: block; }
.cg-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 6px;
}
.envelope-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.envelope-status.ok { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.envelope-status.bad { background: var(--danger); box-shadow: 0 0 6px var(--danger); animation: pulse-bad 1.5s infinite; }
@keyframes pulse-bad {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
.cg-summary {
  font-size: 11px;
  color: var(--text-dim);
}
.cg-summary b { color: var(--text); }
.cg-panel canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* ── Narrow (portrait phone) layout ────────── */
@media (max-aspect-ratio: 9/16) {
  .plan-container {
    display: flex;
    flex-direction: column;
  }
  .widgets {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto;
    gap: 10px 8px;
    padding: 12px 8px 8px;
  }
  .widgets > * {
    min-width: 0;
  }
  .add-box, .left-stack, .fuel-stack, .cg-panel {
    position: static;
    width: auto;
  }
  .left-stack {
    grid-column: 1;
    grid-row: 2 / 4;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }
  .ramp, .hangar {
    min-height: 80px;
  }
  .station { min-height: 0; }
  .add-box    { grid-column: 1; grid-row: 1; }
  .fuel-stack { grid-column: 2; grid-row: 1; }
  .cg-panel   { grid-column: 2; grid-row: 2 / 4; width: auto; }
}
