:root {
  --bg: #eef2ff;
  --card: #ffffff;
  --text: #1e1b4b;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-dark: #3730a3;
  --danger: #e11d48;
  --success: #059669;
  --orange: #f59e0b;
  --red: #e11d48;
  --dark-red: #9f1239;
  --burgundy: #7f1d1d;
  --intense: #450a0a;
  --border: #e0e7ff;
  --shadow: 0 4px 14px rgba(79, 70, 229, 0.08);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav-height: 64px;
  --accent: #06b6d4;
  --pink: #ec4899;
}

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

html, body {
  height: 100%;
  font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior: none;
}

button, input, select, textarea {
  font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom));
}

/* Header */
.header {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 50%, #db2777 100%);
  padding: 14px 16px 12px;
  border-bottom: none;
  position: sticky;
  top: 0;
  z-index: 50;
  text-align: center;
  color: #fff;
}

.header .title {
  color: #fff;
}

.header .datetime {
  color: rgba(255,255,255,0.85);
}

.header .status {
  background: rgba(255,255,255,0.2);
  color: #fff;
}

.header .status.offline {
  background: rgba(225, 29, 72, 0.9);
  color: #fff;
}

.title {
  font-style: italic;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.datetime {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 2px;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.status {
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 8px;
  background: #dcfce7;
  color: #166534;
}

.status.offline {
  background: #fee2e2;
  color: #991b1b;
}

/* Main */
.main {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tab {
  display: none;
}

.tab.active {
  display: block;
}

/* Cards */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.card.highlight {
  border: 1px solid var(--border);
}

.card h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text);
}

.date-label {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 10px;
  text-align: center;
}

/* Big hours display (center of Inserisci) */
.today-hero {
  text-align: center;
  padding: 24px 16px;
}

.hours-display {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
  margin: 20px 0 24px;
  min-height: 90px;
}

.hours-display #today-big-hours {
  font-size: 4.5rem;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.03em;
}

.hours-display .hours-unit {
  font-size: 1.5rem;
  font-weight: 600;
  opacity: 0.7;
}

.hours-display.hours-none #today-big-hours,
.hours-display.hours-none .hours-unit {
  color: #9ca3af;
}

.hours-display.hours-low #today-big-hours,
.hours-display.hours-low .hours-unit {
  color: #16a34a; /* verde 0-2 */
}

.hours-display.hours-mid #today-big-hours,
.hours-display.hours-mid .hours-unit {
  color: #ea580c; /* arancio 2-4 */
}

.hours-display.hours-high #today-big-hours,
.hours-display.hours-high .hours-unit {
  color: #dc2626; /* rosso 4-6 */
}

/* Forms */
.form-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

input[type="text"],
input[type="password"],
input[type="date"],
input[type="number"] {
  flex: 1;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  font-family: inherit;
  background: #fff;
  min-height: 48px;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

input:disabled {
  background: #f3f4f6;
  color: var(--muted);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 18px;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  min-height: 48px;
  transition: background 0.15s, opacity 0.15s;
}

.btn:active {
  opacity: 0.85;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.btn.primary:active {
  background: var(--primary-dark);
}

.btn.secondary {
  background: #f3f4f6;
  color: var(--text);
}

.btn.danger {
  background: var(--danger);
  color: #fff;
}

.btn.small {
  padding: 8px 12px;
  min-height: 36px;
  font-size: 0.85rem;
}

.btn.full {
  width: 100%;
  margin-top: 8px;
}

.btn-row {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.btn-row .btn {
  flex: 1;
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.95rem;
  font-weight: 500;
  padding: 8px 0;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  width: 100%;
}

/* Existing entry */
.existing {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.existing span {
  font-weight: 600;
  font-size: 1.1rem;
}

.info-msg {
  background: #fef3c7;
  color: #92400e;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 10px;
}

/* Entries list */
.entries-list {
  max-height: 280px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.entry-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.entry-row:last-child {
  border-bottom: none;
}

.entry-row:active {
  background: #f9fafb;
}

.entry-date {
  font-weight: 500;
}

.entry-hours {
  color: var(--muted);
  font-weight: 600;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 20px 0;
}

/* Stats */
.big-number {
  font-size: 1.75rem;
  font-weight: 700;
  margin-bottom: 4px;
}

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

.remaining {
  font-size: 0.95rem;
  margin: 6px 0 12px;
  font-weight: 500;
}

.progress-wrap {
  height: 10px;
  background: #e5e7eb;
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-bar {
  height: 100%;
  border-radius: 5px;
  transition: width 0.3s, background 0.3s;
  background: var(--success);
}

.progress-bar.month {
  background: var(--primary);
}

.status-badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
}

.status-badge.normale { background: #dcfce7; color: #166534; }
.status-badge.attenzione { background: #ffedd5; color: #9a3412; }
.status-badge.alta { background: #fee2e2; color: #991b1b; }
.status-badge.quasi { background: #fecaca; color: #7f1d1d; }
.status-badge.estremo { background: #fecaca; color: #450a0a; }
.status-badge.limite { background: #450a0a; color: #fff; }

.month-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-weight: 500;
}

.brutta-piega {
  margin-top: 12px;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f3f4f6;
  text-align: center;
}

.brutta-piega.level-0 { background: #dcfce7; color: #166534; }
.brutta-piega.level-1 { background: #fef9c3; color: #854d0e; }
.brutta-piega.level-2 { background: #ffedd5; color: #9a3412; }
.brutta-piega.level-3 { background: #fee2e2; color: #991b1b; }
.brutta-piega.level-4 { background: #450a0a; color: #fff; }

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-item {
  display: flex;
  flex-direction: column;
}

.stat-item:last-child {
  grid-column: 1 / -1;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--muted);
}

.stat-value {
  font-size: 1.15rem;
  font-weight: 700;
}

.anomaly {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

.monthly-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.month-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.month-row:last-child {
  border-bottom: none;
}

.month-name {
  font-weight: 500;
  min-width: 90px;
}

.month-data {
  text-align: right;
  color: var(--muted);
}

/* Charts */
.chart-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  background: #f3f4f6;
  border-radius: 10px;
  padding: 4px;
}

.chart-tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  color: var(--muted);
}

.chart-tab.active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.chart-container {
  position: relative;
  height: 220px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Settings */
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.switch {
  position: relative;
  width: 50px;
  height: 28px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #d1d5db;
  border-radius: 28px;
  transition: 0.2s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.2s;
}

input:checked + .slider {
  background: var(--primary);
}

input:checked + .slider:before {
  transform: translateX(22px);
}

/* Bottom Nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 480px;
  margin: 0 auto;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  font-family: inherit;
  cursor: pointer;
  padding: 6px 0;
}

.nav-btn.active {
  color: var(--primary);
}

.nav-icon {
  font-size: 1.25rem;
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}

.overlay.hidden {
  display: none;
}

.modal-box, .pin-box {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 320px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

.modal-box h2, .pin-box h2 {
  font-size: 1.15rem;
  margin-bottom: 8px;
}

.modal-box p, .pin-box p {
  color: var(--muted);
  margin-bottom: 16px;
  font-size: 0.95rem;
}

.pin-box input {
  width: 100%;
  text-align: center;
  font-size: 1.5rem;
  letter-spacing: 0.3em;
  margin-bottom: 12px;
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-bottom: 8px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 16px);
  left: 50%;
  transform: translateX(-50%);
  background: #1a1a2e;
  color: #fff;
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  z-index: 150;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  max-width: 90%;
  text-align: center;
}

.toast.hidden {
  display: none;
}

.hidden {
  display: none !important;
}

/* Special alert for 220 */
.alert-extreme {
  background: linear-gradient(135deg, #7f1d1d, #450a0a);
  color: #fff;
}

.alert-extreme h2, .alert-extreme p {
  color: #fff;
}

/* Utility */
@media (min-width: 481px) {
  body {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}


/* === v1.1 colorful enhancements === */
.card-accent {
  background: linear-gradient(180deg, #ffffff 0%, #f5f3ff 100%);
  border: 1px solid #c7d2fe;
}

.card-planning {
  border: 1px solid #a5f3fc;
  background: linear-gradient(180deg, #fff 0%, #ecfeff 100%);
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 10px;
}

.stats-grid-4 {
  grid-template-columns: 1fr 1fr;
}

.stat-value.ok { color: var(--success); }
.stat-value.warn { color: var(--danger); }

.badge-j {
  display: inline-block;
  background: #4f46e5;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 4px;
  vertical-align: middle;
}

.entry-note, .day-note-text {
  font-style: italic;
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 2px;
}

.entry-main {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.month-block {
  border-bottom: 1px solid var(--border);
}

.month-block:last-child {
  border-bottom: none;
}

.month-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 4px;
  background: none;
  border: none;
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--text);
  text-align: left;
}

.month-block.open .month-toggle {
  background: #eef2ff;
  border-radius: 8px;
  font-weight: 600;
}

.month-days {
  padding: 4px 0 10px 8px;
}

.empty-mini {
  font-size: 0.85rem;
  color: var(--muted);
  padding: 6px 0;
}

.day-detail {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 8px;
}

.day-detail-top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-weight: 600;
}

.justified-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
  cursor: pointer;
}

.justified-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.note-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  min-height: 40px;
}

.plan-form input[type="date"] {
  flex: 1.2;
}

.plan-form input[type="text"] {
  flex: 0.8;
  max-width: 80px;
}

.plan-form .btn {
  min-width: 48px;
  padding: 12px;
}

.planning-summary {
  margin: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.plan-stat {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  padding: 6px 0;
  border-bottom: 1px dashed var(--border);
}

.plan-warn {
  color: var(--danger);
  font-weight: 600;
  font-size: 0.85rem;
  margin-top: 6px;
}

.plan-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.plan-row span:first-child { flex: 1; }
.plan-row .btn.small {
  min-height: 32px;
  padding: 4px 10px;
}

.nav-btn.active {
  color: var(--primary);
}

.bottom-nav {
  box-shadow: 0 -4px 20px rgba(79, 70, 229, 0.08);
}

.btn.primary {
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
}

.progress-bar {
  background: linear-gradient(90deg, #22c55e, #4f46e5);
}
