:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --border: #d6dbe3;
  --text: #16202c;
  --muted: #5e6b7c;
  --primary: #0f1b2d;
  --primary-text: #ffffff;
  --accent: #f0a500;
  --accent-text: #1a1200;
  --danger: #c0392b;
  --ok: #1e8e4f;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(10, 20, 40, .06), 0 4px 14px rgba(10, 20, 40, .06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141c; --surface: #171f2a; --surface-2: #1f2937; --border: #2b3645;
    --text: #e6ebf2; --muted: #93a0b2; --primary: #233247; --accent: #f5b623;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100dvh; -webkit-tap-highlight-color: transparent; }
a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1rem;
  background: var(--primary); color: var(--primary-text);
  padding: calc(.55rem + env(safe-area-inset-top)) 1rem .55rem;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; letter-spacing: .02em; }
.brand-mark { color: var(--accent); font-size: 1.2rem; }
.topnav { display: flex; gap: .25rem; }
.topnav a { padding: .35rem .7rem; border-radius: 6px; opacity: .8; }
.topnav a.active, .topnav a:hover { background: rgba(255,255,255,.12); opacity: 1; }
.topbar-right { margin-left: auto; display: flex; gap: .4rem; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.3); }

.container { max-width: 1100px; margin: 0 auto; padding: 1rem 16px 5rem; }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; }
.card + .card, .section + .section { margin-top: 1rem; }
.grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }
h1 { font-size: 1.45rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 0 0 .4rem; }
.page-head { display: flex; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head .actions { margin-left: auto; display: flex; gap: .5rem; flex-wrap: wrap; }
.breadcrumb { font-size: .85rem; color: var(--muted); margin-bottom: .35rem; display: flex; gap: .35rem; flex-wrap: wrap; }
.breadcrumb a:hover { text-decoration: underline; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: .4rem; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .55rem .9rem; border-radius: 8px; font: inherit; font-size: .95rem; line-height: 1;
  min-height: 40px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: .35rem .6rem; font-size: .85rem; min-height: 32px; }
.btn.icon { padding: .35rem .5rem; min-width: 32px; justify-content: center; }
.btn:disabled { opacity: .5; cursor: default; }

/* Lists */
.list { display: flex; flex-direction: column; gap: .5rem; }
.list-item {
  display: flex; align-items: center; gap: .8rem; padding: .75rem .9rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.list-item:hover { border-color: var(--accent); }
.list-item .title { font-weight: 600; }
.list-item .sub { font-size: .85rem; color: var(--muted); }
.list-item .chev { margin-left: auto; color: var(--muted); }
.type-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.4rem; height: 2.4rem; border-radius: 8px; font-size: 1.1rem; flex-shrink: 0;
  background: var(--surface-2); color: var(--text);
}
.type-badge.trafo { background: #fdebc8; color: #6b4700; }
.type-badge.nshv { background: #d9e8ff; color: #10407a; }
.type-badge.mshv { background: #e2d7f7; color: #45237a; }
.type-badge.sonstige { background: var(--surface-2); }
@media (prefers-color-scheme: dark) {
  .type-badge.trafo { background: #4a3300; color: #ffd27a; }
  .type-badge.nshv { background: #15325a; color: #a9c9ff; }
  .type-badge.mshv { background: #342053; color: #d2bdff; }
}
.pill { display: inline-block; padding: .15rem .55rem; border-radius: 999px; font-size: .75rem; background: var(--surface-2); color: var(--muted); }
.pill.ok { background: #d8f3e3; color: #0f5c32; }
.pill.warn { background: #fdebc8; color: #6b4700; }
.pill.bad { background: #fadbd8; color: #7b241c; }

/* Forms */
.form-section { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); margin-bottom: .75rem; overflow: hidden; }
.form-section > summary {
  list-style: none; cursor: pointer; padding: .7rem .9rem; font-weight: 600;
  display: flex; align-items: center; gap: .5rem; background: var(--surface-2);
}
.form-section > summary::-webkit-details-marker { display: none; }
.form-section > summary::before { content: '▸'; color: var(--muted); transition: transform .15s; }
.form-section[open] > summary::before { transform: rotate(90deg); }
.form-section .fields { padding: .75rem .9rem 1rem; display: grid; gap: .7rem .9rem; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.field { display: flex; flex-direction: column; gap: .25rem; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: .8rem; color: var(--muted); font-weight: 500; }
.field .req { color: var(--danger); }
.field input[type=text], .field input[type=number], .field input[type=date], .field select, .field textarea,
.plain-form input, .plain-form textarea, .plain-form select, .login-card input {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: .55rem .65rem; min-height: 42px; width: 100%;
}
.field textarea { min-height: 80px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .plain-form input:focus, .plain-form textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.field .with-unit { display: flex; align-items: center; gap: .4rem; }
.field .with-unit input { flex: 1; }
.field .unit { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.field.bool { flex-direction: row; align-items: center; gap: .6rem; min-height: 42px; }
.field.bool label { font-size: .95rem; color: var(--text); order: 2; }
.field.bool input { width: 1.25rem; height: 1.25rem; accent-color: var(--accent); }
.plain-form { display: grid; gap: .7rem .9rem; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.plain-form .field.wide { grid-column: 1 / -1; }
.save-state { font-size: .8rem; color: var(--muted); min-width: 6rem; }
.save-state.dirty { color: var(--accent); }
.save-state.saved { color: var(--ok); }

/* Tabs */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { background: none; border: none; font: inherit; color: var(--muted); padding: .6rem .9rem; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* Component tree */
.comp { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); margin-bottom: .5rem; }
.comp-head { display: flex; align-items: center; gap: .6rem; padding: .6rem .75rem; cursor: pointer; }
.comp-head:hover { background: var(--surface-2); }
.comp-head .num { font-weight: 700; min-width: 2.2rem; text-align: center; background: var(--primary); color: #fff; border-radius: 6px; padding: .25rem .4rem; font-size: .85rem; }
.comp-head .name { font-weight: 600; }
.comp-head .summary { color: var(--muted); font-size: .85rem; }
.comp-head .toggle { margin-left: auto; color: var(--muted); transition: transform .15s; }
.comp.open .comp-head .toggle { transform: rotate(90deg); }
.comp-body { border-top: 1px solid var(--border); padding: .75rem; background: var(--bg); }
.comp-children { margin-top: .75rem; padding-left: .75rem; border-left: 3px solid var(--accent); }
.comp-actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; margin-top: .5rem; }

/* Attachments */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .6rem; margin-top: .6rem; }
.thumb { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-2); aspect-ratio: 1; display: flex; flex-direction: column; }
.thumb img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; }
.thumb .file { flex: 1; display: flex; align-items: center; justify-content: center; font-size: 2rem; }
.thumb .cap { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.55); color: #fff; font-size: .7rem; padding: .2rem .35rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thumb .del { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.6); color: #fff; border: none; border-radius: 50%; width: 24px; height: 24px; cursor: pointer; }
.upload-btn input { display: none; }

/* Stats */
.stat { text-align: center; padding: .9rem; }
.stat .n { font-size: 1.8rem; font-weight: 700; }
.stat .l { color: var(--muted); font-size: .85rem; }
.search { width: 100%; font: inherit; font-size: 1.05rem; padding: .8rem 1rem; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }

/* Overlays */
.overlay { position: fixed; inset: 0; background: rgba(5, 10, 20, .85); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: .75rem; }
#lightbox img { max-width: 95vw; max-height: 85vh; border-radius: 6px; }
.lb-close { position: absolute; top: 1rem; right: 1rem; background: rgba(255,255,255,.15); color: #fff; border: none; width: 40px; height: 40px; border-radius: 50%; font-size: 1.2rem; cursor: pointer; }
.lb-caption { position: absolute; bottom: 1.5rem; color: #fff; font-size: .9rem; }
.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); background: var(--primary); color: #fff; padding: .6rem 1rem; border-radius: 8px; z-index: 60; box-shadow: var(--shadow); }
.toast.err { background: var(--danger); }

.type-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .6rem; }
.type-picker button { display: flex; flex-direction: column; align-items: center; gap: .4rem; padding: 1rem .5rem; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); font: inherit; cursor: pointer; color: var(--text); }
.type-picker button:hover { border-color: var(--accent); }
.type-picker .ic { font-size: 1.6rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }

@media (max-width: 640px) {
  .topnav a { padding: .35rem .5rem; }
  .btn-text { display: none; }
  .form-section .fields, .plain-form { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .comp-head { flex-wrap: wrap; }
  .comp-head .summary { flex-basis: 100%; padding-left: 2.8rem; margin-top: -.2rem; }
  .comp-head .toggle { position: absolute; right: .75rem; }
  .comp { position: relative; }
}

.group-title { font-weight: 700; font-size: .85rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: .6rem 0 .1rem; }
.group-title:first-child { margin-top: 0; }
