:root {
    --bg: #f3f5f4;
    --surface: #ffffff;
    --surface-2: #eef2f0;
    --text: #17221d;
    --muted: #5d6b64;
    --border: #dbe2de;
    --accent: #0d5c3f;
    --accent-2: #12805a;
    --accent-ink: #ffffff;
    --gas: #e0782b;
    --strom: #2f6fd6;
    --danger: #c0392b;
    --warn: #b7791f;
    --ok: #1e8e5a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 16px rgba(0, 0, 0, .05);
    --radius: 14px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1513;
        --surface: #17201c;
        --surface-2: #1f2a25;
        --text: #e7eeea;
        --muted: #9aaba2;
        --border: #2a3832;
        --accent: #2fa877;
        --accent-2: #3cc08b;
        --accent-ink: #06130d;
        --gas: #f0954f;
        --strom: #6b9cf0;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a { color: var(--accent-2); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* Topbar */
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 1rem;
    padding: .5rem max(16px, env(safe-area-inset-left));
    background: #0d5c3f; color: #fff;
}
.brand { display: flex; align-items: center; gap: .6rem; color: #fff; text-decoration: none; font-weight: 700; letter-spacing: .02em; }
.brand img { height: 36px; width: 36px; object-fit: contain; background: #fff; border-radius: 50%; padding: 2px; }
.nav { display: flex; gap: .25rem; margin-left: auto; flex-wrap: wrap; }
.nav a { color: #e6f4ee; text-decoration: none; padding: .45rem .8rem; border-radius: 999px; font-size: .95rem; }
.nav a:hover { background: rgba(255, 255, 255, .12); }
.nav a.active { background: rgba(255, 255, 255, .2); color: #fff; }
.nav a.nav-cta { background: #fff; color: #0d5c3f; font-weight: 600; }

.container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 1.25rem 16px 2rem; flex: 1; }
.footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 1rem 16px 1.5rem; }

/* Mobile tab bar */
.tabbar { display: none; }
@media (max-width: 760px) {
    .nav { display: none; }
    .brand span { font-size: 1.05rem; }
    .tabbar {
        display: grid; grid-template-columns: repeat(5, 1fr);
        position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
        background: var(--surface); border-top: 1px solid var(--border);
        padding-bottom: env(safe-area-inset-bottom);
    }
    .tabbar a {
        display: flex; flex-direction: column; align-items: center; gap: 2px;
        padding: .45rem 0 .5rem; font-size: .72rem; color: var(--muted); text-decoration: none;
    }
    .tabbar .ic { font-size: 1.15rem; line-height: 1.2; }
    .tabbar a.active { color: var(--accent-2); font-weight: 600; }
    .tabbar .tab-cta .ic {
        background: var(--accent); color: #fff; border-radius: 50%;
        width: 44px; height: 44px; display: grid; place-items: center; margin-top: -18px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, .25);
    }
    body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
    .footer { padding-bottom: 1rem; }
}

/* Cards / layout */
.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 1rem 1.1rem; margin-bottom: 1rem;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; margin-bottom: .5rem; }
.card-head h2, .card-head h3 { margin: 0; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .8rem .9rem; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: .8rem; }
.kpi .value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .8rem; color: var(--muted); }
.kpi.gas { border-top: 3px solid var(--gas); }
.kpi.strom { border-top: 3px solid var(--strom); }
.delta-up { color: var(--danger); }
.delta-down { color: var(--ok); }
.section-title { display: flex; align-items: center; gap: .5rem; margin: 1.25rem 0 .6rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.gas { background: var(--gas); }
.dot.strom { background: var(--strom); }

/* Tabs / segmented */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button {
    border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .9rem;
    padding: .35rem .85rem; border-radius: 999px; cursor: pointer;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -1.1rem; padding: 0 1.1rem; }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; cursor: pointer; user-select: none; white-space: nowrap; }
th.num { text-align: right; }
th .sort { opacity: .5; }
td:first-child { white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tr.partial td { color: var(--muted); font-style: italic; }
.table-foot { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding-top: .6rem; flex-wrap: wrap; }
.row-actions { white-space: nowrap; text-align: right; }
.icon-btn { border: 0; background: transparent; cursor: pointer; font-size: 1rem; padding: .2rem .35rem; border-radius: 6px; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* Forms */
label { display: block; font-size: .88rem; color: var(--muted); margin-bottom: .75rem; }
input, select {
    display: block; width: 100%; margin-top: .25rem;
    font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 10px; padding: .6rem .75rem;
}
input:focus, select:focus { outline: 2px solid var(--accent-2); outline-offset: 0; border-color: transparent; }
input.big { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0 1rem; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
    padding: .6rem 1rem; border-radius: 10px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .3rem .65rem; font-size: .85rem; font-weight: 500; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: wait; }
.btn-row { display: flex; gap: .6rem; flex-wrap: wrap; }

.alert { padding: .65rem .85rem; border-radius: 10px; margin-bottom: .9rem; font-size: .92rem; border: 1px solid; }
.alert.error { background: color-mix(in srgb, var(--danger) 10%, transparent); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.alert.warn { background: color-mix(in srgb, var(--warn) 12%, transparent); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.alert.ok { background: color-mix(in srgb, var(--ok) 10%, transparent); border-color: color-mix(in srgb, var(--ok) 35%, transparent); }
.alert.info { background: var(--surface-2); border-color: var(--border); }

/* Erfassen */
.capture { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
@media (max-width: 760px) { .capture { grid-template-columns: 1fr; } }
.photo-drop {
    border: 2px dashed var(--border); border-radius: var(--radius);
    min-height: 220px; display: grid; place-items: center; text-align: center;
    padding: 1rem; color: var(--muted); background: var(--surface-2); position: relative; overflow: hidden;
}
.photo-drop.drag { border-color: var(--accent-2); }
.photo-drop img { max-width: 100%; max-height: 60vh; border-radius: 10px; display: block; }
.photo-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin-top: .75rem; }
.ocr-status { margin-top: .75rem; }
.badge { display: inline-block; font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-weight: 600; }
.badge.hoch { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.mittel { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.niedrig { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.chart-box { position: relative; height: 320px; }
@media (max-width: 760px) { .chart-box { height: 260px; } }

/* Login */
.login { flex: 1; display: grid; place-items: center; padding: 1.5rem 16px; }
.login-card { width: 100%; max-width: 380px; text-align: left; padding: 1.75rem 1.5rem; }
.login-card h1 { text-align: center; margin-bottom: 0; }
.login-card > .muted { text-align: center; margin: 0 0 1.25rem; }
.login-logo { display: block; margin: 0 auto .75rem; height: 110px; width: 110px; object-fit: contain; background: #fff; border-radius: 50%; padding: 10px; }

.loading { padding: 3rem 0; text-align: center; color: var(--muted); }

.toast {
    position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%) translateY(150%);
    background: var(--text); color: var(--bg); padding: .65rem 1rem; border-radius: 10px;
    transition: transform .25s ease, opacity .25s ease; z-index: 50; opacity: 0; pointer-events: none; max-width: calc(100% - 32px); font-size: .92rem;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
@media (max-width: 760px) { .toast { bottom: calc(84px + env(safe-area-inset-bottom)); } }

dialog {
    border: 0; border-radius: var(--radius); padding: 0; max-width: min(560px, calc(100% - 32px));
    background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .35);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog .dlg { padding: 1.1rem 1.2rem; }
dialog img { max-width: 100%; max-height: 75vh; display: block; border-radius: 8px; }

/* Rahmen-Auswahl */
.photo-drop.has-photo { padding: 0; border-style: solid; display: block; min-height: 0; }
.crop { position: relative; touch-action: none; user-select: none; -webkit-user-select: none; cursor: crosshair; line-height: 0; }
.crop-img { width: 100%; height: auto; display: block; max-height: none; }
.crop-box {
    position: absolute; border: 2px solid #ffd400; cursor: move;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45);
}
.crop-box i {
    position: absolute; width: 22px; height: 22px; background: #ffd400; border-radius: 50%;
    border: 2px solid #000; transform: translate(-50%, -50%);
}
.crop-box i[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.crop-box i[data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.crop-box i[data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.crop-box i[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.photo-drop.has-photo { overflow: hidden; }
.crop-tools { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; margin-top: .6rem; }

/* Mehrfach-Upload */
.stapel { display: grid; gap: .75rem; }
.stapel-zeile { display: grid; grid-template-columns: 140px minmax(0, 1fr); gap: 1rem; align-items: start; margin: 0; }
.stapel-zeile.done { opacity: .7; }
.stapel-bild { width: 140px; height: 105px; object-fit: cover; border-radius: 10px; background: var(--surface-2); cursor: zoom-in; display: block; }
.stapel-felder label { margin-bottom: .4rem; }
.stapel-status { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; font-size: .9rem; }
.stapel-status .alert { margin: .3rem 0 0; width: 100%; }
.stapel-status [data-weg] { margin-left: auto; }
@media (max-width: 760px) {
    .stapel-zeile { grid-template-columns: 1fr; }
    .stapel-bild { width: 100%; height: 160px; }
}
