/*
 * MK.ui components (public/js/ui.js): stand card, value block, chips, charts, legend, stand table,
 * layer panel, search results, messages, toasts, skeletons, sheet handle.
 * Every colour comes from tokens.css; the only local tokens are text-safe inks for species that
 * tokens.css does not cover (--mk-ink-*), with light and dark values.
 * Map colours (swatches, bars) are set inline by ui.js from MK.codes / MK.ui palettes.
 */

:root {
  --mk-ink-lapegle: #a1009e;
  --mk-ink-ozols: #555555;
  --mk-ink-osis: #2d7385;
  --mk-ink-liepa: #7a7000;
  --mk-ink-goba: #27782a;
  --mk-ink-pilads: #8a5a2b;
  --mk-grid-bg:
    linear-gradient(var(--c-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--c-grid) 1px, transparent 1px);
}

[data-theme="dark"] {
  --mk-ink-lapegle: #ff6bff;
  --mk-ink-ozols: #c8c8c8;
  --mk-ink-osis: #8fd0e0;
  --mk-ink-liepa: #e8e85a;
  --mk-ink-goba: #7ccf7c;
  --mk-ink-pilads: #d9b98f;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --mk-ink-lapegle: #ff6bff;
    --mk-ink-ozols: #c8c8c8;
    --mk-ink-osis: #8fd0e0;
    --mk-ink-liepa: #e8e85a;
    --mk-ink-goba: #7ccf7c;
    --mk-ink-pilads: #d9b98f;
  }
}

/* ---- utilities ------------------------------------------------------------------------------ */

.mk-vh {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.mk-mono {
  font-family: var(--font-mono);
}

.mk-num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.mk-strong {
  font-weight: 700;
}

.mk-note {
  margin: 0;
  font-size: var(--fs-sm);
  line-height: var(--lh-body, 1.45);
  color: var(--c-ink-2);
}

[data-ui][hidden],
.mk-facts[hidden],
.mk-layer__hint[hidden] {
  display: none !important;
}

.mk-eyebrow,
.mk-label,
.mk-value__title,
.mk-seg__legend,
.mk-results__count {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-xs);
  font-weight: 650;
  font-stretch: var(--wdth-cond, 75%);
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--c-ink-2);
}

.mk-h2 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 700;
  line-height: var(--lh-tight, 1.2);
  color: var(--c-ink);
}

.mk-h3 {
  margin: 0 0 var(--sp-2);
  font-size: var(--fs-sm);
  font-weight: 700;
  font-stretch: var(--wdth-cond, 75%);
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--c-ink);
}

/* ---- focus ------------------------------------------------------------------------------------ */

.mk-btn:focus-visible,
.mk-iconbtn:focus-visible,
.mk-sortbtn:focus-visible,
.mk-rowbtn:focus-visible,
.mk-result:focus-visible,
.mk-why__summary:focus-visible,
.mk-sheet-handle:focus-visible,
.mk-check:focus-visible,
.mk-range:focus-visible,
.mk-card__title:focus-visible {
  outline: var(--focus-w, 2px) solid var(--c-focus);
  outline-offset: 2px;
}

.mk-seg__input:focus-visible + .mk-seg__text {
  outline: var(--focus-w, 2px) solid var(--c-focus);
  outline-offset: -4px;
}

/* ---- buttons ------------------------------------------------------------------------------------ */

.mk-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 40px;
  padding: var(--sp-2) var(--sp-4);
  border: 1.5px solid var(--c-ink);
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--c-ink);
  font: inherit;
  font-size: var(--fs-md);
  font-weight: 650;
  line-height: 1.2;
  text-align: center;
  cursor: pointer;
  transition: background-color var(--dur-1, 120ms) ease, border-color var(--dur-1, 120ms) ease;
}

.mk-btn--primary {
  border-color: var(--c-accent);
  background: var(--c-accent);
  color: var(--c-accent-ink);
}

.mk-btn--primary:hover {
  border-color: var(--c-accent-hover, var(--c-accent));
  background: var(--c-accent-hover, var(--c-accent));
}

.mk-btn--ghost:hover {
  background: var(--c-surface-2);
}

.mk-btn--sm {
  min-height: 32px;
  padding: var(--sp-1) var(--sp-3);
  font-size: var(--fs-sm);
}

.mk-iconbtn {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--c-ink-2);
  cursor: pointer;
}

.mk-iconbtn:hover {
  border-color: var(--c-line);
  background: var(--c-surface-2);
  color: var(--c-ink);
}

.mk-icon {
  display: block;
}

.mk-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.mk-actions .mk-btn {
  flex: 1 1 180px;
}

/* ---- cards ---------------------------------------------------------------------------------------- */

.mk-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* the track never outgrows the card on narrow phones */
  gap: var(--sp-4);
  min-width: 0;
  padding: 0 var(--sp-4) var(--sp-4);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  background: var(--c-surface);
  color: var(--c-ink);
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: var(--lh-body, 1.45);
}

.mk-card__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  margin: 0 calc(-1 * var(--sp-4));
  padding: var(--sp-4) var(--sp-4) var(--sp-3);
  border-bottom: 1.5px solid var(--c-ink);
  background-color: var(--c-surface);
  background-image: var(--mk-grid-bg);
  background-size: var(--grid-cell, 24px) var(--grid-cell, 24px);
  border-radius: var(--r-md) var(--r-md) 0 0;
}

.mk-card__titles {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.mk-card__title {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 600;
  line-height: var(--lh-tight, 1.2);
  letter-spacing: -.01em;
  color: var(--c-ink);
  overflow-wrap: anywhere;
}

.mk-card__title:focus {
  outline: none;
}

.mk-card__sub {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

.mk-empty {
  padding: var(--sp-5) var(--sp-4);
  border: 1px dashed var(--c-line);
  border-radius: var(--r-sm);
  background-image: var(--mk-grid-bg);
  background-size: var(--grid-cell, 24px) var(--grid-cell, 24px);
  color: var(--c-ink-2);
  font-size: var(--fs-sm);
  text-align: center;
}

.mk-empty p {
  margin: 0;
}

.mk-areas {
  display: flex;
  flex-wrap: wrap;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

.mk-areas__item + .mk-areas__item::before {
  content: "·";
  margin: 0 var(--sp-2);
  color: var(--c-ink-2);
}

/* ---- composition formula --------------------------------------------------------------------------- */

.mk-formula {
  display: grid;
  gap: var(--sp-1);
}

.mk-formula__value {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 .12em;
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--fs-2xl);
  font-weight: 600;
  line-height: 1.05;
  letter-spacing: -.02em;
}

.mk-formula__part {
  white-space: nowrap;
}

.mk-formula__units,
.mk-formula__plus {
  color: var(--c-ink);
}

.mk-formula__letter {
  font-weight: 700;
}

.mk-formula__none {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

/* Species letter inks (text-safe versions of the VMD legend colours). */
.mk-ink--priede,
.mk-ink--citasPriedes { color: var(--ink-priede); }
.mk-ink--egle,
.mk-ink--citasEgles { color: var(--ink-egle); }
.mk-ink--lapegle { color: var(--mk-ink-lapegle); }
.mk-ink--berzs { color: var(--ink-berzs); }
.mk-ink--melnalksnis { color: var(--ink-melnalksnis); }
.mk-ink--apse,
.mk-ink--papele { color: var(--ink-apse); }
.mk-ink--baltalksnis,
.mk-ink--bligzna { color: var(--ink-baltalksnis); }
.mk-ink--ozols { color: var(--mk-ink-ozols); }
.mk-ink--osis,
.mk-ink--dizskabardis,
.mk-ink--skabardis { color: var(--mk-ink-osis); }
.mk-ink--liepa { color: var(--mk-ink-liepa); }
.mk-ink--goba,
.mk-ink--klava,
.mk-ink--vitols { color: var(--mk-ink-goba); }
.mk-ink--pilads,
.mk-ink--kirsis { color: var(--mk-ink-pilads); }
.mk-ink--noData { color: var(--c-ink-2); }

/* ---- ruled inventory tables -------------------------------------------------------------------------- */

/* contain: inline-size keeps a wide table from stretching the panel; the wrapper scrolls instead. */
.mk-tablewrap {
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  contain: inline-size;
}

.mk-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
  color: var(--c-ink);
}

.mk-table th,
.mk-table td {
  padding: 5px var(--sp-2);
  border-bottom: 1px solid var(--c-line);
  vertical-align: baseline;
}

/* No uppercase: headers carry units (m³/ha, ha, €/ha). */
.mk-table thead th {
  border-bottom: 1.5px solid var(--c-ink);
  font-size: var(--fs-xs);
  font-weight: 700;
  font-stretch: var(--wdth-cond, 75%);
  letter-spacing: .02em;
  text-align: left;
  color: var(--c-ink-2);
  white-space: nowrap;
}

.mk-table tfoot th,
.mk-table tfoot td {
  border-top: 1.5px solid var(--c-ink);
  border-bottom: 0;
  font-weight: 700;
}

.mk-table td.mk-num,
.mk-table .mk-th--num {
  text-align: right;
}

.mk-th--text,
.mk-table tbody th,
.mk-table tfoot th {
  text-align: left;
}

.mk-table abbr[title] {
  text-decoration: underline dotted;
  text-underline-offset: 2px;
  cursor: help;
}

.mk-eltable th,
.mk-eltable td {
  padding: 5px 4px;
}

.mk-eltable thead th {
  white-space: normal;
  vertical-align: bottom;
}

.mk-eltable .mk-el__sp {
  font-weight: 500;
  white-space: nowrap;
}

.mk-el__letter {
  display: inline-block;
  min-width: 2ch;
  font-family: var(--font-mono);
  font-weight: 700;
}

.mk-method {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
  white-space: nowrap;
}

.mk-volume {
  margin: calc(-1 * var(--sp-2)) 0 0;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

/* ---- chips & status badge -------------------------------------------------------------------------- */

.mk-facts {
  min-width: 0;
}

.mk-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.mk-chip {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px var(--sp-2);
  max-width: 100%;
  min-height: 28px;
  padding: 3px var(--sp-2);
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
  background: var(--c-surface);
  font-size: var(--fs-sm);
  line-height: 1.35;
}

.mk-chip__k {
  font-size: var(--fs-xs);
  font-weight: 650;
  font-stretch: var(--wdth-cond, 75%);
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--c-ink-2);
  white-space: nowrap;
}

.mk-chip__v {
  min-width: 0;
  color: var(--c-ink);
  overflow-wrap: anywhere;
}

.mk-chip__note {
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

.mk-chip--warn {
  border-color: var(--c-warn);
  background: var(--c-warn-soft, transparent);
}

.mk-chip--warn .mk-chip__k {
  color: var(--c-warn);
}

.mk-chip--info {
  border-color: var(--c-info);
  background: var(--c-info-soft, transparent);
}

.mk-chip--muted .mk-chip__v {
  color: var(--c-ink-2);
  font-style: italic;
}

.mk-chip--status {
  align-items: center;
}

.mk-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 650;
  color: var(--c-ink);
}

.mk-badge__dot {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--mk-st, var(--st-none));
  box-shadow: inset 0 0 0 1px rgba(23, 32, 26, .35);
}

/* Stand table: the dot alone; the label is the cell tooltip and visually hidden text. */
.mk-badge--dot {
  display: inline-grid;
  place-items: center;
  vertical-align: middle;
}

.mk-st--mature { --mk-st: var(--st-mature); }
.mk-st--approaching { --mk-st: var(--st-approaching); }
.mk-st--growing { --mk-st: var(--st-growing); }
.mk-st--young { --mk-st: var(--st-young); }
.mk-st--restricted { --mk-st: var(--st-restricted); }
.mk-st--plantation { --mk-st: var(--st-plantation); }
.mk-st--none { --mk-st: var(--st-none); }

/* ---- value block with a scale-bar range rule ---------------------------------------------------------- */

.mk-value {
  display: grid;
  gap: var(--sp-2);
  padding: var(--sp-4);
  border: 1.5px solid var(--c-ink);
  border-radius: var(--r-sm);
  background-color: var(--c-surface);
  background-image: var(--mk-grid-bg);
  background-size: var(--grid-cell, 24px) var(--grid-cell, 24px);
}

.mk-value__big {
  margin: 0;
  font-family: var(--font-accent);
  font-size: var(--fs-2xl);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
  letter-spacing: -.02em;
  color: var(--c-ink);
}

.mk-value__perha {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

.mk-value__note {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

.mk-rule {
  margin: var(--sp-2) 0 var(--sp-1);
  padding: 0 1px;
}

.mk-rule__bar {
  position: relative;
  display: flex;
  height: 8px;
  border: 1.5px solid var(--c-ink);
}

/* End ticks, like a map scale bar. */
.mk-rule__bar::before,
.mk-rule__bar::after {
  content: "";
  position: absolute;
  top: -6px;
  bottom: -6px;
  width: 1.5px;
  background: var(--c-ink);
}

.mk-rule__bar::before { left: -1.5px; }
.mk-rule__bar::after { right: -1.5px; }

.mk-rule__seg {
  flex: 1 1 0;
  min-width: 4px;
}

.mk-rule__seg--lo {
  background: var(--c-ink);
}

.mk-rule__seg--hi {
  position: relative;
  background: var(--c-surface);
}

/* Mid tick. */
.mk-rule__seg--hi::before {
  content: "";
  position: absolute;
  top: -6px;
  bottom: -6px;
  left: -.75px;
  width: 1.5px;
  background: var(--c-ink);
}

.mk-rule__labels {
  display: flex;
  justify-content: space-between;
  gap: var(--sp-2);
  margin-top: 6px;
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

.mk-valtable {
  margin-top: var(--sp-1);
  background: var(--c-surface);
}

.mk-valtable tbody th {
  font-weight: 500;
}

.mk-valtable tr.is-sub th {
  padding-left: var(--sp-5);
  font-size: var(--fs-xs);
  font-weight: 400;
  color: var(--c-ink-2);
}

.mk-valtable tr.is-sub td {
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

.mk-valtable td {
  white-space: nowrap;
}

/* A range may break after its en dash on narrow phones; the figures themselves never split (their group
   separators are no-break spaces). */
.mk-valtable td .mk-valtable__range {
  white-space: normal;
}

.mk-valtable__mid,
.mk-valtable__range {
  display: block;
}

.mk-valtable__mid {
  font-weight: 600;
}

.mk-valtable__range {
  font-size: var(--fs-xs);
  font-weight: 400;
  color: var(--c-ink-2);
}

.mk-valtable tr.is-total th,
.mk-valtable tr.is-total td {
  border-top: 1.5px solid var(--c-ink);
  border-bottom: 0;
  font-weight: 700;
}

/* ---- "Kāpēc šāda vērtība?" ------------------------------------------------------------------------- */

.mk-why {
  border-top: 1px solid var(--c-line);
  padding-top: var(--sp-2);
}

.mk-why__summary {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 32px;
  font-weight: 650;
  list-style: none;
  cursor: pointer;
}

.mk-why__summary::-webkit-details-marker {
  display: none;
}

.mk-why__summary::before {
  content: "+";
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 18px;
  height: 18px;
  border: 1.5px solid var(--c-ink);
  border-radius: var(--r-sm);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1;
}

.mk-why[open] > .mk-why__summary::before {
  content: "−";
}

.mk-why__body {
  display: grid;
  gap: var(--sp-2);
  padding: var(--sp-2) 0 0;
}

.mk-why__list,
.mk-why__flags {
  display: grid;
  gap: var(--sp-1);
  margin: 0;
  padding-left: 1.6em;
  font-size: var(--fs-sm);
  line-height: var(--lh-body, 1.45);
}

.mk-why__list li::marker {
  font-family: var(--font-mono);
  color: var(--c-ink-2);
}

.mk-why__h {
  margin: var(--sp-1) 0 0;
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--c-ink-2);
}

/* ---- property summary ------------------------------------------------------------------------------ */

.mk-sub {
  display: grid;
  gap: var(--sp-2);
  min-width: 0;
}

.mk-sub > .mk-h3 {
  margin: 0;
}

.mk-kv {
  display: grid;
  margin: 0;
}

.mk-kv__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: 5px 0;
  border-bottom: 1px dotted var(--c-line);
}

.mk-kv dt {
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

.mk-kv dd {
  margin: 0;
  font-size: var(--fs-sm);
  text-align: right;
  color: var(--c-ink);
}

.mk-warnings {
  padding-left: var(--sp-3);
  border-left: 3px solid var(--c-warn);
}

.mk-warnings ul {
  margin: 0;
  padding-left: 1.2em;
  font-size: var(--fs-sm);
}

.mk-assumptions {
  padding-top: var(--sp-3);
  border-top: 1px solid var(--c-line);
}

/* ---- HTML/CSS bar charts ----------------------------------------------------------------------------- */

.mk-chart {
  min-width: 0;
}

/* One shared grid, so every track starts and ends at the same x. */
.mk-bars {
  display: grid;
  grid-template-columns: minmax(84px, 34%) minmax(40px, 1fr) auto;
  align-items: center;
  gap: 6px var(--sp-2);
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--fs-sm);
}

.mk-bar {
  display: contents;
}

.mk-bar__label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.mk-bar__track {
  position: relative;
  height: 14px;
  overflow: hidden;
  border-radius: 1px;
  background: var(--c-surface-2);
}

.mk-bar__fill {
  display: block;
  height: 100%;
  box-shadow: inset 0 0 0 1px rgba(29, 43, 42, .35);
}

.mk-bar__value {
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
  text-align: right;
  white-space: nowrap;
}

/* Very narrow screens: "52,6 ha · 181 500 €" breaks after the dot instead of widening the panel. */
@media (max-width: 340px) {
  .mk-bar__value {
    white-space: normal;
  }
}

/* ---- swatches, theme selector, legend ----------------------------------------------------------------- */

.mk-swatch {
  display: inline-block;
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 1px;
  box-shadow: inset 0 0 0 1px rgba(23, 32, 26, .35);
}

/* Outline samples sit on light "map paper" so they read the same in both themes. */
.mk-swatch--dashed,
.mk-swatch--selected {
  width: 14px;
  height: 14px;
  background: var(--sp-nodata, #f4eeea);
  box-shadow: none;
}

.mk-swatch--dashed {
  border: 1.5px dashed var(--map-ink, #1d2b2a);
}

.mk-swatch--selected {
  border: 2px solid var(--map-ink, #1d2b2a);
  box-shadow: 0 0 0 2px var(--map-halo, #fff), 0 0 0 3px var(--c-line);
}

.mk-ramp {
  display: inline-flex;
  flex: none;
  box-shadow: inset 0 0 0 1px rgba(23, 32, 26, .35);
}

.mk-ramp > span {
  width: 8px;
  height: 12px;
}

.mk-seg {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.mk-seg__legend {
  padding: 0;
  margin-bottom: 6px;
}

.mk-seg__opts {
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
  border: 1.5px solid var(--c-ink);
  border-radius: var(--r-sm);
}

.mk-seg__opt {
  position: relative;
  display: flex;
  flex: 1 1 auto;
}

.mk-seg__input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.mk-seg__text {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: var(--sp-1) 10px;
  border-left: 1px solid var(--c-line);
  font-size: var(--fs-sm);
  font-weight: 650;
  text-align: center;
  color: var(--c-ink);
}

.mk-seg__opt:first-child .mk-seg__text {
  border-left: 0;
}

.mk-seg__opt:hover .mk-seg__input:not(:checked) + .mk-seg__text {
  background: var(--c-surface-2);
}

.mk-seg__input:checked + .mk-seg__text {
  background: var(--c-ink);
  color: var(--c-surface);
}

@media (forced-colors: active) {
  .mk-seg__input:checked + .mk-seg__text {
    outline: 2px solid CanvasText;
    outline-offset: -4px;
  }

  /* Data colours (status dots, legend swatches, bars) carry meaning: keep them in high-contrast mode. */
  .mk-badge__dot,
  .mk-swatch,
  .mk-ramp,
  .mk-bar__fill {
    forced-color-adjust: none;
  }
}

.mk-legendhost {
  display: grid;
  gap: var(--sp-3);
}

.mk-legend {
  display: grid;
  gap: var(--sp-2);
}

.mk-legend__list {
  display: grid;
  gap: var(--sp-1);
  margin: 0;
  padding: 0;
  list-style: none;
}

.mk-legend__item {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 22px;
  font-size: var(--fs-sm);
}

.mk-legend__label {
  flex: 1;
  min-width: 0;
}

.mk-legend__n {
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

.mk-legend__note {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

/* ---- sortable stand table ------------------------------------------------------------------------------ */

.mk-tablehost {
  min-width: 0;
}

/*
 * The panel scrolls, not the table: no nested scroll area (no double scrollbar), and the sticky header
 * sticks to the top of the scrolling panel. The wrapper is a size container: value columns come first
 * and fit a 420 px panel or a phone; Sastāvs and A are dropped (not scrolled) when there is no room.
 */
.mk-standwrap {
  min-width: 0;
  container-type: inline-size;
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
  background: var(--c-surface);
}

@supports not (container-type: inline-size) {
  .mk-standwrap {
    overflow-x: auto;
  }
}

.mk-standtable {
  font-size: var(--fs-xs);
}

.mk-standtable th,
.mk-standtable td {
  padding: 4px 5px;
  white-space: nowrap;
}

/* The row button has its own padding. */
.mk-standtable .mk-td--stand {
  padding-right: 2px;
  padding-left: 2px;
}

.mk-standtable .mk-td--status {
  width: 1%;
  text-align: center;
}

.mk-standtable thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 0;
  border-bottom: 0;
  background: var(--c-surface);
  box-shadow: inset 0 -1.5px 0 var(--c-ink);
}

/*
 * Measured min-content widths (Manrope, fixture 80640060853): value columns 274 px, + A 307 px,
 * + Sastāvs 373 px; without €/ha 225 px. Content width is 307 px on a 375 px phone, ≈292 px on 360 px
 * and 320 px in the 420 px desktop panel; each band keeps ≥ 16 px of slack for longer values.
 */
@container (max-width: 409.98px) {
  .mk-standtable .mk-col--composition {
    display: none;
  }
}

@container (max-width: 329.98px) {
  .mk-standtable .mk-col--age {
    display: none;
  }
}

/* Small phones (≤ 350 px) and docked preview panes: €/ha is value ÷ area, so it goes next. */
@container (max-width: 289.98px) {
  .mk-standtable .mk-col--perHa {
    display: none;
  }

  .mk-standtable th,
  .mk-standtable td {
    padding-inline: 3px;
  }
}

.mk-sortbtn {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  min-height: 32px;
  padding: var(--sp-1) var(--sp-2);
  border: 0;
  background: transparent;
  color: var(--c-ink-2);
  font: inherit;
  font-weight: 700;
  letter-spacing: inherit;
  text-transform: inherit;
  cursor: pointer;
}

.mk-standtable .mk-sortbtn {
  gap: 3px;
  padding: var(--sp-1) 5px;
}

.mk-th--num .mk-sortbtn {
  justify-content: flex-end;
}

.mk-th--dot .mk-sortbtn {
  justify-content: center;
}

.mk-sortbtn:hover,
.mk-sortbtn.is-active {
  color: var(--c-ink);
}

.mk-sortbtn__icon {
  font-size: 9px;
  line-height: 1;
  color: var(--c-ink-3, var(--c-ink-2));
}

.mk-sortbtn.is-active .mk-sortbtn__icon {
  color: var(--c-accent-text);
}

.mk-standtable tbody tr {
  cursor: pointer;
}

.mk-standtable tbody tr:hover {
  background: var(--c-surface-2);
}

.mk-standtable tbody tr.is-selected {
  background: var(--c-accent-soft, var(--c-surface-2));
}

.mk-standtable tbody tr.is-selected > :first-child {
  box-shadow: inset 3px 0 0 var(--c-accent);
}

.mk-rowbtn {
  min-width: 24px;
  min-height: 24px;
  padding: 2px 4px;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 650;
  text-decoration: underline;
  text-decoration-color: var(--c-line);
  text-underline-offset: 3px;
  cursor: pointer;
}

.mk-rowbtn:hover {
  text-decoration-color: currentColor;
}

.mk-td--comp {
  font-size: var(--fs-xs);
}

.mk-table__footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  margin-top: var(--sp-2);
}

.mk-table__count {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--c-ink-2);
}

/* ---- layer panel ------------------------------------------------------------------------------------------ */

.mk-layers {
  display: grid;
  gap: var(--sp-4);
  color: var(--c-ink);
}

.mk-layers__overlays {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.mk-layerlist {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
  border-top: 1px solid var(--c-line);
}

.mk-layer {
  display: grid;
  gap: 2px;
  padding: var(--sp-2) 0;
  border-bottom: 1px solid var(--c-line);
}

.mk-layer__row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-1) var(--sp-2);
}

.mk-check {
  flex: none;
  width: 20px;
  height: 20px;
  margin: 2px;
  accent-color: var(--c-accent);
  cursor: pointer;
}

.mk-layer__name {
  display: flex;
  flex: 1;
  align-items: center;
  min-height: 24px;
  font-size: var(--fs-md);
  cursor: pointer;
}

.mk-layer__hint {
  font-size: var(--fs-xs);
  font-style: italic;
  color: var(--c-warn);
}

.mk-layer__opacity {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding-left: 32px;
}

.mk-range {
  flex: 1;
  min-width: 0;
  min-height: 24px;
  margin: 0;
  accent-color: var(--c-accent);
  cursor: pointer;
}

.mk-range:disabled {
  opacity: .45;
  cursor: not-allowed;
}

.mk-layer__pct {
  width: 5ch;
  font-size: var(--fs-xs);
  text-align: right;
  color: var(--c-ink-2);
}

/* ---- search results ---------------------------------------------------------------------------------------- */

.mk-results {
  padding: var(--sp-1) 0;
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
  background: var(--c-surface);
  box-shadow: var(--shadow-2);
  color: var(--c-ink);
}

.mk-results__count {
  padding: var(--sp-1) var(--sp-3);
}

.mk-results__list {
  max-height: 50vh;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.mk-result {
  display: block;
  width: 100%;
  min-height: 40px;
  padding: var(--sp-2) var(--sp-3);
  border: 0;
  border-top: 1px solid var(--c-line);
  background: transparent;
  color: var(--c-ink);
  font: inherit;
  font-size: var(--fs-sm);
  text-align: left;
  cursor: pointer;
}

.mk-result:hover,
.mk-result:focus-visible {
  background: var(--c-surface-2);
}

.mk-results__empty {
  margin: 0;
  padding: var(--sp-3);
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
  background: var(--c-surface);
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

/* ---- messages, toasts ------------------------------------------------------------------------------------------ */

.mk-msg {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-3);
  border: 1px solid var(--c-line);
  border-left-width: 4px;
  border-radius: var(--r-sm);
  background: var(--c-surface);
  color: var(--c-ink);
}

.mk-msg--error {
  border-color: var(--c-danger);
  background: var(--c-danger-soft, var(--c-surface));
}

.mk-msg--hint {
  border-color: var(--c-info);
  background: var(--c-info-soft, var(--c-surface));
}

.mk-msg--inline {
  display: block;
  margin: 0;
  font-size: var(--fs-sm);
}

.mk-msg__text {
  flex: 1;
  margin: 0;
  padding-top: 6px;
  font-size: var(--fs-sm);
  line-height: var(--lh-body, 1.45);
}

.mk-msg__actions {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
}

.mk-toasts {
  position: fixed;
  left: 50%;
  bottom: calc(var(--sp-5) + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-toast, 110);
  display: grid;
  gap: var(--sp-2);
  width: min(calc(100vw - 32px), 420px);
  transform: translateX(-50%);
  pointer-events: none;
}

.mk-toast {
  padding: 10px 14px;
  border-radius: var(--r-md);
  background: var(--c-ink);
  color: var(--c-surface);
  box-shadow: var(--shadow-2);
  font-size: var(--fs-sm);
  pointer-events: auto;
  animation: mk-toast-in var(--dur-2, 220ms) var(--ease-out, ease-out);
  transition: opacity var(--dur-2, 220ms) ease, transform var(--dur-2, 220ms) ease;
}

.mk-toast__text {
  margin: 0;
}

.mk-toast--warn {
  box-shadow: inset 4px 0 0 var(--c-warn), var(--shadow-2);
}

.mk-toast.is-leaving {
  opacity: 0;
  transform: translateY(6px);
}

@keyframes mk-toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* ---- loading skeletons ------------------------------------------------------------------------------------------- */

.mk-skeleton {
  display: grid;
  gap: var(--sp-3);
  padding: var(--sp-4);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  background: var(--c-surface);
  animation: mk-fade-in var(--dur-2, 220ms) ease 150ms both;
}

.mk-skeleton__msg {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--c-ink-2);
}

.mk-skeleton__body {
  display: grid;
  gap: 10px;
}

.mk-skel-line {
  display: block;
  height: 12px;
  border-radius: var(--r-sm);
  background: linear-gradient(90deg, var(--c-surface-2) 0 40%, var(--c-bg) 50%, var(--c-surface-2) 60% 100%);
  background-size: 250% 100%;
  animation: mk-shimmer 1.4s linear infinite;
}

.mk-skel-line--lg {
  height: 32px;
}

.mk-spinner {
  flex: none;
  width: 14px;
  height: 14px;
  border: 2px solid var(--c-line);
  border-top-color: var(--c-accent);
  border-radius: 50%;
  animation: mk-spin .8s linear infinite;
}

@keyframes mk-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes mk-shimmer {
  from { background-position: 100% 0; }
  to { background-position: 0 0; }
}

@keyframes mk-spin {
  to { transform: rotate(360deg); }
}

/* ---- mobile sheet handle (geometry lives in app.css, keyed on data-sheet-state) ------------------------------------ */

.mk-sheet-handle {
  display: none;
}

@media (max-width: 899px) {
  .mk-sheet-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 28px;
    padding: var(--sp-2) 0;
    border: 0;
    background: transparent;
    cursor: grab;
    touch-action: none;
  }

  .mk-sheet-handle:active {
    cursor: grabbing;
  }

  .mk-sheet-handle__bar {
    width: 44px;
    height: 5px;
    border-radius: 3px;
    background: var(--c-ink-3, var(--c-ink-2));
  }

  .mk-toasts {
    top: calc(72px + env(safe-area-inset-top, 0px));
    bottom: auto;
  }

  .mk-card__title {
    font-size: var(--fs-lg);
  }

  .mk-formula__value,
  .mk-value__big {
    font-size: var(--fs-xl);
  }
}

/* ---- motion preferences ----------------------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .mk-skeleton,
  .mk-skel-line,
  .mk-spinner,
  .mk-toast {
    animation: none;
  }

  .mk-btn,
  .mk-toast {
    transition: none;
  }
}
