/* ===== Token di design (modifica qui colori e spaziature) ===== */
:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #f7f6f2;
  --line: #dedad1;
  --text: #22251f;
  --muted: #6c6f66;
  --accent: #1f6f5c;
  --accent-soft: #dcece6;
  --danger: #b23a2e;
  --warn: #b77a12;
  --radius: 12px;
  --gap: 12px;
  --topbar-h: 52px;
  --panel-w: 340px;
  --shadow: 0 6px 24px rgba(20, 24, 18, .12);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17191a; --surface: #212426; --surface-2: #1b1e1f; --line: #34383a;
    --text: #e9ebe6; --muted: #9aa097; --accent: #5cc0a3; --accent-soft: #23423a;
    --shadow: 0 6px 24px rgba(0, 0, 0, .45); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17191a; --surface: #212426; --surface-2: #1b1e1f; --line: #34383a;
  --text: #e9ebe6; --muted: #9aa097; --accent: #5cc0a3; --accent-soft: #23423a;
  --shadow: 0 6px 24px rgba(0, 0, 0, .45); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.4 var(--font); color: var(--text); background: var(--bg);
  overflow: hidden; -webkit-tap-highlight-color: transparent; overscroll-behavior: none;
}
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; min-height: 40px; width: 100%;
}
input[type="color"] { padding: 2px; width: 48px; }
input[type="checkbox"] { width: 20px; min-height: 20px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; min-height: 40px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: default; }
.icon-btn {
  width: 44px; height: 44px; border: 0; background: transparent; border-radius: 10px;
  font-size: 22px; cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: .35; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
.topbar {
  height: calc(var(--topbar-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 10px 0;
  display: flex; align-items: center; gap: 8px; background: var(--surface);
  border-bottom: 1px solid var(--line); flex: none;
}
.brand { display: flex; gap: 8px; align-items: center; font-weight: 700; font-size: 18px; }
.spacer { flex: 1; }
.project-name {
  border: 0; background: transparent; font-weight: 600; min-width: 0; width: 180px; flex: 0 1 auto;
}
.seg { display: flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; min-height: 36px; }
.seg button.active { background: var(--accent); color: #fff; }

/* ===== Progetti ===== */
.projects-main { flex: 1; overflow: auto; padding: 16px; }
.projects-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; max-width: 1100px; margin: 0 auto; }
.project-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.project-card .thumb { aspect-ratio: 4/3; background: var(--surface-2) center/cover no-repeat; cursor: pointer; display: grid; place-items: center; color: var(--muted); }
.project-card .meta { padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; }
.project-card .meta b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-card .meta small { color: var(--muted); }
.project-card .actions { display: flex; gap: 4px; padding: 0 8px 10px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); margin-top: 60px; }
.storage-info { text-align: center; color: var(--muted); font-size: 13px; margin-top: 24px; }

/* ===== Editor ===== */
.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.stage { flex: 1; position: relative; overflow: hidden; background: var(--surface-2); touch-action: none; }
#plan-canvas, #three-host, #three-host canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

.toolbar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 14px; display: flex; gap: 2px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 4px;
  box-shadow: var(--shadow); max-width: calc(100% - 16px); overflow-x: auto; scrollbar-width: none;
}
.toolbar button {
  border: 0; background: transparent; border-radius: 10px; min-width: 56px; height: 52px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; flex: none;
}
.toolbar button span { font-size: 19px; line-height: 1; }
.toolbar button small { font-size: 11px; color: var(--muted); margin-top: 3px; }
.toolbar button.active { background: var(--accent-soft); }
.toolbar button.active small { color: var(--accent); font-weight: 600; }

.hint {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 5;
  background: var(--text); color: var(--bg); padding: 8px 10px 8px 14px; border-radius: 12px;
  display: flex; gap: 10px; align-items: center; max-width: calc(100% - 20px); box-shadow: var(--shadow); font-size: 14px;
}
.hint .btn { background: transparent; color: inherit; border-color: currentColor; }
.toast {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 84px; z-index: 6;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; max-width: 90%;
}

.joystick {
  position: absolute; left: 24px; bottom: 90px; width: 120px; height: 120px; border-radius: 50%;
  background: rgba(127, 127, 127, .22); border: 2px solid rgba(127, 127, 127, .4); touch-action: none; z-index: 4;
}
.joystick .knob { position: absolute; left: 50%; top: 50%; width: 50px; height: 50px; margin: -25px 0 0 -25px; border-radius: 50%; background: rgba(255, 255, 255, .75); }

/* ===== Pannello ===== */
.panel {
  width: var(--panel-w); background: var(--surface); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0; flex: none;
}
.panel-handle { display: none; }
.tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 12px 4px; cursor: pointer; border-bottom: 2px solid transparent; font-size: 14px; }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.tab-body { flex: 1; overflow-y: auto; padding: 14px; -webkit-overflow-scrolling: touch; }

.section { margin-bottom: 18px; }
.section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.row > label { flex: 0 0 42%; font-size: 14px; color: var(--muted); }
.row > .grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 3px; }
.field span { font-size: 12px; color: var(--muted); }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13px; }
.warn { color: var(--warn); font-size: 13px; }

.list-item {
  display: flex; align-items: center; gap: 10px; padding: 10px; border: 1px solid var(--line);
  border-radius: 10px; margin-bottom: 6px; cursor: pointer; background: var(--surface);
}
.list-item.selected { border-color: var(--accent); background: var(--accent-soft); }
.list-item .swatch { width: 18px; height: 18px; border-radius: 5px; flex: none; border: 1px solid var(--line); }
.list-item .grow { flex: 1; min-width: 0; }
.list-item small { color: var(--muted); display: block; }

.catalog-filter { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 10px; scrollbar-width: none; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 5px 12px; cursor: pointer; white-space: nowrap; font-size: 13px; }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.catalog { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.cat-item {
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); cursor: pointer;
  padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; position: relative;
}
.cat-item:hover { border-color: var(--accent); }
.cat-item img { width: 100%; aspect-ratio: 1; object-fit: contain; }
.cat-item b { font-size: 12px; font-weight: 500; line-height: 1.2; }
.cat-item small { font-size: 11px; color: var(--muted); }
.cat-item .del { position: absolute; top: 2px; right: 2px; border: 0; background: var(--surface); border-radius: 6px; width: 24px; height: 24px; cursor: pointer; font-size: 13px; }

/* ===== Dialog ===== */
dialog {
  border: 0; border-radius: 16px; padding: 0; background: var(--surface); color: var(--text);
  width: min(460px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog form { padding: 18px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100dvh - 24px); }
dialog h2 { margin: 0; font-size: 18px; }
#dialog-body { overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.preview-img { width: 100%; max-height: 220px; object-fit: contain; background: var(--surface-2); border-radius: 10px; }

/* ===== Smartphone: pannello come foglio dal basso ===== */
@media (max-width: 860px) {
  .project-name { width: 0; flex: 1 1 60px; }
  .seg button { padding: 6px 8px; font-size: 14px; }
  .workspace { flex-direction: column; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; z-index: 8;
    border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
    box-shadow: var(--shadow); transition: height .2s ease; padding-bottom: env(safe-area-inset-bottom);
  }
  .panel[data-state="collapsed"] { height: 92px; }
  .panel[data-state="half"] { height: 44%; }
  .panel[data-state="full"] { height: 88%; }
  .panel[data-state="collapsed"] .tab-body { display: none; }
  .panel-handle { display: flex; justify-content: center; padding: 8px 0 4px; cursor: grab; touch-action: none; flex: none; }
  .panel-handle span { width: 44px; height: 5px; border-radius: 3px; background: var(--line); }
  .stage { flex: 1; }
  .toolbar { bottom: calc(var(--panel-offset, 92px) + 10px); }
  .toast { bottom: calc(var(--panel-offset, 92px) + 76px); }
  .joystick { bottom: calc(var(--panel-offset, 92px) + 20px); }
}
@media (max-width: 420px) {
  .seg button { padding: 6px 6px; font-size: 13px; }
  .toolbar button { min-width: 46px; height: 48px; padding: 0 2px; }
  .toolbar button small { font-size: 10px; }
  .topbar { gap: 4px; padding-left: 4px; padding-right: 4px; }
  .icon-btn { width: 38px; }
}
