:root {
  --primary: #8B4513; --primary-dark: #6B340D; --accent: #1565c0; --danger: #c62828;
  --bg: #f4f1ec; --panel: #ffffff; --line: #e3ddd3; --text: #2A1810; --muted: #7a6a5e;
  --radius: 10px; --shadow: 0 2px 8px rgba(0,0,0,.12);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font-family: var(--font); color: var(--text); background: var(--bg); font-size: 14px; }
body { display: flex; flex-direction: column; overflow: hidden; }
a { color: var(--accent); }
code { font-family: var(--mono); font-size: 12px; background: #f1ede6; padding: 1px 5px; border-radius: 4px; }

/* Topbar */
.topbar { display: flex; align-items: center; gap: 18px; padding: 8px 16px; background: var(--primary); color: #fff; box-shadow: var(--shadow); z-index: 20; }
.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand span { font-weight: 400; opacity: .8; margin-left: 6px; font-size: 13px; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tabs a { color: #fff; text-decoration: none; padding: 6px 12px; border-radius: 6px; opacity: .85; font-weight: 500; }
.tabs a:hover { background: rgba(255,255,255,.12); opacity: 1; }
.tabs a.active { background: rgba(255,255,255,.22); opacity: 1; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.project-pick { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.project-pick select { min-width: 160px; padding: 5px 8px; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 480px 1fr; min-height: 0; }
.panel { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 14px 16px 40px; }
.mapwrap { position: relative; }
#map { position: absolute; inset: 0; }
.draw-hint { display: none; position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: var(--danger); color: #fff; padding: 6px 14px; border-radius: 20px; font-size: 13px; z-index: 1000; box-shadow: var(--shadow); }
body.drawing .draw-hint { display: block; }
body.drawing #map { cursor: crosshair; }

/* Panel widgets */
.panel h2 { font-size: 17px; margin: 4px 0 12px; display: flex; align-items: center; gap: 10px; }
.panel h2 .spacer { flex: 1; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.toolbar .input { flex: 1; min-width: 120px; }
.row { display: flex; align-items: center; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.gap { gap: 8px; }
.wrap { flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px 12px; }
.muted { color: var(--muted); }
.hint { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }
.hint.error { color: var(--danger); }
.mono { font-family: var(--mono); font-size: 12px; }
.empty { padding: 24px 8px; text-align: center; color: var(--muted); }

.btn { border: 1px solid var(--line); background: #fff; color: var(--text); padding: 7px 12px; border-radius: 7px; cursor: pointer; font: inherit; font-size: 13px; font-weight: 500; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: #f7f4ef; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,.5); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.icon { padding: 2px 8px; line-height: 1; font-size: 16px; }
.btn[disabled] { opacity: .5; cursor: default; }

.input { width: 100%; border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px; font: inherit; font-size: 13px; background: #fff; color: var(--text); }
.input:focus { outline: 2px solid rgba(21,101,192,.25); border-color: var(--accent); }
textarea.input { resize: vertical; }
.field { display: block; margin-bottom: 10px; }
.field-label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: .03em; }
.check { display: inline-flex; align-items: center; gap: 6px; margin: 4px 12px 4px 0; cursor: pointer; }

.section { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; background: #fff; }
.section > summary { cursor: pointer; padding: 9px 12px; font-weight: 600; list-style: none; display: flex; align-items: center; }
.section > summary::before { content: "▸"; margin-right: 8px; color: var(--muted); transition: transform .15s; }
.section[open] > summary::before { transform: rotate(90deg); }
.section > :not(summary) { padding: 0 12px; }
.section > :last-child { padding-bottom: 10px; }

.loc-row { display: grid; grid-template-columns: 56px 1fr 30px; gap: 6px; margin-bottom: 6px; align-items: start; }
.codes { display: flex; flex-wrap: wrap; gap: 6px; }
.code-chip { display: inline-flex; align-items: center; gap: 5px; border: 1px solid var(--line); border-radius: 16px; padding: 3px 9px 3px 6px; font-size: 12px; cursor: pointer; background: #fff; }
.code-chip:has(input:checked) { background: #e8f0fb; border-color: var(--accent); }
.code-chip code { background: transparent; color: var(--muted); padding: 0; }

/* Liste */
.list { display: flex; flex-direction: column; gap: 4px; }
.item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: #fff; }
.item:hover { border-color: var(--accent); }
.item.selected { border-color: var(--danger); background: #fff5f5; }
.item .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; border: 1px solid rgba(0,0,0,.2); }
.item .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.item .sub { font-size: 12px; color: var(--muted); white-space: nowrap; }
.item.inactive { opacity: .55; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 10px; background: #f1ede6; color: var(--muted); }
.badge.osm { background: #e3f2fd; color: #0d47a1; }
.badge.custom { background: #fce4ec; color: #880e4f; }

table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th, .tbl td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th { font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; }
.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: middle; border: 1px solid rgba(0,0,0,.15); }

.card { border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 8px; background: #fbfaf8; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; font-weight: 600; }

.stat { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; margin-bottom: 12px; }
.stat > div { border: 1px solid var(--line); border-radius: 8px; padding: 10px; background: #fff; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 12px; color: var(--muted); }

.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(to top, #fff 75%, transparent); padding: 12px 0 4px; display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }

/* Percorsi: tappe numerate sulla mappa e nell'editor */
.stop-marker { background: #E67E00; color: #fff; border: 2px solid #fff; border-radius: 50%; width: 24px; height: 24px; line-height: 20px; text-align: center; font-weight: 700; font-size: 12px; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.stop-marker.selected { background: var(--danger); transform: scale(1.2); }
.stops { display: flex; flex-direction: column; gap: 6px; }
.stop { border: 1px solid var(--line); border-radius: 8px; background: #fbfaf8; padding: 8px 10px; }
.stop.selected { border-color: var(--danger); }
.stop-head { display: flex; align-items: center; gap: 8px; }
.stop-head .num { width: 22px; height: 22px; border-radius: 50%; background: #E67E00; color: #fff; font-weight: 700; font-size: 12px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.stop-head .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; cursor: pointer; }
.stop-body { margin-top: 8px; display: grid; grid-template-columns: 1fr 110px; gap: 8px 12px; align-items: start; }
.stop-leg { font-size: 12px; color: var(--muted); padding: 0 0 2px 30px; }
.stop-leg.error { color: var(--danger); }
.picker { max-height: 220px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; margin-top: 6px; }
.picker .item { border: 0; border-bottom: 1px solid var(--line); border-radius: 0; padding: 6px 10px; }
.status-ok { color: #2e7d32; } .status-partial { color: #ef6c00; } .status-failed, .status-pending { color: var(--danger); }

/* Toast e dialog */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 3000; max-width: 420px; }
.toast { background: #333; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13px; white-space: pre-wrap; animation: fadein .2s; }
.toast-ok { background: #2e7d32; } .toast-error { background: var(--danger); } .toast.out { opacity: 0; transition: opacity .3s; }
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 2500; }
.dialog { background: #fff; padding: 20px 22px; border-radius: 12px; min-width: 340px; max-width: 520px; box-shadow: 0 8px 30px rgba(0,0,0,.3); }
.dialog h3 { margin: 0 0 10px; } .dialog p { margin: 0 0 16px; white-space: pre-wrap; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr 40vh; }
  .panel { border-right: 0; border-bottom: 1px solid var(--line); }
  .tabs a { padding: 6px 8px; }
}

/* Modalità ospite */
body.guest .admin-only { display: none; }
.tabs a[hidden] { display: none; }
