:root {
  color-scheme: light;
  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #efeee9;
  --border: #dddcd6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6e69;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --series-1: #2a78d6;
  --danger: #d03b3b;
  --warn-bg: #fff4d6;
  --err-bg: #fde8e8;
  --grid-line: #e7e6e1;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border: #383835;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #9a998f;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --series-1: #3987e5;
    --warn-bg: #3a3014;
    --err-bg: #3d1c1c;
    --grid-line: #2c2c2a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 var(--font); background: var(--surface-0); color: var(--text-primary); }
a { color: var(--accent-ink); }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 10px; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.error { color: var(--danger); background: var(--err-bg); padding: 8px 12px; border-radius: var(--radius); }
.warn { background: var(--warn-bg); padding: 8px 12px; border-radius: var(--radius); margin-top: 8px; }

.topbar { display: flex; align-items: center; gap: 24px; padding: 0 16px; height: 48px; background: var(--surface-1); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 1000; }
.brand { font-weight: 700; color: var(--text-primary); text-decoration: none; }
nav { display: flex; gap: 4px; overflow-x: auto; }
nav a { padding: 6px 10px; border-radius: 6px; text-decoration: none; color: var(--text-secondary); white-space: nowrap; }
nav a.active, nav a:hover { background: var(--surface-2); color: var(--text-primary); }

main { padding: 16px; max-width: 1600px; margin: 0 auto; }
.page-head { margin-bottom: 12px; }
.head-row { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; }
.crumbs { font-size: 12px; color: var(--text-muted); }
.title-input { font: 700 22px var(--font); border: 1px solid transparent; background: transparent; color: var(--text-primary); padding: 2px 6px; border-radius: 6px; min-width: 0; width: 18ch; }
.title-input:hover, .title-input:focus { border-color: var(--border); background: var(--surface-1); }

.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 12px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }

button, .button { font: inherit; padding: 6px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); cursor: pointer; text-decoration: none; display: inline-block; }
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: default; }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--accent-ink); }
button.danger { color: var(--danger); }
input, select { font: inherit; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-1); color: var(--text-primary); max-width: 100%; }
.num-input { width: 8em; }
.unit-input { width: 7em; }
input.wide { width: 32em; }
label.inline-field, label.tool { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }
label.check input { margin: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px 16px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; color: var(--text-secondary); }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); border: 1px solid var(--border); }
.kinds { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }

/* fields overview */
.field-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.field-card { text-decoration: none; color: inherit; display: block; }
.field-card:hover { border-color: var(--accent); }
.field-label { background: rgba(255, 255, 255, .85); border: 0; font-weight: 600; box-shadow: none; }
.empty { text-align: center; padding: 40px; }

/* field page */
.field-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 12px; align-items: start; }
@media (max-width: 900px) { .field-layout { grid-template-columns: 1fr; } }
.sidebar { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; max-height: calc(100vh - 140px); overflow: auto; position: sticky; top: 60px; }
@media (max-width: 900px) { .sidebar { position: static; max-height: 40vh; } }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.chip { padding: 3px 10px; border-radius: 999px; font-size: 12px; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.ds-group-title { font-weight: 700; font-size: 12px; color: var(--text-muted); margin: 10px 4px 4px; text-transform: uppercase; letter-spacing: .04em; }
.ds-item { display: block; width: 100%; text-align: left; padding: 8px; margin-bottom: 4px; border-color: transparent; background: transparent; }
.ds-item:hover { background: var(--surface-2); }
.ds-item.active { border-color: var(--accent); background: var(--surface-2); }
.ds-item-top { display: flex; justify-content: space-between; gap: 6px; align-items: center; }
.ds-item-label { font-weight: 600; margin: 3px 0 1px; }

.map-wrap { position: relative; }
.map { height: 58vh; min-height: 340px; border-radius: var(--radius); border: 1px solid var(--border); background: #2c2c2a; }
.map-small { height: 38vh; margin-bottom: 12px; }
.map-legend { position: absolute; left: 10px; bottom: 26px; z-index: 500; pointer-events: none; }
.map-legend > * { pointer-events: auto; }
.legend { background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; min-width: 200px; max-width: 280px; box-shadow: var(--shadow); }
.legend-title { font-weight: 600; margin-bottom: 4px; }
.legend-bar { height: 10px; border-radius: 3px; }
.legend-ticks { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-secondary); margin-top: 2px; }
.legend-row { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.legend-note { background: var(--surface-1); font-size: 11px; padding: 2px 6px; border-radius: 4px; margin-top: 4px; display: inline-block; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex: none; vertical-align: -1px; }
.map-tools { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; padding: 8px 2px; }
.data-canvas { pointer-events: none; }
.popup-kv th { text-align: left; padding-right: 8px; font-weight: 500; color: #52514e; white-space: nowrap; }
.popup-kv td { font-variant-numeric: tabular-nums; }

.detail { margin-top: 4px; }
.detail-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 8px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 12px; overflow-x: auto; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; color: var(--text-secondary); padding: 8px 12px; white-space: nowrap; }
.tabs button.active { border-bottom-color: var(--accent); color: var(--text-primary); font-weight: 600; }

/* tables */
.table-wrap { overflow-x: auto; }
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
table.grid th, table.grid td { padding: 5px 8px; border-bottom: 1px solid var(--grid-line); text-align: left; white-space: nowrap; }
table.grid th { position: sticky; top: 0; background: var(--surface-1); color: var(--text-secondary); font-weight: 600; }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid.dense td, table.grid.dense th { padding: 3px 6px; font-size: 12px; }
table.kv { border-collapse: collapse; }
table.kv th { text-align: left; font-weight: 500; color: var(--text-secondary); padding: 3px 16px 3px 0; vertical-align: top; }
table.kv td { padding: 3px 0; font-variant-numeric: tabular-nums; }
.pager { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
details.more { margin: 6px 0; }
details.more summary { cursor: pointer; color: var(--accent-ink); }
pre.json { max-height: 400px; overflow: auto; background: var(--surface-2); padding: 10px; border-radius: 6px; font-size: 12px; }
.device-el { padding: 6px 0 10px; border-bottom: 1px solid var(--grid-line); }

/* charts */
.chart { position: relative; max-width: 760px; }
.chart-title { font-weight: 600; margin-bottom: 6px; }
.chart-body { position: relative; }
.hist { width: 100%; height: auto; display: block; }
.hist .grid-line { stroke: var(--grid-line); stroke-width: 1; }
.hist .axis-line { stroke: var(--text-muted); stroke-width: 1; }
.hist .axis-text { fill: var(--text-secondary); font-size: 11px; font-family: var(--font); }
.hist .bar { stroke: var(--surface-1); stroke-width: 0; }
.chart-note { font-size: 12px; margin-top: 4px; }
.tooltip { position: absolute; top: 0; transform: translateX(-50%); background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; font-size: 12px; white-space: nowrap; pointer-events: none; box-shadow: var(--shadow); }

/* import */
.dropzone { border: 2px dashed var(--border); border-radius: var(--radius); padding: 28px 16px; text-align: center; }
.dropzone.over { border-color: var(--accent); background: var(--surface-2); }
.drop-title { font-size: 16px; font-weight: 600; }
.dropzone .row { justify-content: center; }
.queue { margin-top: 10px; }

/* prices */
.calc-out { margin-top: 12px; }
.calc-out .big { font-size: 24px; font-weight: 700; }
.sticky-save { position: sticky; bottom: 0; background: var(--surface-0); padding: 8px 0; }

.toast { position: fixed; right: 16px; bottom: 16px; z-index: 2000; background: var(--text-primary); color: var(--surface-1); padding: 8px 14px; border-radius: 6px; box-shadow: var(--shadow); }

/* analysis */
.controls { padding: 10px 14px; }
.tools-row { margin-top: 0; gap: 8px 18px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 12px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.tile-label { font-size: 12px; color: var(--text-secondary); }
.tile-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; margin: 2px 0; }
.tile-sub { font-size: 12px; }
.two-col { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 16px; align-items: start; margin-bottom: 10px; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }
.econ-text p { margin: 0 0 8px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 4px; }
.crosshair { stroke: var(--text-muted); stroke-width: 1; }
.tip-head { font-weight: 600; margin-bottom: 2px; }
.tooltip .swatch { width: 10px; height: 10px; }
.badge.conf-high { color: #006300; border-color: #0ca30c; }
.badge.conf-low { color: #8a5a00; border-color: #fab219; }
.toast.error { background: var(--danger); color: #fff; }
