:root {
  --bg: #F0F7FF;
  --card: #FFFFFF;
  --text: #1A2B4A;
  --text-secondary: #5A6B8A;
  --primary: #4A90D9;
  --primary-dark: #357ABD;
  --production: #FFB800;
  --consumption: #4A90D9;
  --grid: #718096;
  --export: #2ECC71;
  --import: #E74C3C;
  --battery: #9B59B6;
  --shadow: 0 4px 16px rgba(26, 43, 74, 0.08);
  --radius: 16px;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior-y: none;
}

#app { width: 100%; height: 100%; }

.screen {
  width: 100%;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}

.hidden { display: none !important; }

/* Login */
#login-screen {
  justify-content: center;
  align-items: center;
  padding: 24px;
}

.login-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.logo { margin-bottom: 16px; }

.login-card h1 { margin: 0 0 4px; font-size: 24px; }

.subtitle { color: var(--text-secondary); margin: 0 0 24px; font-size: 14px; }

#login-form { display: flex; flex-direction: column; gap: 12px; }

#login-form input {
  padding: 14px 16px;
  border: 1px solid #D0D9E8;
  border-radius: 12px;
  font-size: 16px;
  background: #FAFBFD;
  color: var(--text);
}

#login-form input:focus { outline: none; border-color: var(--primary); }

.remember {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  text-align: left;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 8px;
}

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

.error { color: var(--import); font-size: 14px; min-height: 20px; margin: 12px 0 0; }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  background: var(--card);
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header h1 { margin: 0; font-size: 18px; font-weight: 700; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text);
  padding: 6px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:active { background: #E8F0FA; }

.loading { text-align: center; padding: 40px; color: var(--text-secondary); }

/* Lista impianti */
.plants-list { padding: 12px; display: flex; flex-direction: column; gap: 12px; }

.plant-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  cursor: pointer;
}

.plant-card:active { transform: scale(0.995); }

.plant-header { display: flex; justify-content: space-between; align-items: flex-start; }

.plant-name { font-weight: 700; font-size: 16px; margin: 0 0 2px; }

.plant-location { font-size: 13px; color: var(--text-secondary); }

.status-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 20px;
  text-transform: uppercase;
}

.status-online { background: #E6F9ED; color: #27AE60; }
.status-offline { background: #FDEDEC; color: #C0392B; }
.status-warning { background: #FCF3CF; color: #B7950B; }

.plant-kpi-row { display: flex; gap: 12px; }

.plant-kpi {
  flex: 1;
  background: #F7FAFF;
  border-radius: 12px;
  padding: 10px;
  text-align: center;
}

.plant-kpi-label { font-size: 11px; color: var(--text-secondary); margin-bottom: 4px; }

.plant-kpi-value { font-size: 16px; font-weight: 700; color: var(--text); }

.plant-kpi-value.production { color: var(--production); }

/* Dettaglio */
.detail-content { padding: 12px; display: flex; flex-direction: column; gap: 12px; }

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.kpi {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  text-align: center;
}

.kpi-icon { font-size: 24px; margin-bottom: 6px; }

.kpi-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }

.kpi-value { font-size: 18px; font-weight: 700; }

.kpi.production .kpi-value { color: var(--production); }
.kpi.consumption .kpi-value { color: var(--consumption); }
.kpi.grid .kpi-value { color: var(--grid); }
.kpi.battery .kpi-value { color: var(--battery); }

/* Flow card */
.flow-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.flow-title { font-weight: 700; margin-bottom: 12px; }

.flow-scene {
  position: relative;
  height: 220px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.flow-node {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  z-index: 2;
}

.flow-node.solar { top: 4%; left: 4%; }
.flow-node.grid { top: 4%; right: 4%; }
.flow-node.home { bottom: 4%; left: 50%; transform: translateX(-50%); }

.flow-node-icon {
  width: 56px;
  height: 56px;
  background: #F7FAFF;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.flow-node-label { font-size: 12px; font-weight: 600; color: var(--text-secondary); }

.flow-node-value { font-size: 13px; font-weight: 700; color: var(--text); }

.flow-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.flow-line {
  stroke-dasharray: 10 10;
  stroke-dashoffset: 0;
  opacity: 0.15;
  transition: opacity 0.3s;
}

.flow-line.active {
  opacity: 1;
  animation: dash 1s linear infinite;
}

.flow-line.reverse.active {
  animation: dash-reverse 1s linear infinite;
}

@keyframes dash {
  to { stroke-dashoffset: -20; }
}

@keyframes dash-reverse {
  to { stroke-dashoffset: 20; }
}

.flow-legend {
  text-align: center;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* Detail info */
.detail-info {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px 16px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #EDF2F7;
  font-size: 14px;
}

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

.info-row span:first-child { color: var(--text-secondary); }

.info-row span:last-child { font-weight: 600; }

/* Safe area iOS */
@supports (padding-top: env(safe-area-inset-top)) {
  .app-header { padding-top: calc(16px + env(safe-area-inset-top)); }
  #login-screen { padding-bottom: env(safe-area-inset-bottom); }
  .detail-content { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .plants-list { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}

/* Responsive smartphone */
@media (max-width: 380px) {
  .login-card {
    padding: 24px 18px;
    border-radius: 14px;
  }
  .login-card .logo img { width: 180px; }
  .login-card h1 { font-size: 20px; }
  .subtitle { font-size: 13px; }

  .app-header h1 {
    font-size: 16px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: calc(100% - 80px);
  }

  .plant-card { padding: 14px; }
  .plant-name { font-size: 15px; }
  .plant-location { font-size: 12px; }
  .status-badge { font-size: 10px; padding: 3px 8px; }
  .plant-kpi-value { font-size: 14px; }

  .kpi { padding: 12px 8px; }
  .kpi-value { font-size: 15px; }
  .kpi-icon { font-size: 20px; }
  .kpi-label { font-size: 11px; }

  .flow-scene { height: 200px; }
  .flow-node-icon {
    width: 48px;
    height: 48px;
  }
  .flow-node-icon svg {
    width: 32px;
    height: 32px;
  }
  .flow-node-label { font-size: 11px; }
  .flow-node-value { font-size: 12px; }
  .flow-title { font-size: 15px; }

  .info-row { font-size: 13px; padding: 10px 0; }
}

@media (max-width: 340px) {
  .kpi-grid { gap: 8px; }
  .kpi-value { font-size: 14px; }
  .flow-scene { height: 180px; }
}

@media (min-width: 768px) {
  .detail-content {
    max-width: 680px;
    margin: 0 auto;
    width: 100%;
  }
  .plants-list {
    max-width: 680px;
    margin: 0 auto;
    width: 100%;
  }
}
