:root {
  --bg: #14110f;
  --panel: rgba(28, 24, 21, 0.92);
  --line: rgba(232, 214, 190, 0.12);
  --ink: #efe6d8;
  --dim: #a89a88;
  --accent: #c99a63;
  --accent2: #8c5a2b;
  --serif: "Iowan Old Style", "Palatino", "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  --pw: 360px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--sans); overflow: hidden; overscroll-behavior: none; }
#app { position: fixed; inset: 0; }
#stage { position: absolute; inset: 0; }
#view { width: 100%; height: 100%; touch-action: none; display: block; }

/* top bar */
#topbar { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 14px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px; pointer-events: none; z-index: 5; }
#topbar > * { pointer-events: auto; }
.brand { font-family: var(--serif); font-size: 22px; letter-spacing: .5px; display: flex; align-items: center; gap: 10px; text-shadow: 0 1px 8px #000; }
.brand em { color: var(--accent); font-style: italic; }
.mark { width: 26px; height: 26px; border: 1.5px solid var(--accent); border-radius: 50%; position: relative; }
.mark::after { content: ""; position: absolute; inset: 4px; border: 1.5px solid var(--accent); }
.seg { display: flex; background: rgba(0,0,0,.45); border: 1px solid var(--line); border-radius: 12px; padding: 3px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.seg button { background: none; border: 0; color: var(--dim); font: 600 15px var(--sans); padding: 9px 16px; border-radius: 9px; }
.seg button.on { background: var(--accent); color: #1b130b; }
.tools { margin-left: auto; display: flex; gap: 8px; margin-right: calc(var(--pw) + 4px); transition: margin .25s; }
body.panel-closed .tools { margin-right: 0; }
.chip { background: rgba(0,0,0,.45); border: 1px solid var(--line); color: var(--ink); font: 600 14px var(--sans); padding: 10px 14px; border-radius: 11px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.chip:active { background: var(--accent2); }

/* panel */
#panel { position: absolute; top: 0; right: 0; bottom: 0; width: var(--pw); background: var(--panel); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; z-index: 6; transition: transform .25s ease; padding-top: env(safe-area-inset-top);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
body.panel-closed #panel { transform: translateX(100%); }
.panel-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.stat { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--serif); }
.stat span { color: var(--dim); font-size: 15px; }
.stat b { font-size: 26px; font-weight: 500; color: var(--accent); }
.toggles { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tg { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--dim); background: rgba(255,255,255,.04); border: 1px solid var(--line); padding: 7px 10px; border-radius: 10px; }
.tg input { accent-color: var(--accent); width: 18px; height: 18px; margin: 0; }
#sections { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px 0 20px; }
.sec { border-bottom: 1px solid var(--line); }
.sec > summary { list-style: none; padding: 15px 16px; font-family: var(--serif); font-size: 18px; display: flex; justify-content: space-between; align-items: center; cursor: pointer; }
.sec > summary::-webkit-details-marker { display: none; }
.sec > summary::after { content: "+"; color: var(--accent); font-size: 22px; font-family: var(--sans); }
.sec[open] > summary::after { content: "–"; }
.sec .count { font-family: var(--sans); font-size: 12px; color: var(--accent); margin-left: auto; margin-right: 10px; }
.sec-body { padding: 0 16px 12px; }
.sl { padding: 9px 0 6px; }
.sl-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 14px; }
.sl-top .lab { color: var(--ink); }
.sl-top .val { color: var(--accent); font-variant-numeric: tabular-nums; font-size: 13px; min-width: 52px; text-align: right; }
.sl.changed .lab::before { content: "● "; color: var(--accent); font-size: 10px; vertical-align: middle; }
.pair { display: grid; grid-template-columns: 26px 1fr; align-items: center; gap: 4px; }
.pair .side { font-size: 11px; color: var(--dim); }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 34px; background: transparent; margin: 0; touch-action: pan-y; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: linear-gradient(90deg, var(--line), rgba(201,154,99,.45) 50%, var(--line)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; border-radius: 50%; background: var(--ink); border: 3px solid var(--accent); margin-top: -11px; box-shadow: 0 2px 6px rgba(0,0,0,.5); }
input[type=range]::-moz-range-track { height: 4px; background: var(--line); }
input[type=range]::-moz-range-thumb { width: 22px; height: 22px; border-radius: 50%; background: var(--ink); border: 3px solid var(--accent); }
.hint { font-size: 12px; color: var(--dim); margin: 2px 0 8px; line-height: 1.4; }
.panel-foot { padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px); border-top: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.btn { background: var(--accent); color: #1b130b; border: 0; font: 600 14px var(--sans); padding: 11px 16px; border-radius: 11px; }
.btn.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.ver { font-size: 11px; color: var(--dim); }

/* loading + toast */
#loading { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; background: var(--bg); z-index: 20; transition: opacity .4s; font-family: var(--serif); color: var(--dim); }
#loading.hide { opacity: 0; pointer-events: none; }
.spin { width: 54px; height: 54px; margin: 0 auto 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: sp 1s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }
#toast { position: absolute; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translateX(-50%) translateY(20px); background: rgba(0,0,0,.8); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; transition: .25s; z-index: 30; pointer-events: none; }
#toast.show { opacity: 1; transform: translateX(-50%); }

/* iPad verticale / schermi stretti: pannello in basso */
@media (max-aspect-ratio: 1/1) {
  :root { --pw: 0px; }
  #panel { top: auto; left: 0; width: auto; height: 48%; border-left: 0; border-top: 1px solid var(--line); border-radius: 18px 18px 0 0; padding-top: 0; }
  body.panel-closed #panel { transform: translateY(100%); }
  .tools { margin-right: 0; }
}
@media (max-width: 560px) {
  .brand { font-size: 18px; } .chip { padding: 8px 10px; font-size: 13px; }
  .seg button { padding: 8px 11px; }
  #topbar { flex-wrap: wrap; }
}

/* lo spazio del 3D si adatta al pannello */
#stage { right: var(--pw); transition: right .25s; }
body.panel-closed #stage { right: 0; }
@media (max-aspect-ratio: 1/1) {
  #stage { right: 0; bottom: 48%; }
  body.panel-closed #stage { bottom: 0; }
}
