:root {
  --bg: #12161c;
  --surface: #1b2129;
  --surface-2: #252d38;
  --line: #333d4a;
  --text: #e8edf3;
  --muted: #9aa7b6;
  --accent: #4da3ff;
  --accent-ink: #06203d;
  --warn: #ffcf66;
  --error: #ff8a80;
  --radius: 10px;
  --panel-w: 320px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

#app {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel-w);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "header header"
    "stage  panel"
    "toolbar toolbar";
}

/* ---------- Header ---------- */
#header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
#header h1 { margin: 0; font-size: 16px; font-weight: 650; letter-spacing: .01em; }
#model-name { margin: 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
#btn-panel { display: none; }
.header-end { display: flex; align-items: center; gap: 8px; flex: none; }
#back-site {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 12px;
  color: var(--text); font-size: 14px; text-decoration: none; white-space: nowrap;
  border: 1px solid var(--line); border-radius: 8px;
}
#back-site:hover { border-color: var(--accent); }
#back-site:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Stage ---------- */
#stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  background: radial-gradient(ellipse at 50% 35%, #2b3440 0%, #171c23 70%);
  overflow: hidden;
}
#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  outline: none;
}
#stage.dragover::after {
  content: "Rilascia qui il file GLB";
  position: absolute;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  background: rgba(18, 22, 28, .7);
  font-size: 18px;
}

#loader {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(18, 22, 28, .55);
  pointer-events: none;
}
.loader-box {
  width: min(340px, 80%);
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.loader-box p { margin: 0 0 10px; font-size: 14px; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .15s linear; }
#progress-bar.indeterminate { width: 35%; animation: slide 1.1s ease-in-out infinite alternate; }
@keyframes slide { from { margin-left: 0; } to { margin-left: 65%; } }

#status {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  max-width: min(92%, 560px);
  padding: 9px 14px;
  background: rgba(27, 33, 41, .94);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 14px;
  text-align: center;
}
#status.warn { border-color: var(--warn); color: var(--warn); }
#status.error { border-color: var(--error); color: var(--error); }

/* ---------- Panel ---------- */
#panel {
  grid-area: panel;
  min-height: 0;
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--line);
  padding: 4px 16px 16px;
}
#panel section { padding: 14px 0; border-bottom: 1px solid var(--line); }
#panel section:last-child { border-bottom: 0; }
#panel h2 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
}
.muted { color: var(--muted); margin: 0 0 8px; font-size: 14px; }
.sel-name { margin: 0 0 6px; font-size: 17px; font-weight: 650; overflow-wrap: anywhere; }
dl { margin: 0 0 10px; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4px 12px; font-size: 14px; }
dt { color: var(--muted); }
dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; margin-bottom: 8px; }
.row.between h2 { margin: 0; }
.link { color: var(--accent); font-size: 14px; }
#xr-support { white-space: pre-line; }

#component-list { list-style: none; margin: 0; padding: 0; }
#component-list li { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
#component-list input { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }
#component-list button {
  flex: 1;
  min-height: 36px;
  text-align: left;
  background: transparent;
  border-color: transparent;
  padding: 6px 8px;
  overflow-wrap: anywhere;
}
#component-list li.selected button { background: var(--surface-2); border-color: var(--accent); }
#component-list li.is-hidden button { color: var(--muted); text-decoration: line-through; }

/* ---------- Buttons ---------- */
button {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 44px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .45; cursor: not-allowed; }
button[aria-pressed="true"] { background: #1d3a5c; border-color: var(--accent); }
button.ghost { background: transparent; }
button.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
button.xr:not(:disabled) { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }

/* ---------- Toolbar ---------- */
#toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: thin;
}
#toolbar .spacer { flex: 1; min-width: 4px; }
#toolbar button { flex: none; }

/* ---------- Smartphone / tablet in verticale ---------- */
@media (max-width: 820px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "header" "stage" "toolbar";
  }
  #btn-panel { display: inline-block; }
  #header h1 { font-size: 15px; }
  #panel {
    grid-area: stage;
    align-self: end;
    z-index: 5;
    max-height: 62%;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, .45);
    transform: translateY(105%);
    transition: transform .2s ease;
    visibility: hidden;
  }
  #panel.open { transform: none; visibility: visible; }
  #status { bottom: 10px; }
  #toolbar { padding-left: 10px; padding-right: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- Overlay AR (smartphone / tablet) ---------- */
#xr-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: max(16px, env(safe-area-inset-top)) 12px max(18px, env(safe-area-inset-bottom));
  background: transparent;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#xr-overlay p {
  margin: 0;
  max-width: 92%;
  padding: 8px 14px;
  border-radius: 99px;
  background: rgba(18, 22, 28, .78);
  color: #fff;
  font-size: 14px;
  text-align: center;
}
#xr-selected { margin-top: 8px !important; margin-bottom: auto !important; font-weight: 650; }
#xr-buttons { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
#xr-buttons button { background: rgba(18, 22, 28, .82); border-color: rgba(255, 255, 255, .35); color: #fff; }
