:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #eef2f6;
  --border: #d9e0e8;
  --text: #17202b;
  --text-muted: #5b6b7c;
  --accent: #0b74de;
  --accent-soft: #e3f0fc;
  --danger: #d63b3b;
  --ready: #1a8f5a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px rgba(16, 24, 40, 0.04);
  --mono: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #161d25;
    --surface-2: #1d2630;
    --border: #2a3542;
    --text: #e6edf3;
    --text-muted: #8fa1b3;
    --accent: #4aa3ff;
    --accent-soft: #15283c;
    --danger: #ff6b6b;
    --ready: #3fcf8e;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main { flex: 1; padding-bottom: 48px; }

.container { max-width: 1100px; margin: 0 auto; padding: 0 16px; }

a { color: var(--accent); }

/* Header / footer */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 0;
}
.brand {
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 1.1rem;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.brand-mark {
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 2px 7px;
  font-size: 0.85rem;
  font-family: var(--mono);
}
.site-footer {
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 16px 0;
}

/* Hero */
.hero { padding: 32px 0 20px; }
.hero h1 { margin: 0 0 6px; font-size: 1.8rem; }
.hero p { margin: 0; color: var(--text-muted); max-width: 680px; }
.back-link {
  display: inline-block;
  margin-top: 20px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
}
.back-link:hover { color: var(--accent); }

/* Tool cards on the home page */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.tool-card {
  position: relative;
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  color: inherit;
  text-decoration: none;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, transform 0.15s;
}
a.tool-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.tool-card.disabled { opacity: 0.6; }
.tool-card h2 { margin: 12px 0 6px; font-size: 1.15rem; }
.tool-card p { margin: 0 0 14px; color: var(--text-muted); font-size: 0.92rem; }
.tool-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 1.3rem;
}
.tag {
  display: inline-block;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
}
.tag-ready { color: var(--ready); }

/* Panels & fields */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.panel h2 {
  margin: 0 0 12px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.field { display: block; margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }

.input-wrap {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  overflow: hidden;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field.error .input-wrap { border-color: var(--danger); }
.input-wrap input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: 1rem var(--mono);
  padding: 9px 10px;
  outline: none;
}
.unit {
  display: flex;
  align-items: center;
  padding: 0 10px;
  min-width: 64px;
  justify-content: center;
  background: var(--surface);
  border-left: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: var(--mono);
  white-space: nowrap;
}
#converter .unit { min-width: 72px; justify-content: flex-start; }

.btn-ghost {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.panel-head h2 { margin: 0; }
.converter-fields {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px 12px;
}
.converter-fields .field { margin: 0; }
@media (max-width: 900px) {
  .converter-fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Calculator pages: inputs panel + results panel */
.calc-layout {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
  align-items: start;
}
.form-fields { display: grid; gap: 12px; }
.form-fields .field { margin: 0; }
.field-label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.unit-select {
  border: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 0.85rem var(--mono);
  padding: 0 8px;
  min-width: 72px;
  cursor: pointer;
  outline: none;
}

.results { margin: 0; display: grid; gap: 2px; }
.result {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.result:last-child { border-bottom: 0; }
.result.good dd { color: var(--ready); }
.result.bad dd { color: var(--danger); }
.form-group-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
  padding-top: 6px;
  border-top: 1px solid var(--border);
}
.form-group-title:first-child { border-top: 0; padding-top: 0; }
.result dt { color: var(--text-muted); font-size: 0.9rem; }
.result dd { margin: 0; font: 600 1.15rem var(--mono); text-align: right; }
.result dd small { font-weight: 400; color: var(--text-muted); font-size: 0.8rem; }

/* Beam visualization (SVG) */
.viz-panel { margin-top: 16px; }
.viz-block + .viz-block { margin-top: 20px; }
.viz-title { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 6px; }
.viz {
  display: block;
  width: 100%;
  height: auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.viz-caption { margin: 12px 0 0; font-size: 0.8rem; color: var(--text-muted); }

.viz .beam-fill { fill: var(--accent); fill-opacity: 0.16; }
.viz .beam-edge { stroke: var(--accent); stroke-width: 1.5; }
.viz .axis-dash { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 4 4; }
.viz .angle-arc { fill: none; stroke: var(--text); stroke-width: 1.5; }
.viz .dish { fill: none; stroke: var(--text); stroke-width: 5; stroke-linecap: round; }
.viz .feed { stroke: var(--text-muted); stroke-width: 2; }
.viz .feed-dot { fill: var(--accent); }
.viz .grid { stroke: var(--border); stroke-width: 1; }
.viz .tick { stroke: var(--text-muted); stroke-width: 1; }
.viz .half-power { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 5 4; }
.viz .marker-line { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 2 3; }
.viz .pattern-curve { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; }
.viz .dimension { stroke: var(--text); stroke-width: 1.2; }
.viz .arrow-head { fill: var(--text); }
.viz-label {
  font: 12px var(--mono);
  fill: var(--text-muted);
  paint-order: stroke;
  stroke: var(--surface-2);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.viz-strong { fill: var(--text); font-weight: 600; font-size: 13px; }
.viz-accent { fill: var(--accent); }
.viz-empty { font-size: 13px; }

.note { color: var(--text-muted); font-size: 0.85rem; margin-top: 16px; }
.note code { font-family: var(--mono); background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

@media (max-width: 480px) {
  .hero h1 { font-size: 1.5rem; }
  .converter-fields { grid-template-columns: minmax(0, 1fr); }
}

/* Link budget: signal type toggle and mode-specific fields */
[hidden] { display: none !important; }
body[data-signal="cw"] .mod-only,
body[data-signal="mod"] .cw-only { display: none; }

.field-hint { display: block; margin-top: 4px; font: 0.8rem var(--mono); color: var(--accent); }
.field-hint:empty { display: none; }

.select-full {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: 1rem var(--mono);
  padding: 9px 10px;
  outline: none;
  cursor: pointer;
}
.select-full option, .unit-select option { background: var(--surface); color: var(--text); }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block;
  text-align: center;
  padding: 8px 10px;
  font-size: 0.9rem;
  background: var(--surface-2);
  color: var(--text-muted);
}
.segmented label + label span { border-left: 1px solid var(--border); }
.segmented input:checked + span { background: var(--accent); color: #fff; font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -4px; }

/* Channel power spectrum view */
.viz .pch-line { stroke: var(--text); stroke-width: 2; stroke-dasharray: 8 4; }
.viz .rbw-slice { fill: var(--accent); fill-opacity: 0.45; }
.viz .marker-dot { fill: var(--surface-2); stroke: var(--accent); stroke-width: 2.5; }
