/* Mobile-first styles in the spirit of the KHS panel. */
:root {
  --khs-blue: #003d7c;
  --khs-blue-2: #0068b4;
  --bg: #eef1f5;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #6b7685;
  --line: #d6dce4;
  --ok: #2e7d32;
  --warn: #b26a00;
  --err: #c62828;
  --tab-h: 62px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161d; --card: #1b232d; --text: #e6ebf1; --muted: #9aa6b4; --line: #2c3743;
  }
}
* { 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.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
}
a { color: var(--khs-blue-2); }
main { padding: 12px 16px 24px; max-width: 1100px; margin: 0 auto; }
h1 { font-size: 1.35rem; margin: .4rem 0 .8rem; }
h2 { font-size: 1.15rem; margin: 1.2rem 0 .6rem; }
h3 { font-size: 1rem; margin: 1rem 0 .4rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--err); }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 16px 8px; background: var(--khs-blue); color: #fff;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 600; }
.topbar .brand img { background: #fff; border-radius: 4px; padding: 2px 4px; }
.topbar .logout { color: #cfe0f3; text-decoration: none; font-size: .9rem; }

/* bottom tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .75rem; }
.tabbar a img { width: 28px; height: 28px; object-fit: contain; }
@media (prefers-color-scheme: dark) { .tabbar a img { filter: invert(1) brightness(1.4); } }

/* buttons and forms */
.btn {
  display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--text);
  font: inherit; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--khs-blue-2); border-color: var(--khs-blue-2); color: #fff; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 12px; }
.actions form { display: inline; }
.form label, .form .field { display: block; margin: 0 0 10px; }
.form input, .form select, .form textarea, select {
  width: 100%; min-height: 44px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text); font: inherit;
}
.form.inline { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; }
.form.inline label { flex: 1 1 140px; margin: 0; }
.form div { margin-bottom: 10px; }
.sticky-actions {
  position: sticky; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom)); z-index: 10; display: flex; flex-wrap: wrap; gap: 8px;
  padding: 10px 0; background: var(--bg);
}
.errorlist { color: var(--err); margin: 4px 0; padding-left: 18px; }

/* cards, lists */
.cards { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
@media (min-width: 720px) { .cards { grid-template-columns: repeat(2, 1fr); } }
.card { background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--line); border-radius: 10px; padding: 12px; }
.card > a { display: flex; flex-direction: column; gap: 2px; color: inherit; text-decoration: none; }
.card .muted { display: block; font-size: .9rem; }
.status-ok { border-left-color: var(--ok); }
.status-not_tuned { border-left-color: var(--warn); }
.status-unused { border-left-color: var(--muted); }
.tag { display: inline-block; padding: 0 8px; border-radius: 10px; background: var(--khs-blue-2); color: #fff; font-size: .75rem; vertical-align: middle; }
.groups { list-style: none; padding: 0; columns: 2 160px; }
.groups li { padding: 6px 0; break-inside: avoid; }
.plain { list-style: none; padding: 0; }
details.block { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin: 8px 0; }
details.block > summary { cursor: pointer; font-weight: 600; min-height: 32px; display: flex; align-items: center; gap: 8px; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 8px 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }

/* parameter tables: scroll inside, never the page */
table.params { width: 100%; border-collapse: collapse; display: block; overflow-x: auto; }
table.params td, table.params th { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.params td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.params.edit input { width: 7.5em; min-height: 40px; text-align: right; }
table.params .old { color: var(--muted); text-decoration: line-through; }
table.params .new { font-weight: 700; color: var(--khs-blue-2); }
tr.src-unused, tr.src-base { color: var(--muted); }
body:not(.show-all) .group tr.src-unused, body:not(.show-all) .group tr.src-base { display: none; }
.toggle { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.toggle input { width: 22px; height: 22px; }

/* messages and warnings */
.messages, .warnings { list-style: none; padding: 0; margin: 8px 16px; }
.messages li, .warnings li { padding: 10px 12px; border-radius: 8px; margin-bottom: 6px; background: #fff4e0; color: #5a3b00; }
.messages li.success { background: #e5f4e7; color: #1d4d21; }
.messages li.error { background: #fde8e8; color: #7a1d1d; }
.warnings { margin: 8px 0; }
.offline-banner { background: var(--warn); color: #fff; padding: 8px 16px; font-size: .9rem; }

/* login */
.login-box { max-width: 380px; margin: 10vh auto 0; background: var(--card); padding: 20px; border-radius: 12px; border: 1px solid var(--line); }
.login-box label { display: block; margin-bottom: 12px; }
.login-box input { width: 100%; min-height: 44px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: var(--card); color: var(--text); }

/* map */
.map-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.map-tools select { flex: 1 1 180px; width: auto; }
.layers { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
.layers label { display: flex; gap: 8px; align-items: center; min-height: 36px; }
.map-hint { font-size: .8rem; color: var(--muted); margin: 0 0 6px; }
.map-wrap { background: #fff; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; touch-action: none;
  height: calc(100vh - 210px); min-height: 320px; }
#map { width: 100%; height: 100%; display: block; cursor: grab; user-select: none; }
#map .drive rect { fill: rgba(0, 104, 180, .12); stroke: var(--khs-blue-2); stroke-width: 3; }
#map .drive text { font: 700 22px system-ui, sans-serif; fill: var(--khs-blue); pointer-events: none; }
#map .drive:hover rect, #map .drive:focus rect { fill: rgba(0, 104, 180, .3); }
#map.show-lube .drive rect { fill: var(--lube); fill-opacity: .45; stroke: var(--lube); }
#map .jam rect { fill: none; stroke: #ff6e00; stroke-width: 5; }
#map .jam text { font: 700 22px system-ui, sans-serif; fill: #ff6e00; }
#map .icon circle { fill: none; stroke: #ff00c8; stroke-width: 5; }
#map .icon text { font: 700 22px system-ui, sans-serif; fill: #ff00c8; }
.legend-lube { display: flex; flex-wrap: wrap; gap: 10px; margin: 8px 0; font-size: .85rem; }
.legend-lube i { display: inline-block; width: 14px; height: 14px; border-radius: 3px; margin-right: 4px; vertical-align: -2px; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom)); z-index: 25;
  max-height: 65vh; overflow: auto; background: var(--card); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0; box-shadow: 0 -6px 24px rgba(0, 0, 0, .18); padding: 12px 16px 16px;
}
@media (min-width: 900px) { .sheet { left: auto; width: 420px; top: 70px; bottom: calc(var(--tab-h) + 10px); max-height: none; right: 10px; border-radius: 12px; } }
.sheet-close { position: sticky; top: 0; float: right; width: 40px; height: 40px; border: 0; border-radius: 20px; background: var(--bg); color: var(--text); font-size: 22px; }

/* knowledge base */
article.kb { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; overflow-wrap: anywhere; }
article.kb table { border-collapse: collapse; display: block; overflow-x: auto; margin: 8px 0; }
article.kb td, article.kb th { border: 1px solid var(--line); padding: 4px 8px; }
article.kb img { max-width: 100%; height: auto; }
article.kb h3 { scroll-margin-top: 64px; }
.kb-nav { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 16px; font-size: .9rem; }
.kb-nav a[aria-current] { font-weight: 700; }
