:root {
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --tap-target: 44px;
  --surface: #ffffff;
  --surface-muted: #f2f7f8;
  --ink: #0f172a;
  --ink-muted: #475569;
  --line: #d7e2e6;
  --brand: #0f766e;
  --brand-soft: rgba(15, 118, 110, 0.12);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  background: #f8fafc;
}

body.app-body {
  background: #f8fafc;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-shell-header {
  position: sticky;
  top: 0;
  z-index: 40;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
}

.app-shell-header-inner {
  min-height: calc(var(--tap-target) + var(--space-2));
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.app-brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--tap-target);
  text-decoration: none;
  color: inherit;
}

.app-brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.625rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #ffffff;
  background: linear-gradient(135deg, #0f766e 0%, #155e75 100%);
}

.app-brand-text {
  text-transform: uppercase;
  font-size: 0.775rem;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: #0b2533;
}

.app-shell-context {
  margin-left: auto;
  text-align: right;
  flex-direction: column;
  line-height: 1.1;
}

.app-shell-context-label {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.625rem;
  color: #64748b;
}

.app-shell-context-value {
  font-size: 0.8rem;
  font-weight: 600;
  color: #0f172a;
}

.app-top-nav-link {
  border-radius: 10px;
  min-height: var(--tap-target);
  padding: var(--space-2) var(--space-3);
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  color: var(--ink-muted);
  transition: color 0.2s ease, background-color 0.2s ease;
}

.app-top-nav-link:hover {
  background: #eef6f8;
  color: #0b5565;
}

.app-top-nav-link.is-active {
  background: var(--brand-soft);
  color: #0f766e;
}

.app-top-nav-logout {
  min-height: var(--tap-target);
  border-radius: 10px;
  border: 1px solid #c8d6dd;
  background: #ffffff;
  padding: var(--space-2) var(--space-3);
  font-size: 0.875rem;
  font-weight: 600;
  color: #1e293b;
}

.app-top-nav-logout:hover {
  background: #f4f8f9;
}

.app-shell-main {
  flex: 1;
  padding-bottom: calc(88px + env(safe-area-inset-bottom));
}

.app-shell-footer {
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.86);
}

.app-message {
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  padding: var(--space-3) var(--space-4);
  font-size: 0.875rem;
}

.app-message--info {
  background: #eff6ff;
  color: #1e3a8a;
  border-color: #bfdbfe;
}

.app-message--success {
  background: #ecfdf5;
  color: #166534;
  border-color: #bbf7d0;
}

.app-message--warning {
  background: #fffbeb;
  color: #92400e;
  border-color: #fde68a;
}

.app-message--error {
  background: #fef2f2;
  color: #b91c1c;
  border-color: #fecaca;
}

.app-bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 55;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  padding-top: 8px;
  padding-right: calc(10px + env(safe-area-inset-right));
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  padding-left: calc(10px + env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(14px);
}

.app-bottom-nav__link {
  min-height: var(--tap-target);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  color: #5b6b77;
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 2px;
  text-decoration: none;
  font-size: 0.675rem;
  font-weight: 600;
  line-height: 1.2;
  transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.app-bottom-nav__link:hover {
  background: #f2f7f8;
}

.app-bottom-nav__link.is-active {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: rgba(15, 118, 110, 0.2);
}

.app-bottom-nav__icon {
  width: 18px;
  height: 18px;
}

.app-more-sheet {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding-top: var(--space-3);
  padding-right: calc(var(--space-3) + env(safe-area-inset-right));
  padding-bottom: calc(var(--space-3) + env(safe-area-inset-bottom));
  padding-left: calc(var(--space-3) + env(safe-area-inset-left));
}

.app-more-sheet[aria-hidden="true"] {
  display: none;
}

.app-more-sheet__backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  padding: 0;
  background: rgba(15, 23, 42, 0.45);
}

.app-more-sheet__panel {
  position: relative;
  z-index: 1;
  width: min(560px, 100%);
  max-height: 82dvh;
  overflow: auto;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 0 20px 55px rgba(15, 23, 42, 0.22);
  padding: var(--space-4);
}

.app-more-sheet__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.app-more-sheet__title {
  font-size: 1rem;
  font-weight: 700;
  color: #102433;
}

.app-more-sheet__close {
  min-width: var(--tap-target);
  min-height: var(--tap-target);
  border-radius: 999px;
  border: 1px solid #cdd9df;
  background: #ffffff;
  color: #475569;
  font-size: 1.25rem;
  line-height: 1;
}

.app-more-sheet__grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-2);
}

.app-more-sheet__link {
  min-height: var(--tap-target);
  border-radius: var(--radius-md);
  border: 1px solid #d7e2e6;
  background: #f9fcfd;
  color: #0f172a;
  padding: var(--space-2) var(--space-3);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.app-more-sheet__link:hover {
  background: #eff7f9;
}

.app-more-sheet__link.is-active {
  border-color: rgba(15, 118, 110, 0.35);
  background: var(--brand-soft);
  color: #0f766e;
}

.app-more-sheet__logout {
  margin-top: var(--space-3);
  width: 100%;
  min-height: var(--tap-target);
  border-radius: var(--radius-md);
  border: 1px solid #fecaca;
  background: #fff5f5;
  color: #b91c1c;
  font-weight: 700;
}

.app-filter-drawer {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: none;
}

.app-filter-drawer.is-open {
  display: block;
}

.app-filter-drawer__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
}

.app-filter-drawer__panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 20px 20px 0 0;
  background: #ffffff;
  border-top: 1px solid #cfdde4;
  padding: var(--space-4);
  padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  max-height: 84dvh;
  overflow: auto;
}

.app-filter-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.app-filter-drawer__close {
  min-width: var(--tap-target);
  min-height: var(--tap-target);
  border-radius: 999px;
  border: 1px solid #d2dee4;
  background: #ffffff;
  color: #475569;
  font-size: 1.25rem;
}

.app-sticky-actions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(72px + env(safe-area-inset-bottom));
  z-index: 45;
  padding-top: 0;
  padding-right: calc(var(--space-3) + env(safe-area-inset-right));
  padding-bottom: 0;
  padding-left: calc(var(--space-3) + env(safe-area-inset-left));
}

.app-sticky-actions__inner {
  width: min(620px, 100%);
  margin: 0 auto;
  border-radius: 14px;
  border: 1px solid #d6e2e7;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 8px 22px rgba(15, 23, 42, 0.12);
  padding: var(--space-2);
  display: flex;
  gap: var(--space-2);
}

.app-sticky-actions__inner > * {
  flex: 1;
}

a,
button,
input,
select,
textarea {
  -webkit-tap-highlight-color: rgba(15, 118, 110, 0.15);
}

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(15, 118, 110, 0.35);
  outline-offset: 2px;
}

.app-surface {
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  background: var(--surface);
}

.app-section {
  border-radius: var(--radius-lg);
  border: 1px solid #dbe6ea;
  background: #ffffff;
}

.app-section__header {
  padding: var(--space-4);
  border-bottom: 1px solid #e7eef1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.app-section__title {
  font-size: 1rem;
  font-weight: 700;
  color: #102433;
}

.app-section__body {
  padding: var(--space-4);
}

.app-section__toggle {
  min-height: var(--tap-target);
  min-width: var(--tap-target);
  border-radius: 999px;
  border: 1px solid #d6e2e8;
  background: #ffffff;
  color: #334155;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  font-weight: 700;
}

.app-section__toggle[aria-expanded="true"] .app-section__toggle-icon {
  transform: rotate(180deg);
}

.app-section__toggle-icon {
  transition: transform 0.2s ease;
}

.app-kpi-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-2);
}

.app-kpi-card {
  border: 1px solid #dce7eb;
  border-radius: var(--radius-md);
  background: #ffffff;
  padding: var(--space-3);
}

.app-kpi-label {
  font-size: 0.675rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
}

.app-kpi-value {
  margin-top: 4px;
  font-size: 1.15rem;
  font-weight: 700;
  color: #0f172a;
}

.app-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border-radius: 999px;
  border: 1px solid #d6e3e8;
  background: #f5fafb;
  color: #164e63;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0 10px;
}

.app-mobile-card-list > * {
  content-visibility: auto;
  contain-intrinsic-size: 180px;
}

.app-stepper {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: var(--space-4);
}

.app-step {
  border: 1px solid #d7e3e8;
  border-radius: 10px;
  background: #f7fbfc;
  color: #5a6b78;
  min-height: var(--tap-target);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-align: center;
  padding: 8px 6px;
}

.app-step.is-active {
  border-color: rgba(15, 118, 110, 0.35);
  background: rgba(15, 118, 110, 0.12);
  color: #0f766e;
}

.app-step.is-completed {
  border-color: #86efac;
  background: #ecfdf3;
  color: #166534;
}

.app-step-panel[hidden] {
  display: none !important;
}

.app-step-controls {
  display: flex;
  gap: var(--space-2);
}

.app-step-controls > * {
  flex: 1;
}

.app-form-help {
  margin-top: 6px;
  font-size: 0.75rem;
  color: #64748b;
}

.app-empty-state {
  border: 1px dashed #c9d9e0;
  border-radius: var(--radius-md);
  background: #f8fbfc;
  color: #56707f;
  padding: var(--space-4);
  text-align: center;
}

.app-recipient-pill {
  border: 1px solid #d5e3e9;
  background: #f9fcfd;
  border-radius: 999px;
  min-height: 34px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
}

.app-recipient-pill button {
  border: 0;
  background: transparent;
  color: #b91c1c;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@media (min-width: 768px) {
  html,
  body,
  body.app-body {
    background: #f8fafc !important;
  }

  .app-shell-main {
    padding-bottom: var(--space-6);
  }

  .app-bottom-nav {
    display: none;
  }

  .app-more-sheet {
    display: none;
  }

  .app-filter-drawer {
    display: none !important;
  }

  .app-sticky-actions {
    display: none;
  }
}

/* iPhone landscape fallback: keep mobile navigation usable on small-height touch devices */
@media (max-height: 500px) and (orientation: landscape) and (hover: none) and (pointer: coarse) {
  .app-shell-context {
    display: none !important;
  }

  .app-shell-header nav[aria-label="Navigazione principale"] {
    display: none !important;
  }

  .app-shell-main {
    padding-bottom: calc(88px + env(safe-area-inset-bottom));
  }

  .app-bottom-nav {
    display: grid !important;
  }

  .app-more-sheet {
    display: none !important;
  }

  .app-more-sheet[aria-hidden="false"] {
    display: flex !important;
  }

  .app-filter-drawer {
    display: none !important;
  }

  .app-filter-drawer.is-open {
    display: block !important;
  }

  .app-sticky-actions {
    display: block !important;
  }
}
