/* ==========================================================================
   Mappe Posti Barca — stile ispirato al linguaggio visivo iOS/iPadOS:
   superfici bianche translucide, palette di sistema, angoli continui,
   grandi titoli, liste raggruppate. I colori di stato restano quelli
   semantici di iOS (verde/rosso/arancio) e non vengono mai riusati altrove.
   ========================================================================== */

:root {
  --color-bg: #f2f2f7;
  --color-surface: #ffffff;
  --color-surface-2: #ffffff;
  --color-header: #ffffff;
  --color-header-text: #000000;
  --color-text: #000000;
  --color-muted: rgba(60, 60, 67, 0.6);
  --color-border: rgba(60, 60, 67, 0.18);
  --color-separator: rgba(60, 60, 67, 0.29);
  --color-accent: #007aff;
  --color-accent-dark: #0060df;
  --color-fill: rgba(120, 120, 128, 0.12);

  --stato-libero: #34c759;
  --stato-libero-bg: rgba(52, 199, 89, 0.15);
  --stato-occupato: #ff3b30;
  --stato-occupato-bg: rgba(255, 59, 48, 0.13);
  --stato-manutenzione: #ff9500;
  --stato-manutenzione-bg: rgba(255, 149, 0, 0.16);

  --font-display: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text',
    'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --font-ui: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI',
    Roboto, Helvetica, Arial, sans-serif;

  --radius: 12px;
  --radius-lg: 20px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-float: 0 4px 14px rgba(0, 0, 0, 0.16);
  --blur-bg: rgba(255, 255, 255, 0.78);
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #000000;
    --color-surface: #1c1c1e;
    --color-surface-2: #2c2c2e;
    --color-header: #1c1c1e;
    --color-header-text: #ffffff;
    --color-text: #ffffff;
    --color-muted: rgba(235, 235, 245, 0.6);
    --color-border: rgba(84, 84, 88, 0.5);
    --color-separator: rgba(84, 84, 88, 0.6);
    --color-accent: #0a84ff;
    --color-accent-dark: #409cff;
    --color-fill: rgba(120, 120, 128, 0.24);

    --stato-libero: #30d158;
    --stato-libero-bg: rgba(48, 209, 88, 0.18);
    --stato-occupato: #ff453a;
    --stato-occupato-bg: rgba(255, 69, 58, 0.18);
    --stato-manutenzione: #ff9f0a;
    --stato-manutenzione-bg: rgba(255, 159, 10, 0.2);

    --shadow: 0 8px 24px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
    --blur-bg: rgba(28, 28, 30, 0.78);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
}

html { scroll-padding-top: env(safe-area-inset-top, 0px); }

body {
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  color: var(--color-text);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; }

button, .dock-btn, .icon-btn, .btn {
  -webkit-tap-highlight-color: transparent;
}
button:active, .dock-btn:active, .icon-btn:active, .btn:active {
  transform: scale(0.97);
}

::selection { background: var(--color-accent); color: #fff; }

/* ---------- Intestazione ---------- */

.topbar {
  background: var(--blur-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  color: var(--color-text);
  padding: calc(16px + env(safe-area-inset-top, 0px)) 20px 16px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  border-bottom: 0.5px solid var(--color-separator);
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.7rem;
  letter-spacing: -0.02em;
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.topbar .subtitle {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--color-muted);
  letter-spacing: 0;
}

.legend {
  display: flex;
  gap: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  flex-wrap: wrap;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--color-fill);
  padding: 5px 10px 5px 8px;
  border-radius: 20px;
  color: var(--color-text);
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.legend-dot.libero { background: var(--stato-libero); }
.legend-dot.occupato { background: var(--stato-occupato); }
.legend-dot.manutenzione { background: var(--stato-manutenzione); }

/* ---------- Layout principale ---------- */

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

.dock-list {
  width: 230px;
  flex-shrink: 0;
  background: transparent;
  padding: 16px 12px;
  overflow-y: auto;
}

.dock-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  margin-bottom: 2px;
  background: var(--color-surface);
  border: none;
  border-radius: 0;
  cursor: pointer;
  color: var(--color-text);
  font-size: 0.95rem;
  font-weight: 500;
  position: relative;
  transition: background 0.15s ease;
}

.dock-list .dock-btn:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.dock-list .dock-btn:last-child { border-radius: 0 0 var(--radius) var(--radius); }
.dock-list .dock-btn:only-child { border-radius: var(--radius); }

.dock-btn:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 14px;
  right: 0;
  bottom: 0;
  height: 0.5px;
  background: var(--color-separator);
}

.dock-btn .n {
  font-family: var(--font-display);
  color: #fff;
  background: var(--color-accent);
  font-weight: 600;
  font-size: 0.75rem;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.dock-btn:hover { background: var(--color-fill); }

.dock-btn.active { background: var(--color-fill); }
.dock-btn.active { color: var(--color-accent); font-weight: 600; }
.dock-btn.active .n { background: var(--color-accent); }

.map-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

.map-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 0.5px solid var(--color-separator);
  background: var(--blur-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  gap: 12px;
  flex-wrap: wrap;
  position: relative;
  z-index: 5;
}

.map-toolbar h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  margin: 0;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.zoom-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  position: absolute;
  right: 20px;
  bottom: 24px;
  z-index: 15;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--blur-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  box-shadow: var(--shadow-float);
  cursor: pointer;
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1;
  color: var(--color-accent);
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover { background: var(--color-surface); }

.map-toolbar .zoom-controls {
  flex-direction: row;
  position: static;
}
.map-toolbar .icon-btn {
  width: 32px;
  height: 32px;
  font-size: 1rem;
  box-shadow: none;
  background: var(--color-fill);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

.map-viewport {
  flex: 1;
  overflow: hidden;
  position: relative;
  background: var(--color-bg);
  cursor: grab;
}

.map-viewport.panning { cursor: grabbing; }

.map-canvas-wrap {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

.map-canvas-wrap canvas {
  display: block;
  box-shadow: var(--shadow);
  border-radius: 10px;
  background: #fff;
}

.map-canvas-wrap svg {
  position: absolute;
  top: 0;
  left: 0;
}

.berth-poly {
  stroke-width: 2;
  cursor: pointer;
  transition: opacity 0.1s ease;
}

.berth-poly.libero { fill: var(--stato-libero-bg); stroke: var(--stato-libero); }
.berth-poly.occupato { fill: var(--stato-occupato-bg); stroke: var(--stato-occupato); }
.berth-poly.manutenzione { fill: var(--stato-manutenzione-bg); stroke: var(--stato-manutenzione); }

.berth-poly:hover { opacity: 0.7; }

.berth-poly.selected {
  stroke-width: 3;
  filter: drop-shadow(0 0 3px rgba(0,0,0,0.35));
}

.berth-label {
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 600;
  fill: var(--color-text);
  pointer-events: none;
  text-anchor: middle;
  paint-order: stroke;
  stroke: #fff;
  stroke-width: 3px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--color-muted);
  max-width: 320px;
  padding: 30px;
}

.empty-state h3 {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--color-text);
}

/* ---------- Pannello dettaglio posto barca (bottom sheet stile iOS) ---------- */

.berth-panel {
  position: fixed;
  left: 50%;
  bottom: 16px;
  width: min(360px, calc(100% - 32px));
  max-height: 70vh;
  background: var(--blur-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-float);
  padding: 22px 22px 26px;
  transform: translate(-50%, calc(100% + 32px));
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 40;
  overflow-y: auto;
  margin-bottom: env(safe-area-inset-bottom, 0px);
}

.berth-panel::before {
  content: '';
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 5px;
  border-radius: 3px;
  background: var(--color-border);
}

.berth-panel.open { transform: translate(-50%, 0); }

.berth-panel .close-btn {
  position: absolute;
  top: 14px;
  right: 16px;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-fill);
  font-size: 1rem;
  cursor: pointer;
  color: var(--color-muted);
}

.berth-panel h3 {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 10px 0 12px;
}

.stato-badge {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 700;
}

.stato-badge.libero { background: var(--stato-libero-bg); color: var(--stato-libero); }
.stato-badge.occupato { background: var(--stato-occupato-bg); color: var(--stato-occupato); }
.stato-badge.manutenzione { background: var(--stato-manutenzione-bg); color: var(--stato-manutenzione); }

.berth-panel dl { margin-top: 16px; }

.berth-panel dt {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  color: var(--color-muted);
  margin-top: 14px;
}

.berth-panel dd {
  margin: 3px 0 0;
  font-size: 1rem;
}

/* ---------- Pannello statistiche ---------- */

.stats-panel { width: min(420px, calc(100% - 32px)); }

.stats-updated {
  margin: 0 0 16px;
  font-size: 0.78rem;
  color: var(--color-muted);
}

.stats-totals {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 22px;
}

.stats-card {
  background: var(--color-fill);
  border-radius: 14px;
  padding: 12px 10px;
  text-align: center;
}

.stats-card .num {
  font-family: var(--font-display);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  display: block;
}

.stats-card .lbl {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stats-card.libero .num { color: var(--stato-libero); }
.stats-card.occupato .num { color: var(--stato-occupato); }
.stats-card.manutenzione .num { color: var(--stato-manutenzione); }

.stats-per-map { display: flex; flex-direction: column; gap: 14px; }

.stats-map-row .row-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}

.stats-map-row .map-name { font-weight: 600; font-size: 0.92rem; }
.stats-map-row .map-count { font-size: 0.78rem; color: var(--color-muted); }

.stats-bar {
  display: flex;
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--color-fill);
}

.stats-bar span { height: 100%; }
.stats-bar .b-libero { background: var(--stato-libero); }
.stats-bar .b-occupato { background: var(--stato-occupato); }
.stats-bar .b-manutenzione { background: var(--stato-manutenzione); }

@media (max-width: 720px) {
  .layout { flex-direction: column; }
  .dock-list {
    width: 100%;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 12px 14px;
  }
  .dock-btn {
    flex-shrink: 0;
    width: auto;
    border-radius: 20px !important;
    background: var(--color-fill);
    padding: 9px 14px;
  }
  .dock-btn.active { background: var(--color-accent); color: #fff; }
  .dock-btn.active .n { background: rgba(255,255,255,0.3); }
  .dock-btn::after { display: none; }
  .berth-panel { width: calc(100% - 24px); bottom: 12px; }
}

/* ==========================================================================
   Admin
   ========================================================================== */

.admin-body { background: var(--color-bg); }

.login-screen {
  max-width: 340px;
  margin: 14vh auto;
  background: var(--color-surface);
  padding: 32px 28px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.login-screen h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: -0.01em;
  margin-top: 0;
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--color-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 11px 12px;
  border: none;
  border-radius: 10px;
  font-family: inherit;
  font-size: 0.95rem;
  background: var(--color-fill);
  color: var(--color-text);
  transition: box-shadow 0.15s ease;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.35);
  background: var(--color-surface);
}

.field textarea { resize: vertical; min-height: 60px; }

.btn {
  display: inline-block;
  padding: 10px 18px;
  border-radius: 980px;
  border: none;
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 600;
  transition: opacity 0.12s ease;
}

.btn-primary {
  background: var(--color-accent);
  color: #fff;
}
.btn-primary:hover { background: var(--color-accent-dark); }

.btn-accent {
  background: var(--color-accent);
  color: #fff;
}
.btn-accent:hover { background: var(--color-accent-dark); }

.btn-ghost {
  background: var(--color-fill);
  color: var(--color-accent);
}
.btn-ghost:hover { background: rgba(0, 122, 255, 0.14); }

.btn-danger {
  background: var(--stato-occupato-bg);
  color: var(--stato-occupato);
}
.btn-danger:hover { background: rgba(255, 59, 48, 0.22); }

.error-msg {
  color: var(--stato-occupato);
  font-size: 0.85rem;
  font-weight: 500;
  margin-top: 6px;
}

.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: rgba(28, 28, 30, 0.9);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  color: #fff;
  padding: 12px 22px;
  border-radius: 980px;
  font-size: 0.88rem;
  font-weight: 500;
  box-shadow: var(--shadow-float);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.admin-layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

.admin-sidebar {
  width: 300px;
  flex-shrink: 0;
  background: transparent;
  padding: 18px;
  overflow-y: auto;
}

.admin-sidebar h2 {
  font-family: var(--font-display);
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  margin: 4px 0 12px 6px;
}

.map-card {
  background: var(--color-surface);
  border: none;
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

.map-card.active { box-shadow: 0 0 0 2px var(--color-accent); }

.map-card .name-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.map-card input[type="text"] {
  flex: 1;
  padding: 7px 9px;
  border: none;
  border-radius: 8px;
  background: var(--color-fill);
  font-size: 0.88rem;
  font-family: inherit;
  color: var(--color-text);
}

.map-card .row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.map-card .row-actions .btn {
  padding: 7px 12px;
  font-size: 0.8rem;
}

.map-card .file-status {
  font-size: 0.76rem;
  font-weight: 500;
  color: var(--color-muted);
  margin: 2px 0 8px;
}

.admin-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.admin-toolbar {
  padding: 12px 20px;
  border-bottom: 0.5px solid var(--color-separator);
  background: var(--blur-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.admin-toolbar h2 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.01em;
  margin: 0;
}

.admin-toolbar .hint {
  font-size: 0.82rem;
  color: var(--color-muted);
}

.admin-content {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.admin-map-stage {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--color-bg);
}

.admin-content .zoom-controls {
  right: 20px;
  bottom: 24px;
}

.berth-form-panel {
  width: 320px;
  flex-shrink: 0;
  background: var(--color-surface);
  border-left: 0.5px solid var(--color-separator);
  padding: 20px;
  overflow-y: auto;
}

.berth-form-panel h3 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.2rem;
  letter-spacing: -0.01em;
  margin-top: 0;
}

.berth-list {
  margin-top: 20px;
  border-top: 0.5px solid var(--color-separator);
  padding-top: 14px;
}

.berth-list h3 {
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
}

.berth-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
}

.berth-list-item:hover { background: var(--color-fill); }
.berth-list-item.active { background: rgba(0, 122, 255, 0.12); color: var(--color-accent); font-weight: 600; }

.vertex-handle {
  fill: var(--color-accent);
  stroke: #fff;
  stroke-width: 1.5;
  cursor: move;
}

.draft-line {
  stroke: var(--color-accent-dark);
  stroke-width: 2;
  stroke-dasharray: 5 4;
  fill: rgba(0, 122, 255, 0.12);
}
