/* ============================
   CSS Custom Properties
   ============================ */
:root {
    --primary:      #2563eb;
    --primary-dk:   #1d4ed8;
    --primary-lt:   #eff6ff;
    --success:      #16a34a;
    --success-lt:   #f0fdf4;
    --danger:       #dc2626;
    --warning:      #d97706;
    --bg:           #f1f5f9;
    --surface:      #ffffff;
    --text:         #1e293b;
    --text-muted:   #64748b;
    --border:       #e2e8f0;
    --shadow:       0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:    0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
    --radius:       14px;
    --radius-sm:    8px;
    --nav-h:        64px;
    --header-h:     56px;
}

/* ============================
   Reset & Base
   ============================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; font-family: inherit; border: none; background: none; }
input  { font-family: inherit; }

/* ============================
   Screens
   ============================ */
.screen { display: none; }
.screen.active { display: flex; flex-direction: column; min-height: 100dvh; }

/* ============================
   Login
   ============================ */
#login-screen {
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: linear-gradient(145deg, #1e40af 0%, #2563eb 60%, #3b82f6 100%);
}

.login-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 36px 28px;
    width: 100%;
    max-width: 360px;
    box-shadow: var(--shadow-md);
}

.login-logo {
    text-align: center;
    margin-bottom: 28px;
}
.login-logo .logo-icon {
    font-size: 48px;
    display: block;
    margin-bottom: 8px;
}
.login-logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}
.login-logo p {
    font-size: .85rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.form-group {
    margin-bottom: 16px;
}
.form-group label {
    display: block;
    font-size: .8rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: .04em;
}
.form-group input {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    color: var(--text);
    background: var(--bg);
    transition: border-color .15s;
}
.form-group input:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--surface);
}

.btn-primary {
    width: 100%;
    padding: 13px;
    background: var(--primary);
    color: #fff;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    transition: background .15s;
    margin-top: 8px;
}
.btn-primary:hover   { background: var(--primary-dk); }
.btn-primary:active  { background: var(--primary-dk); }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }

.login-error {
    background: #fef2f2;
    color: var(--danger);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: .875rem;
    margin-top: 12px;
    display: none;
}

/* ============================
   App Header
   ============================ */
#app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--primary);
    color: #fff;
    height: var(--header-h);
    display: flex;
    align-items: center;
    padding: 0 16px;
    box-shadow: 0 2px 4px rgba(0,0,0,.15);
    gap: 12px;
}
#app-header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    flex: 1;
}
#app-header .header-subtitle {
    font-size: .75rem;
    opacity: .8;
    display: block;
    font-weight: 400;
}
.btn-logout {
    padding: 7px 12px;
    background: rgba(255,255,255,.2);
    color: #fff;
    border-radius: var(--radius-sm);
    font-size: .8rem;
    font-weight: 600;
    transition: background .15s;
}
.btn-logout:hover { background: rgba(255,255,255,.3); }

/* ============================
   Bottom Navigation
   ============================ */
#bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0; right: 0;
    height: var(--nav-h);
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    z-index: 100;
    box-shadow: 0 -2px 8px rgba(0,0,0,.06);
}
.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 4px;
    color: var(--text-muted);
    font-size: .65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    transition: color .15s;
    border-top: 3px solid transparent;
}
.nav-btn .nav-icon { font-size: 1.35rem; line-height: 1; }
.nav-btn.active {
    color: var(--primary);
    border-top-color: var(--primary);
}

/* ============================
   Page Content
   ============================ */
#page-content {
    flex: 1;
    padding: 16px 16px calc(var(--nav-h) + 16px);
    overflow-y: auto;
}

/* ============================
   Cards
   ============================ */
.card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    margin-bottom: 12px;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
}
.card-title {
    font-size: .95rem;
    font-weight: 600;
    color: var(--text);
}
.card-sub {
    font-size: .75rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* ============================
   Dashboard
   ============================ */
.dash-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 16px;
}
.stat-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    text-align: center;
}
.stat-card .stat-icon  { font-size: 1.8rem; margin-bottom: 6px; }
.stat-card .stat-value { font-size: 1.4rem; font-weight: 700; color: var(--text); }
.stat-card .stat-unit  { font-size: .7rem;  color: var(--text-muted); font-weight: 600; }
.stat-card .stat-label { font-size: .75rem; color: var(--text-muted); margin-top: 4px; }

.section-title {
    font-size: .8rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .06em;
    margin: 16px 0 8px;
}

/* ============================
   Switch / Toggle
   ============================ */
.device-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 12px;
}
.device-icon {
    font-size: 1.4rem;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    flex-shrink: 0;
}
.device-icon.on { background: #fef9c3; }
.device-info { flex: 1; min-width: 0; }
.device-name {
    font-size: .9rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.device-status {
    font-size: .75rem;
    color: var(--text-muted);
    margin-top: 2px;
}
.device-status.on  { color: var(--success); font-weight: 600; }
.device-status.off { color: var(--text-muted); }

/* Toggle Switch */
.toggle {
    position: relative;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
}
.toggle input {
    opacity: 0;
    width: 0; height: 0;
    position: absolute;
}
.toggle-track {
    position: absolute;
    inset: 0;
    border-radius: 14px;
    background: var(--border);
    transition: background .2s;
    cursor: pointer;
}
.toggle-track::after {
    content: '';
    position: absolute;
    width: 22px; height: 22px;
    border-radius: 50%;
    background: #fff;
    top: 3px; left: 3px;
    transition: transform .2s;
    box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.toggle input:checked + .toggle-track {
    background: var(--success);
}
.toggle input:checked + .toggle-track::after {
    transform: translateX(20px);
}

/* ============================
   Shutter / Rolladen
   ============================ */
.shutter-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
    margin-bottom: 10px;
}
.shutter-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}
.shutter-pos {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary);
    margin-left: auto;
    white-space: nowrap;
}
.shutter-slider {
    width: 100%;
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    border-radius: 3px;
    background: var(--border);
    outline: none;
    margin: 8px 0;
}
.shutter-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 22px; height: 22px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    box-shadow: var(--shadow);
}
.shutter-slider::-moz-range-thumb {
    width: 22px; height: 22px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
    border: none;
    box-shadow: var(--shadow);
}
.shutter-btns {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}
.btn-shutter {
    flex: 1;
    padding: 8px;
    border-radius: var(--radius-sm);
    font-size: .8rem;
    font-weight: 600;
    background: var(--bg);
    color: var(--text);
    transition: background .15s;
}
.btn-shutter:hover { background: var(--border); }
.btn-shutter.open  { background: var(--primary-lt); color: var(--primary); }
.btn-shutter.close { background: #fef2f2; color: var(--danger); }
.btn-shutter.stop  { background: #fef9c3; color: var(--warning); }

/* ============================
   Dimmer
   ============================ */
.dimmer-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 8px;
}
.dimmer-val {
    font-size: .85rem;
    font-weight: 700;
    color: var(--primary);
    width: 38px;
    text-align: right;
}

/* ============================
   PV / Solar
   ============================ */
.pv-hero {
    background: linear-gradient(135deg, #1e40af, #2563eb);
    color: #fff;
    border-radius: var(--radius);
    padding: 24px;
    text-align: center;
    margin-bottom: 12px;
    box-shadow: var(--shadow-md);
}
.pv-hero .pv-power {
    font-size: 3rem;
    font-weight: 800;
    line-height: 1;
}
.pv-hero .pv-unit { font-size: 1.2rem; opacity: .8; }
.pv-hero .pv-label { font-size: .85rem; opacity: .7; margin-top: 4px; }
.pv-hero .pv-yield {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid rgba(255,255,255,.2);
    display: flex;
    gap: 24px;
    justify-content: center;
}
.pv-hero .pv-yield-item { text-align: center; }
.pv-hero .pv-yield-val  { font-size: 1.3rem; font-weight: 700; }
.pv-hero .pv-yield-lbl  { font-size: .7rem; opacity: .7; margin-top: 2px; }

.inv-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
    margin-bottom: 10px;
}
.inv-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}
.inv-name  { font-size: .9rem; font-weight: 600; }
.inv-power { font-size: 1rem;  font-weight: 700; color: var(--warning); }
.inv-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px 16px;
}
.inv-row { font-size: .8rem; color: var(--text-muted); display: flex; justify-content: space-between; }
.inv-row span:last-child { color: var(--text); font-weight: 500; }

/* ============================
   Meter / Verbrauch
   ============================ */
.meter-hero {
    background: linear-gradient(135deg, #065f46, #059669);
    color: #fff;
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 12px;
    box-shadow: var(--shadow-md);
}
.meter-hero .meter-power {
    font-size: 2.6rem;
    font-weight: 800;
    line-height: 1;
}
.meter-hero .meter-unit  { font-size: 1rem; opacity: .8; }
.meter-hero .meter-label { font-size: .8rem; opacity: .7; margin-top: 2px; }
.meter-phases {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid rgba(255,255,255,.2);
}
.phase-item { text-align: center; }
.phase-val  { font-size: 1.1rem; font-weight: 700; }
.phase-lbl  { font-size: .65rem; opacity: .7; margin-top: 2px; }

.meter-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 14px;
}
.meter-icon-wrap {
    width: 48px; height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    flex-shrink: 0;
}
.meter-icon-wrap.gas   { background: #fff7ed; }
.meter-icon-wrap.water { background: #eff6ff; }
.meter-card-info { flex: 1; }
.meter-card-val  { font-size: 1.4rem; font-weight: 700; color: var(--text); }
.meter-card-unit { font-size: .8rem; color: var(--text-muted); font-weight: 600; }
.meter-card-label{ font-size: .8rem; color: var(--text-muted); margin-top: 2px; }

/* ============================
   States
   ============================ */
.loading-msg {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
    font-size: .9rem;
}
.loading-msg .spin {
    display: inline-block;
    width: 24px; height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
    margin-bottom: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-msg {
    text-align: center;
    padding: 24px;
    color: var(--danger);
    background: #fef2f2;
    border-radius: var(--radius);
    font-size: .875rem;
}

.empty-msg {
    text-align: center;
    padding: 32px;
    color: var(--text-muted);
    font-size: .875rem;
}

/* ============================
   Update indicator
   ============================ */
.last-update {
    text-align: center;
    font-size: .7rem;
    color: var(--text-muted);
    padding: 8px 0 0;
}

/* ============================
   Responsive
   ============================ */
@media (min-width: 480px) {
    .dash-grid { grid-template-columns: repeat(2, 1fr); }
    #page-content { max-width: 600px; margin: 0 auto; }
}
