:root {
  --bg: #111417;
  --panel: #1b2025;
  --panel-2: #242b31;
  --line: #333c44;
  --text: #e8ecef;
  --muted: #8d99a4;
  --accent: #f0b429;
  --accent-text: #1a1400;
  --warn: #ff6b5b;
  --red: #e5484d;
  --green: #3fb950;
  --blue: #4c8dff;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

h1 { font-size: 18px; margin: 0; letter-spacing: 0.08em; }
h1 span { color: var(--muted); font-weight: 400; letter-spacing: 0; font-size: 14px; margin-left: 6px; }
h2 { font-size: 14px; margin: 0; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
h2 .muted { text-transform: none; letter-spacing: 0; color: var(--accent); }

/* Readout */
.readout {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 2;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.readout-main { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 20px; }
.readout .label, .sheet .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.readout .value {
  font: 700 clamp(30px, 9.5vw, 52px)/1.1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  white-space: nowrap;
}
.readout .compass, .readout .unit { font-size: 0.45em; color: var(--text); font-weight: 600; }
.readout-sub {
  display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px;
  font: 600 18px ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.readout .status { color: var(--warn); }
.readout.invalid .value { color: var(--muted); }

/* Cards */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; }

.grid3 { display: grid; grid-template-columns: 1fr 1fr 0.9fr; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; min-width: 0; }
.field input, .field select {
  width: 100%;
  min-width: 0;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  font: 600 18px ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.field.big input { font-size: 24px; padding: 12px 10px; }
.field input.bad { border-color: var(--warn); color: var(--warn); }
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }

/* Buttons */
.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  font: 600 15px system-ui, sans-serif;
  min-height: 44px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--warn); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.4; }

.tabs { display: flex; gap: 6px; }
.tab {
  min-width: 52px; min-height: 40px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  font: 700 15px ui-monospace, monospace;
  cursor: pointer;
}
.tab.empty { color: var(--muted); }
.tab[aria-selected="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.gun-summary {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font: 600 18px ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; min-height: 40px;
  font: 600 15px system-ui, sans-serif;
  cursor: pointer;
}
.chip[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.chip.red { border-left: 6px solid var(--red); }
.chip.green { border-left: 6px solid var(--green); }
.chip.blue { border-left: 6px solid var(--blue); }
.chip:disabled { opacity: 0.35; }

.hint { color: var(--muted); font-size: 13px; margin: 0; }
.hint code { color: var(--text); }

/* Naming sheet */
.sheet {
  width: min(520px, 100vw);
  max-width: 100vw;
  margin: auto auto 0;
  border: 1px solid var(--line);
  border-radius: 14px 14px 0 0;
  background: var(--panel);
  color: var(--text);
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.6); }
.sheet form { display: flex; flex-direction: column; gap: 10px; }
.sheet h2 { text-transform: none; letter-spacing: 0; color: var(--text); font-size: 18px; }
.sheet h2 span { color: var(--accent); }
.sheet-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; }

/* ---------- Wide screens: grid of every mortar against every target ---------- */

.matrix { display: none; }

@media (min-width: 720px) {
  .layout {
    max-width: 1280px;
    margin: 0 auto;
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
  .matrix {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin: 0 16px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 14px;
    min-width: 0;
  }
}

@media (min-width: 1080px) {
  .layout {
    display: grid;
    grid-template-columns: 460px minmax(0, 1fr);
    align-items: start;
    gap: 16px;
    padding-inline: 16px;
  }
  .layout .app { padding-inline: 0; margin: 0; max-width: none; }
  .matrix {
    margin: 12px 0 0;
    position: sticky;
    top: calc(12px + env(safe-area-inset-top, 0px));
  }
}

.matrix-scroll { overflow-x: auto; }

#matrix-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 6px;
  margin: -6px;
  font-variant-numeric: tabular-nums;
}
#matrix-table th {
  text-align: left;
  vertical-align: bottom;
  font-weight: 600;
  padding: 4px 6px;
}
#matrix-table th.corner { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 500; }
#matrix-table thead th { min-width: 150px; }
#matrix-table th.active-col .col-name { color: var(--accent); }
.col-name, .row-name { display: block; font: 700 16px ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.row-name { font-family: system-ui, sans-serif; }
.col-coords { display: block; font: 500 12px ui-monospace, "SF Mono", Menlo, Consolas, monospace; color: var(--muted); }
#matrix-table th.row-head { vertical-align: middle; border-left: 4px solid transparent; padding-left: 8px; min-width: 130px; }
#matrix-table th.row-head.red { border-left-color: var(--red); }
#matrix-table th.row-head.green { border-left-color: var(--green); }
#matrix-table th.row-head.blue { border-left-color: var(--blue); }

#matrix-table td { padding: 0; }
.cell-empty { color: var(--muted); text-align: center; }
.cell-btn {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: 600 14px ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  cursor: pointer;
}
.cell-btn:hover { border-color: var(--muted); }
.cell-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.cell-btn.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.cell-bearing { font-size: 18px; color: var(--accent); }
.cell-mil { font-size: 22px; font-weight: 700; }
.cell-bearing small, .cell-mil small { font-size: 12px; color: var(--text); font-weight: 600; }
.cell-range { font-size: 12px; color: var(--muted); }
.cell-btn.out .cell-bearing, .cell-btn.out .cell-mil { color: var(--muted); }
.cell-btn.out .cell-mil { font-size: 14px; color: var(--warn); }
