:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1b1f24;
  --muted: #5f6873;
  --border: #d9dde3;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --ok: #1d7a3e;
  --ok-bg: #e5f4ea;
  --bad: #c0262d;
  --bad-bg: #fbe7e8;
  --to: #1f5fbf;
  --ldg: #d9730d;
  --zfw: #6b7280;
  --env-fill: rgba(31, 95, 191, 0.10);
  --env-stroke: #1f5fbf;
  --grid: #e3e6eb;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111418;
    --surface: #1a1e24;
    --surface-2: #232830;
    --text: #e6e9ee;
    --muted: #9aa3ae;
    --border: #2f353e;
    --accent: #5b9bff;
    --accent-text: #0b1020;
    --ok: #4cc27a;
    --ok-bg: #16301f;
    --bad: #ff6b6f;
    --bad-bg: #3a1a1c;
    --to: #5b9bff;
    --ldg: #ffa54a;
    --zfw: #a3abb6;
    --env-fill: rgba(91, 155, 255, 0.12);
    --env-stroke: #5b9bff;
    --grid: #2a3038;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

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

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.05rem; margin-bottom: 12px; }
h3 { font-size: 0.9rem; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.print-only { display: none; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px;
  align-items: center; justify-content: space-between;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- controls ---------- */
button, select, input {
  font: inherit; color: inherit;
}
button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
}
button:hover { filter: brightness(1.08); }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.secondary:hover { background: var(--surface-2); filter: none; }
button.danger { color: var(--bad); }
button.icon { background: transparent; border: none; color: var(--muted); font-size: 1.4rem; line-height: 1; padding: 2px 8px; }
button.small-btn { padding: 4px 10px; font-size: 0.85rem; margin-top: 8px; }

select, input[type="text"], input[type="number"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
select { max-width: 60vw; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 16px;
  padding: 16px;
  max-width: 1200px;
  margin: 0 auto;
}
.span-all { grid-column: 1 / -1; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}

.fields { display: grid; gap: 10px; }
.field { display: grid; gap: 4px; }
.field > span:first-child { font-size: 0.88rem; }
.field small { color: var(--muted); font-weight: normal; }
.input-unit { display: flex; align-items: stretch; }
.input-unit input { flex: 1; border-radius: 8px 0 0 8px; text-align: right; font-variant-numeric: tabular-nums; }
.input-unit em {
  font-style: normal; font-size: 0.85rem; color: var(--muted);
  border: 1px solid var(--border); border-left: none;
  border-radius: 0 8px 8px 0; background: var(--surface-2);
  padding: 7px 10px; min-width: 44px; text-align: center;
}
input.over { border-color: var(--bad); background: var(--bad-bg); }

/* ---------- status ---------- */
.banner {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px; margin-bottom: 12px;
  font-weight: 600;
}
.banner.ok { background: var(--ok-bg); color: var(--ok); }
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner ul { margin: 4px 0 0; padding-left: 18px; font-weight: normal; }

.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.tile { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.tile .k { font-size: 0.78rem; color: var(--muted); }
.tile .v { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile .v small { font-size: 0.75rem; font-weight: normal; color: var(--muted); }
.tile.bad .v { color: var(--bad); }

/* ---------- chart ---------- */
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid line { stroke: var(--grid); }
.chart .axis text, .chart .tick { fill: var(--muted); font-size: 12px; }
.chart .axis-title { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .env { fill: var(--env-fill); stroke: var(--env-stroke); stroke-width: 2; stroke-linejoin: round; }
.chart .limit { stroke: var(--muted); stroke-dasharray: 5 4; stroke-width: 1.2; }
.chart .limit-label { fill: var(--muted); font-size: 11px; }
.chart .burn { stroke: var(--text); stroke-width: 2; }
.chart .zfw-line { stroke: var(--zfw); stroke-width: 1.2; stroke-dasharray: 3 4; }
.chart .pt-to { fill: var(--to); }
.chart .pt-ldg { fill: var(--ldg); }
.chart .pt-zfw { fill: var(--surface); stroke: var(--zfw); stroke-width: 2; }
.chart .pt-label { font-size: 12px; font-weight: 600; fill: var(--text); paint-order: stroke; stroke: var(--surface); stroke-width: 4px; }
.chart .out-ring { fill: none; stroke: var(--bad); stroke-width: 2; stroke-dasharray: 3 2; }

.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 14px; padding: 0; margin: 10px 0 0; font-size: 0.85rem; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.sw { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.sw-to { background: var(--to); }
.sw-ldg { background: var(--ldg); border-radius: 2px; }
.sw-zfw { border: 2px solid var(--zfw); }
.sw-env { background: var(--env-fill); border: 2px solid var(--env-stroke); border-radius: 2px; }

/* ---------- load sheet ---------- */
.table-wrap { overflow-x: auto; }
table.wb { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.wb th, table.wb td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
table.wb th:first-child, table.wb td:first-child { text-align: left; }
table.wb thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
table.wb tbody th { font-weight: normal; }
table.wb tr.sub { background: var(--surface-2); }
table.wb tr.sub th { font-weight: 600; }
table.wb tr.total th, table.wb tr.total td { font-weight: 700; }
table.wb tr.total.to { box-shadow: inset 4px 0 0 var(--to); }
table.wb tr.total.ldg { box-shadow: inset 4px 0 0 var(--ldg); }
table.wb tr.fuel th { color: var(--muted); }
.ok-txt { color: var(--ok); }
.bad-txt { color: var(--bad); font-weight: 700; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 34px); }
.dlg-head, .dlg-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 12px 16px; }
.dlg-head { border-bottom: 1px solid var(--border); }
.dlg-head h2 { margin: 0; }
.dlg-foot { border-top: 1px solid var(--border); flex-wrap: wrap; }
.dlg-foot .left, .dlg-foot .right { display: flex; gap: 8px; flex-wrap: wrap; }
.dlg-body { overflow-y: auto; padding: 8px 16px 16px; }

fieldset { border: none; padding: 0; margin: 14px 0 0; }
legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.grid .wide { grid-column: 1 / -1; }

table.edit { width: 100%; border-collapse: collapse; }
table.edit th { text-align: left; font-size: 0.8rem; color: var(--muted); font-weight: 600; padding: 4px; }
table.edit td { padding: 3px 4px; }
table.edit input { width: 100%; }
table.edit td:first-child { color: var(--muted); }
table.edit td:last-child { width: 36px; }

.error { color: var(--bad); min-height: 1em; margin: 12px 0 0; }

.footer { text-align: center; padding: 8px 16px 24px; max-width: 900px; margin: 0 auto; }

/* ---------- responsive ---------- */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- print ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; --surface-2: #f2f2f2; --text: #000; --muted: #444; --border: #bbb; --grid: #ddd; }
  .actions, .footer button, dialog { display: none !important; }
  .print-only { display: block; padding: 0 16px; margin: 4px 0 0; }
  .topbar { border: none; padding-bottom: 0; }
  .layout { grid-template-columns: 1fr 1.4fr; gap: 10px; padding: 10px 16px; }
  .card { break-inside: avoid; border-color: #bbb; }
  input { border-color: #bbb !important; }
}
