:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --card: #ffffff;
  --fg: #1b1f24;
  --dim: #6a7480;
  --line: #dfe3e8;
  --accent: #1f6feb;
  --accent-fg: #ffffff;
  --ok-bg: #e7f6ec;
  --ok-fg: #17632e;
  --err-bg: #fdeaea;
  --err-fg: #9b1c1c;
  --warn-bg: #fdf3d8;
  --warn-fg: #7a5a06;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181d;
    --card: #1d2127;
    --fg: #e6e9ee;
    --dim: #98a2b0;
    --line: #2d333c;
    --accent: #4c8dff;
    --accent-fg: #08111f;
    --ok-bg: #17301f;
    --ok-fg: #9ae3b3;
    --err-bg: #3a1d1d;
    --err-fg: #ffb3b3;
    --warn-bg: #3a3117;
    --warn-fg: #f2d98b;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

main { max-width: 1100px; margin: 0 auto; padding: 1rem 1rem 4rem; }

h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.1rem; margin: 1.4rem 0 .5rem; }

/* ---------------------------------------------------------------- Topbar */

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: .7rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; text-decoration: none; color: var(--fg); }
.topbar nav { display: flex; gap: .3rem; margin-left: auto; }
.topbar nav a {
  padding: .35rem .7rem;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--fg);
}
.topbar nav a:hover { background: var(--bg); }
.topbar nav a.active { background: var(--accent); color: var(--accent-fg); }
.logout { margin: 0; display: flex; align-items: center; gap: .5rem; }
.logout .account { color: var(--fg); text-decoration: none; font-size: .9rem; }
.logout .account:hover { text-decoration: underline; }
button.link {
  background: none; border: 0; padding: .35rem .5rem;
  color: var(--dim); cursor: pointer; font: inherit;
}
button.link:hover { color: var(--fg); text-decoration: underline; }

/* ------------------------------------------------------------------ Card */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.2rem;
  margin-bottom: 1rem;
}
.card.narrow { max-width: 420px; margin: 3rem auto; }

.meta { color: var(--dim); font-size: .85rem; }
.hint { color: var(--dim); font-size: .85rem; }
.dim { color: var(--dim); }

.alert { padding: .6rem .8rem; border-radius: var(--radius); margin: 0 0 1rem; }
.alert.ok { background: var(--ok-bg); color: var(--ok-fg); }
.alert.error { background: var(--err-bg); color: var(--err-fg); }
.alert.warn { background: var(--warn-bg); color: var(--warn-fg); }
.alert ul { margin: 0; padding-left: 1.1rem; }

/* --------------------------------------------------------------- Formular */

label { display: block; margin-bottom: .8rem; font-size: .85rem; color: var(--dim); }

input[type="text"], input[type="search"], input[type="password"],
input[type="number"], select, textarea {
  display: block;
  width: 100%;
  margin-top: .25rem;
  padding: .5rem .6rem;
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

label.check { display: flex; align-items: center; gap: .5rem; color: var(--fg); font-size: .9rem; }
label.check input { width: auto; margin: 0; }

table.devices { margin-bottom: .8rem; }
table.devices td { font-size: .9rem; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0 1rem; }

fieldset.specs {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .8rem 1rem .2rem;
  margin: 0 0 1rem;
}
fieldset.specs legend { color: var(--dim); font-size: .8rem; padding: 0 .4rem; }

button, .button {
  display: inline-block;
  padding: .5rem .9rem;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--fg);
  text-decoration: none;
  cursor: pointer;
}
button:hover, .button:hover { border-color: var(--accent); }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
button.ghost, .button.ghost { background: none; }
button.danger { background: var(--err-bg); color: var(--err-fg); border-color: transparent; }

.form-actions { display: flex; gap: .6rem; align-items: center; margin-top: .5rem; }
.danger-zone {
  display: flex; gap: .8rem; align-items: center; flex-wrap: wrap;
  margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--line);
}

/* --------------------------------------------------------------- Toolbar */

.toolbar {
  display: flex; gap: 1rem; align-items: flex-start; flex-wrap: wrap;
  margin-bottom: .8rem;
}
.toolbar h1 { margin: 0; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; flex: 1 1 420px; }
.filters label { margin: 0; }
.filters input, .filters select { margin: 0; width: auto; }
.filters input[type="search"] { flex: 1 1 220px; }
.filters input[type="number"] { width: 8rem; }
.actions { display: flex; gap: .5rem; margin-left: auto; }

/* --------------------------------------------------------------- Tabelle */

table.parts { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table.parts th { text-align: left; font-size: .8rem; color: var(--dim); font-weight: 600; padding: .6rem .8rem; border-bottom: 1px solid var(--line); }
table.parts th a { color: inherit; text-decoration: none; }
table.parts td { padding: .6rem .8rem; border-bottom: 1px solid var(--line); vertical-align: top; }
table.parts tr:last-child td { border-bottom: 0; }
table.parts .name { font-weight: 600; text-decoration: none; }
table.parts .sub { display: block; font-size: .82rem; color: var(--dim); }
.row-actions { text-align: right; font-size: .85rem; white-space: nowrap; }

.tag {
  display: inline-block; padding: .1rem .5rem; border-radius: 999px;
  background: var(--bg); border: 1px solid var(--line);
  font-size: .75rem; color: var(--dim);
}
.tag.warn { background: var(--warn-bg); color: var(--warn-fg); border-color: transparent; }

.qty { display: flex; align-items: center; gap: .25rem; }
.qty-value { min-width: 2.6rem; text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }
.qty-value.zero { color: var(--err-fg); }
.qty-value.flash { transition: none; color: var(--ok-fg); }
.qty-btn { padding: .2rem .55rem; line-height: 1.3; }
.qty-btn.small { font-size: .75rem; color: var(--dim); }
.qty-btn[disabled] { opacity: .5; cursor: progress; }

/* --------------------------------------------------------------- Projekte */

.project-list { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.card.project { text-decoration: none; color: inherit; display: block; }
.card.project:hover { border-color: var(--accent); }
.card.project h2 { margin-top: 0; }
ul.bom { margin: .3rem 0; padding-left: 1.1rem; }
pre.schematic {
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .8rem; overflow-x: auto; white-space: pre-wrap; font-size: .85rem;
}

/* ------------------------------------------------------------ Feldeditor */

table.fieldrows { width: 100%; border-collapse: collapse; margin-bottom: .6rem; }
table.fieldrows th {
  text-align: left; font-size: .72rem; font-weight: 600; color: var(--dim);
  text-transform: uppercase; letter-spacing: .03em; padding: 0 .3rem .3rem;
}
table.fieldrows td { padding: .15rem .3rem; vertical-align: top; }
table.fieldrows input, table.fieldrows select { margin-top: 0; margin-bottom: .2rem; font-size: .9rem; }
table.fieldrows .row-actions { white-space: nowrap; }
table.fieldrows .main-cell { text-align: center; }
table.fieldrows .main-cell input { width: auto; margin: .55rem 0 0; }
@media (max-width: 720px) {
  table.fieldrows .main-cell { text-align: left; }
  table.fieldrows .main-cell input { margin: .2rem 0 .4rem; }
}

.warn-text { color: var(--warn-fg); }
#bulk-text { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9rem; }

/* ----------------------------------------------------------------- Export */

ul.formats { margin: .8rem 0 0; padding-left: 1.1rem; color: var(--dim); font-size: .85rem; }
ul.formats li { margin-bottom: .2rem; }
ul.formats strong { color: var(--fg); }

#export-text, #dictate-text {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .82rem;
  line-height: 1.45;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}
#dictate-text { white-space: pre-wrap; }

/* ------------------------------------------------------------------- Scan */

.filepick input[type="file"] { margin-top: .4rem; }
#preview { max-width: 100%; max-height: 320px; border-radius: var(--radius); border: 1px solid var(--line); display: block; margin: .8rem 0; }
.alt {
  display: flex; gap: .8rem; align-items: center; justify-content: space-between;
  padding: .5rem .7rem; margin-bottom: .4rem;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg);
}
.alt .conf { font-variant-numeric: tabular-nums; color: var(--dim); font-size: .8rem; }
.spinner { display: inline-block; width: .8rem; height: .8rem; border: 2px solid var(--dim); border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -1px; margin-right: .4rem; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----------------------------------------------------------------- Mobile */

@media (max-width: 720px) {
  .topbar { gap: .4rem .8rem; padding: .6rem .8rem; }
  .topbar nav { margin-left: 0; order: 3; width: 100%; }
  .logout { margin-left: auto; font-size: .85rem; }

  table.parts, table.parts tbody, table.parts tr, table.parts td,
  table.fieldrows, table.fieldrows tbody, table.fieldrows tr, table.fieldrows td { display: block; width: 100%; }
  table.parts thead, table.fieldrows thead { display: none; }
  table.fieldrows tr { border: 1px solid var(--line); border-radius: var(--radius); padding: .5rem; margin-bottom: .6rem; }
  table.fieldrows td[data-label]::before {
    content: attr(data-label); display: block;
    font-size: .7rem; color: var(--dim); text-transform: uppercase; letter-spacing: .03em;
  }
  table.parts tr { border-bottom: 1px solid var(--line); padding: .6rem 0; }
  table.parts td { border: 0; padding: .15rem .8rem; }
  table.parts td[data-label="Menge"]::before,
  table.parts td[data-label="Ort"]::before {
    content: attr(data-label);
    font-size: .7rem; color: var(--dim); text-transform: uppercase; letter-spacing: .03em;
    display: inline-block; min-width: 4rem;
  }
  table.parts td[data-label="Ort"] { color: var(--dim); font-size: .9rem; }
  .qty { display: inline-flex; }
  .row-actions { text-align: left; }
  .filters { gap: .4rem; }
  .filters input[type="search"] { flex: 1 1 100%; }
  .actions { margin-left: 0; width: 100%; }
  .actions .button { flex: 1; text-align: center; }
}
