/* ============================================================
   Studio Legale — Design moderno mobile-first
   ============================================================ */

:root {
  /* Palette */
  --primary: #1e40af;
  --primary-hover: #1e3a8a;
  --primary-soft: #dbeafe;
  --accent: #f97316;
  --bg: #f8fafc;
  --bg-alt: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --text-secondary: #475569;
  --muted: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --success: #047857;
  --success-soft: #d1fae5;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.08), 0 1px 2px -1px rgb(0 0 0 / 0.06);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.08), 0 2px 4px -2px rgb(0 0 0 / 0.04);
  --shadow-lg: 0 10px 25px -5px rgb(0 0 0 / 0.10), 0 8px 10px -6px rgb(0 0 0 / 0.06);
  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  /* Spacing */
  --topbar-h: 60px;
  --bottomnav-h: 64px;
  --container-px: 1rem;
  --container-pad: 1.25rem;
  /* Font */
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  --duration: 180ms;
  --easing: cubic-bezier(.2,.7,.2,1);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
  padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px));
}
@media (min-width: 900px) {
  body { padding-bottom: 0; }
}

a { color: var(--primary); text-decoration: none; transition: color var(--duration); }
a:hover { color: var(--primary-hover); }
a:active { opacity: .8; }

/* ============================================================
   Topbar
   ============================================================ */
.topbar {
  background: rgba(15, 23, 42, 0.97);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  color: white;
  padding: 0 1rem;
  padding-top: env(safe-area-inset-top, 0);
  height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  display: flex;
  align-items: center;
  gap: .8rem;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 1px 0 rgba(255,255,255,.06), 0 6px 24px -8px rgba(0,0,0,.25);
}
.topbar a { color: rgba(255,255,255,.92); text-decoration: none; }
.topbar a:hover { color: white; }

.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.05rem; letter-spacing: -.01em; }
.brand-mark { font-size: 1.3rem; }
.brand-text { white-space: nowrap; }
@media (max-width: 540px) {
  .brand-text { display: none; }
}

.mainnav { display: none; gap: .2rem; flex: 1; }
.mainnav a {
  padding: .5rem .85rem;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: .92rem;
  color: rgba(255,255,255,.78);
  transition: all var(--duration);
}
.mainnav a:hover { background: rgba(255,255,255,.08); color: white; }
.mainnav a.active { background: rgba(255,255,255,.14); color: white; }
@media (min-width: 900px) { .mainnav { display: flex; } }

.globalsearch {
  flex: 1;
  max-width: 380px;
  position: relative;
  display: none;
}
.globalsearch .search-ico {
  position: absolute;
  left: 12px; top: 50%;
  transform: translateY(-50%);
  color: rgba(255,255,255,.55);
  pointer-events: none;
}
.globalsearch input {
  width: 100%;
  padding: .55rem .85rem .55rem 2.25rem;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.18);
  background: rgba(255,255,255,.10);
  color: white;
  font-size: .9rem;
  transition: background var(--duration), border-color var(--duration);
}
.globalsearch input::placeholder { color: rgba(255,255,255,.55); }
.globalsearch input:focus { outline: none; background: white; color: var(--text); border-color: white; }
.globalsearch input:focus::placeholder { color: var(--muted); }
.globalsearch input:focus ~ .search-ico { color: var(--text); }
@media (min-width: 900px) { .globalsearch { display: block; } }

.usermenu { display: flex; gap: .4rem; margin-left: auto; }
.iconbtn {
  width: 38px; height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background var(--duration);
}
.iconbtn:hover { background: rgba(255,255,255,.10); }
.iconbtn svg { display: block; }

.burger {
  width: 40px; height: 40px;
  background: transparent;
  border: 0;
  color: white;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  padding: 0;
}
.burger:hover { background: rgba(255,255,255,.10); }
@media (min-width: 900px) { .burger { display: none; } }

/* ============================================================
   Drawer mobile
   ============================================================ */
.drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration);
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(86vw, 320px);
  background: white;
  z-index: 70;
  transform: translateX(-100%);
  transition: transform var(--duration) var(--easing);
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(0,0,0,.12);
  padding-top: env(safe-area-inset-top, 0);
}
.drawer.open { transform: translateX(0); }
.drawer-header {
  padding: 1.5rem 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
}
.drawer-header strong { display: block; font-size: 1.05rem; }
.drawer-header small { color: var(--muted); font-size: .85rem; }
.drawer-nav {
  flex: 1;
  display: flex; flex-direction: column;
  padding: .5rem;
  overflow-y: auto;
}
.drawer-nav a {
  display: flex; align-items: center; gap: .75rem;
  padding: .85rem 1rem;
  border-radius: var(--radius-sm);
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
  min-height: 44px;
}
.drawer-nav a span { width: 22px; text-align: center; }
.drawer-nav a:hover { background: var(--bg-alt); }
.drawer-nav a.active { background: var(--primary-soft); color: var(--primary); }
.drawer-nav hr { border: 0; border-top: 1px solid var(--border); margin: .5rem 0; }

/* ============================================================
   Bottom nav mobile
   ============================================================ */
.bottomnav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(255,255,255,0.96);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  z-index: 40;
  box-shadow: 0 -4px 16px -4px rgba(0,0,0,.06);
}
.bottomnav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  font-size: .7rem;
  font-weight: 500;
  padding: .3rem 0;
  text-decoration: none;
  transition: color var(--duration);
}
.bottomnav a.active { color: var(--primary); }
.bottomnav a svg { display: block; }
.bottomnav-center {
  position: relative;
}
.bottomnav-center svg {
  background: var(--primary);
  color: white;
  border-radius: 50%;
  padding: 6px;
  margin-top: -10px;
  box-shadow: 0 4px 14px rgba(30,64,175,.35);
}
.bottomnav-center.active svg { background: var(--primary-hover); }
@media (min-width: 900px) { .bottomnav { display: none; } }

/* ============================================================
   Container & layout
   ============================================================ */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: var(--container-pad) var(--container-px);
}
@media (min-width: 768px) {
  :root { --container-px: 1.5rem; --container-pad: 2rem; }
}

h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -.02em; margin: 0 0 1.25rem; }
h2 { font-size: 1.2rem; font-weight: 600; letter-spacing: -.01em; margin: 1.2rem 0 .5rem; }
h3 { font-size: 1rem; font-weight: 600; margin: 1rem 0 .3rem; color: var(--text-secondary); }
p { margin: 0 0 .8rem; }

.muted { color: var(--muted); font-size: .92rem; }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: .8rem;
  margin-bottom: 1.25rem;
}
.page-head h1 { margin: 0; }

/* ============================================================
   Card
   ============================================================ */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--duration);
}
.card:hover { box-shadow: var(--shadow); }

/* ============================================================
   Stats grid (dashboard)
   ============================================================ */
.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: .75rem;
  margin-bottom: 1.5rem;
}
@media (min-width: 640px) {
  .stats { grid-template-columns: repeat(4, 1fr); gap: 1rem; }
}
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.1rem;
  text-align: left;
  display: block;
  text-decoration: none;
  transition: all var(--duration);
  box-shadow: var(--shadow-sm);
}
.stat:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary-soft);
}
.stat-num { font-size: 1.85rem; font-weight: 700; color: var(--primary); letter-spacing: -.02em; line-height: 1.1; }
.stat-lbl { color: var(--muted); font-size: .8rem; font-weight: 500; margin-top: .25rem; }

/* ============================================================
   Tabs
   ============================================================ */
.tabs {
  display: flex; gap: .3rem; flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.25rem;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  padding: .65rem 1rem;
  color: var(--muted);
  font-size: .92rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  white-space: nowrap;
  transition: all var(--duration);
}
.tabs a:hover { color: var(--text-secondary); }
.tabs a.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }

/* ============================================================
   Search bar
   ============================================================ */
.searchbar {
  display: flex; gap: .5rem;
  margin-bottom: 1.25rem;
}
.searchbar input {
  flex: 1;
  padding: .65rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: white;
  font-size: .95rem;
}
.searchbar input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ============================================================
   Tabelle (desktop)
   ============================================================ */
.data {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  font-size: .92rem;
}
.data th, .data td {
  padding: .85rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.data th {
  background: var(--bg-alt);
  font-weight: 600;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}
.data tr:last-child td { border-bottom: none; }
.data tbody tr { transition: background var(--duration); }
.data tbody tr:hover { background: var(--bg-alt); }

/* Card-style su mobile: tabelle data diventano una lista di card */
@media (max-width: 640px) {
  .data, .data tbody, .data tr, .data td { display: block; width: 100%; }
  .data thead { display: none; }
  .data { border: 0; background: transparent; box-shadow: none; }
  .data tr {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: .75rem;
    padding: .75rem 1rem;
    box-shadow: var(--shadow-sm);
  }
  .data td {
    border: 0;
    padding: .35rem 0;
  }
  .data td:first-child {
    font-weight: 600;
    font-size: 1.02rem;
  }
  .data td:not(:first-child) {
    color: var(--muted);
    font-size: .88rem;
  }
}

/* ============================================================
   Liste
   ============================================================ */
ul.list { list-style: none; padding: 0; margin: 0; }
ul.list li {
  padding: .85rem 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: .4rem;
}
ul.list li:last-child { border-bottom: none; }
ul.list li > a:first-child { font-weight: 500; }

/* ============================================================
   Definition list (key-value)
   ============================================================ */
dl.kv {
  display: grid;
  grid-template-columns: minmax(140px, max-content) 1fr;
  gap: .55rem 1.25rem;
  margin: 0;
}
dl.kv dt { font-weight: 500; color: var(--muted); font-size: .85rem; align-self: start; }
dl.kv dd { margin: 0; font-size: .95rem; }
@media (max-width: 600px) {
  dl.kv { grid-template-columns: 1fr; gap: .15rem .5rem; }
  dl.kv dt { margin-top: .5rem; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; }
}

/* ============================================================
   Form
   ============================================================ */
input, select, textarea {
  width: 100%;
  padding: .65rem .85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  font-size: .95rem;
  background: white;
  color: var(--text);
  transition: border-color var(--duration), box-shadow var(--duration);
  min-height: 42px;
}
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
input[type="search"] { -webkit-appearance: none; }
textarea { resize: vertical; min-height: 80px; }
label {
  display: block;
  margin-bottom: .9rem;
  font-size: .92rem;
  font-weight: 500;
  color: var(--text-secondary);
}
label input, label select, label textarea { margin-top: .3rem; }
label.inline {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  margin-right: 1rem;
  font-weight: 500;
}
label.inline input { width: auto; min-height: 0; }

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  background: var(--card);
}
legend {
  font-weight: 600;
  padding: 0 .5rem;
  color: var(--primary);
  font-size: .9rem;
  letter-spacing: .01em;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .9rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .9rem; }
@media (max-width: 640px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: .5rem; }
}

.actions {
  display: flex; gap: .6rem; margin-top: 1.25rem; flex-wrap: wrap;
}
@media (max-width: 480px) {
  .actions { flex-direction: column; }
  .actions > * { width: 100%; }
}

/* ============================================================
   Bottoni
   ============================================================ */
.btn, .btn-primary, .btn-danger, .btn-small, button, .btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .65rem 1.15rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
  font: inherit;
  font-size: .92rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: all var(--duration);
  min-height: 42px;
  white-space: nowrap;
}
.btn:hover, button:hover { border-color: var(--border-strong); background: var(--bg-alt); transform: translateY(-1px); }
.btn:active, button:active { transform: translateY(0); }

.btn-primary, button.btn-primary, button[type="submit"]:not(.btn-danger):not(.btn):not(.btn-small) {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  box-shadow: 0 1px 2px rgba(30,64,175,.2);
}
.btn-primary:hover, button.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
  color: white;
  box-shadow: 0 4px 12px rgba(30,64,175,.25);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
}
.btn-danger:hover {
  background: #991b1b;
  border-color: #991b1b;
  color: white;
}

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg-alt); }

.btn-small {
  padding: .35rem .7rem;
  font-size: .85rem;
  min-height: 32px;
  border-radius: var(--radius-sm);
}

/* ============================================================
   Alert
   ============================================================ */
.alert {
  padding: .9rem 1.1rem;
  border-radius: var(--radius);
  margin: 1rem 0;
  font-size: .92rem;
  border-left: 4px solid;
}
.alert.error {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: var(--danger);
}
.alert.warn {
  background: var(--warn-soft);
  color: var(--warn);
  border-color: var(--warn);
}

/* ============================================================
   Badge
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: .2rem .65rem;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  background: var(--bg-alt);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.badge-aperta, .badge-da_fare, .badge-draft { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.badge-completata, .badge-confirmed { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge-sospesa, .badge-posticipata { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-archiviata, .badge-definita { background: var(--bg-alt); color: var(--muted); border-color: var(--border); }
.badge-rejected, .badge-annullata { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

/* ============================================================
   OCR pre / code
   ============================================================ */
pre.ocr {
  background: var(--bg-alt);
  padding: 1rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  font-size: .82rem;
  max-height: 400px;
  overflow: auto;
  border: 1px solid var(--border);
  line-height: 1.5;
}
code {
  background: var(--bg-alt);
  padding: .15em .4em;
  border-radius: 4px;
  font-size: .88em;
  font-family: 'SF Mono', Menlo, monospace;
}

/* ============================================================
   Login
   ============================================================ */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, var(--bg) 0%, var(--primary-soft) 100%);
  padding: 1rem;
}
.login-card {
  background: white;
  padding: 2.25rem 2rem;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 400px;
}
.login-card h1 {
  text-align: center;
  margin-bottom: .25rem;
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -.02em;
}
.login-card .muted { text-align: center; margin-bottom: 1.5rem; }
.login-card .btn-primary { width: 100%; margin-top: .5rem; padding: .85rem; font-size: 1rem; }

/* ============================================================
   Timeline (diario eventi pratica)
   ============================================================ */
ul.timeline { list-style: none; padding: 0; margin: 0; position: relative; }
ul.timeline::before {
  content: ""; position: absolute; left: 9px; top: 8px; bottom: 8px;
  width: 2px; background: var(--border);
}
.timeline-item { position: relative; padding: .5rem 0 1rem 36px; }
.timeline-dot {
  position: absolute; left: 0; top: .9rem;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--card);
  border: 2px solid var(--primary);
  box-shadow: 0 0 0 3px var(--card);
}
.timeline-meta {
  font-size: .82rem;
  color: var(--muted);
  margin-bottom: .25rem;
  display: flex; flex-wrap: wrap; gap: .4rem; align-items: center;
}
.timeline-content {
  background: var(--card);
  padding: .85rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

/* ============================================================
   Utility
   ============================================================ */
hr { border: 0; border-top: 1px solid var(--border); margin: 1.5rem 0; }
details > summary { cursor: pointer; padding: .5rem 0; user-select: none; }
details > summary:hover { color: var(--primary); }

/* iOS PWA safe areas */
@supports (padding: max(0px)) {
  body { padding-left: max(0px, env(safe-area-inset-left)); padding-right: max(0px, env(safe-area-inset-right)); }
}

/* Animazioni soft entrance */
.card, .stat, table.data { animation: fadeIn 250ms var(--easing) both; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Scrollbar discreta */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
