:root {
  --bg: #0a0f0d;
  --panel: rgba(14, 20, 18, 0.94);
  --panel-solid: #0e1412;
  --line: #22302a;
  --line-2: #2d3d36;
  --text: #e4ede8;
  --muted: #8aa196;
  --accent: #6fd08c;
  --accent-ink: #08130c;
  --pred: #ff6b6b;
  --pw-left: 320px;
  --pw-right: 360px;
  --radius: 10px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }

#world { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#world.dragging { cursor: grabbing; }

/* ---------- HUD ---------- */
.hud { position: fixed; top: calc(10px + var(--sat)); left: 50%; transform: translateX(-50%); z-index: 5;
  display: flex; align-items: center; gap: 10px; padding: 5px 14px 5px 5px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 6px 24px rgba(0,0,0,.35);
  white-space: nowrap; max-width: calc(100vw - 120px); }
.hud-stats { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); overflow: hidden; }
.hud-stats b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }
.hud-stats .sep { width: 1px; height: 14px; background: var(--line-2); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); margin-right: 4px; vertical-align: -1px; }
.tri { display: inline-block; width: 0; height: 0; border-left: 10px solid var(--pred); border-top: 5px solid transparent;
  border-bottom: 5px solid transparent; margin-right: 4px; vertical-align: -1px; }

.icon-btn { width: 32px; height: 32px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  font-size: 12px; font-weight: 700; cursor: pointer; flex: none; display: grid; place-items: center; }
.icon-btn.small { width: 26px; height: 26px; background: transparent; color: var(--muted); font-size: 14px; }
.icon-btn.small:hover { color: var(--text); background: rgba(255,255,255,.06); }

/* ---------- Pannelli laterali ---------- */
.panel { position: fixed; top: 0; bottom: 0; z-index: 10; background: var(--panel); backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px); transition: transform .28s cubic-bezier(.2,.8,.2,1); display: flex; flex-direction: column; }
.panel.left { left: 0; width: calc(var(--pw-left) + var(--sal)); padding-left: var(--sal); border-right: 1px solid var(--line); transform: translateX(-100%); }
.panel.right { right: 0; width: calc(var(--pw-right) + var(--sar)); padding-right: var(--sar); border-left: 1px solid var(--line); transform: translateX(100%); }
.panel.open { transform: none; }

.panel-tab { position: absolute; top: 50%; transform: translateY(-50%); width: 30px; padding: 14px 0; border: 1px solid var(--line);
  background: var(--panel-solid); color: var(--muted); cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.3); }
.panel-tab:hover { color: var(--text); }
.panel.left .panel-tab { left: 100%; border-left: 0; border-radius: 0 var(--radius) var(--radius) 0; }
.panel.right .panel-tab { right: 100%; border-right: 0; border-radius: var(--radius) 0 0 var(--radius); }
.tab-label { writing-mode: vertical-rl; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; font-weight: 600; }
.panel.left .tab-label { transform: rotate(180deg); }
.chev { font-size: 18px; line-height: 1; transition: transform .28s; }
.panel.open .chev { transform: rotate(180deg); }

.panel-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: calc(16px + var(--sat)) 18px calc(20px + var(--sab)); }
.panel-body::-webkit-scrollbar { width: 8px; }
.panel-body::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 4px; }

.brand { margin: 0 0 14px; font-size: 20px; letter-spacing: -.01em; }
.brand small { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
section { border-top: 1px solid var(--line); padding: 14px 0 6px; }
h2 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; }
h2 small { text-transform: none; letter-spacing: 0; font-weight: 400; }

.row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.row.wrap { flex-wrap: wrap; }
.row.tight { margin: 0; gap: 6px; }
.row.tight input { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

.btn { border: 1px solid var(--line-2); background: #16201c; padding: 7px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: #1c2924; border-color: #3a4e45; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.wide { width: 100%; margin-top: 4px; }
.btn.icon { padding: 6px 9px; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.field { display: block; margin-bottom: 10px; }
.field > span { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field output { color: var(--text); font-variant-numeric: tabular-nums; }
.field input[type=number], .field input[type=text], .field select {
  width: 100%; background: #0b110f; border: 1px solid var(--line-2); border-radius: 8px; padding: 7px 9px; }
.field input:focus, .field select:focus { outline: 2px solid rgba(111,208,140,.35); border-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); margin: 2px 0; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; margin: 4px 0 10px; cursor: pointer; }
.check input { accent-color: var(--accent); margin-top: 3px; }
.check small, .hint { color: var(--muted); font-size: 12px; }
.hint { margin: 6px 0 8px; }
.help p { color: var(--muted); font-size: 12.5px; margin: 0 0 8px; }
kbd { font: 11px ui-monospace, monospace; padding: 1px 5px; border: 1px solid var(--line-2); border-radius: 4px; color: var(--text); }

/* ---------- Grafici ---------- */
.chart { margin-bottom: 14px; }
.chart h3 { margin: 0 0 4px; font-size: 13px; font-weight: 600; }
.chart canvas { width: 100%; height: 110px; display: block; background: rgba(0,0,0,.18); border: 1px solid var(--line); border-radius: 8px; }
.legend { display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: 3px; }
.legend b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; margin-left: 3px; }

.species, .events { list-style: none; margin: 0; padding: 0; }
.species li { display: grid; grid-template-columns: 14px 1fr auto; gap: 8px; align-items: center; padding: 5px 0; font-size: 12.5px; cursor: pointer; border-radius: 6px; }
.species li:hover { background: rgba(255,255,255,.04); }
.species .sw { width: 12px; height: 12px; border-radius: 50%; }
.species .sw.pred { border-radius: 2px; transform: rotate(45deg) scale(.85); }
.species .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.species .nm small { color: var(--muted); margin-left: 4px; }
.species .ct { font-variant-numeric: tabular-nums; color: var(--muted); }
.species .bar { grid-column: 2 / 4; height: 3px; border-radius: 2px; background: var(--line); overflow: hidden; margin-top: -2px; }
.species .bar span { display: block; height: 100%; }
.events li { font-size: 12px; color: var(--muted); padding: 4px 0; border-bottom: 1px dashed var(--line); display: flex; gap: 8px; }
.events li b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; min-width: 46px; }
.events .ev-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; flex: none; }
.empty { color: var(--muted); font-size: 12px; }

/* ---------- Ispettore ---------- */
.inspector { position: fixed; z-index: 8; left: 50%; bottom: calc(14px + var(--sab)); transform: translateX(-50%);
  width: min(440px, calc(100vw - 80px)); background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,.45); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.inspector[hidden] { display: none; }
.insp-head { display: flex; align-items: center; gap: 10px; padding: 10px 10px 8px 14px; border-bottom: 1px solid var(--line); }
.insp-head .swatch { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.insp-title { flex: 1; min-width: 0; line-height: 1.25; }
.insp-title b { display: block; }
.insp-title small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.insp-body { padding: 10px 14px 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 18px; font-size: 12.5px; max-height: 40vh; overflow-y: auto; }
.kv { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); }
.kv b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; text-align: right; }
.gene { grid-column: span 1; }
.gene .bar { height: 4px; border-radius: 2px; background: var(--line); margin-top: 2px; overflow: hidden; }
.gene .bar span { display: block; height: 100%; }
.insp-sep { grid-column: 1 / -1; height: 1px; background: var(--line); margin: 6px 0; }
.dead-note { grid-column: 1 / -1; color: var(--pred); font-weight: 600; }

/* ---------- Toast ---------- */
.toast { position: fixed; z-index: 20; left: 50%; top: calc(58px + var(--sat)); transform: translate(-50%, -10px); opacity: 0;
  pointer-events: none; transition: .25s; background: var(--accent); color: var(--accent-ink); padding: 7px 14px; border-radius: 999px; font-weight: 600; font-size: 13px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  :root { --pw-left: min(330px, calc(100vw - 44px)); --pw-right: min(360px, calc(100vw - 44px)); }
  .hud { top: calc(8px + var(--sat)); gap: 6px; padding-right: 10px; max-width: calc(100vw - 84px); }
  .hud-stats { gap: 6px; font-size: 12px; }
  .hud-extra { display: none; }
  .panel-tab { width: 28px; padding: 12px 0; }
  .inspector { width: calc(100vw - 72px); }
  body.open-right .panel.left .panel-tab, body.open-left .panel.right .panel-tab { opacity: 0; pointer-events: none; }
  .insp-body { gap: 3px 12px; font-size: 12px; max-height: 34vh; }
  .insp-title b { font-size: 13px; }
}
@media (max-width: 380px) {
  .hud .sep:first-of-type, .hud-stats > span:first-child { display: none; }
}
@media (prefers-reduced-motion: reduce) { .panel, .chev, .toast { transition: none; } }
