/* IP Picker — visual direction.
 *
 * This is a network-operations tool: someone watches it allocate and release
 * addresses, often for hours. So it borrows from the NOC / terminal tradition
 * rather than the generic SaaS dashboard — near-black ground, a single amber
 * signal colour, and monospace treated as a first-class typeface for the data
 * itself. IP addresses, counters and IDs are the content here; they get the
 * tabular figures and the visual weight, while chrome stays quiet.
 *
 * The one deliberate risk: amber over blue. Blue is the default every admin
 * panel reaches for; amber reads as instrumentation, and it keeps green
 * (matched) and red (failed) unambiguous as status colours rather than
 * competing with the brand.
 */

:root {
  color-scheme: dark;

  --bg: #0b0d10;
  --surface: #13161b;
  --surface-2: #1b1f26;
  --border: #262c36;
  --border-bright: #333b48;

  --text: #e8eaed;
  --muted: #8a929f;
  --dim: #5d6673;

  --accent: #f0a13c;
  --accent-soft: rgba(240, 161, 60, 0.13);
  --accent-hover: #ffb356;

  --ok: #3ddc97;
  --ok-soft: rgba(61, 220, 151, 0.12);
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, 0.12);
  --warn: #f0a13c;
  --warn-soft: rgba(240, 161, 60, 0.13);

  --radius: 8px;
  --radius-sm: 5px;

  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

  /* Spring-ish easing, in the spirit of stiffness/damping rather than a flat
     duration curve. Motion here has a job: it says "an attempt just landed",
     "the counter moved", "this one matched". A run can go on for hours, so
     anything decorative would wear thin fast — these are short and few. */
  --spring: cubic-bezier(.34, 1.56, .64, 1);   /* overshoots, then settles */
  --glide:  cubic-bezier(.22, 1, .36, 1);      /* fast out, soft landing */

  /* Brand-consistent form controls: checkboxes and range inputs pick this up
     natively, so they match without being rebuilt as custom components. */
  accent-color: var(--accent);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }

/* Data — addresses, counters, ids. Tabular figures keep columns aligned as
   values change during polling, so numbers don't shift under the eye. */
.mono {
  font-family: var(--mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------------- Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; }
.login-card h1 {
  margin: 0 0 4px; font-size: 20px; letter-spacing: -0.02em;
}
.login-card h1::before {
  content: "▚"; color: var(--accent); margin-right: 9px;
}
.login-card label { display: block; margin: 16px 0 0; }
.login-card input { width: 100%; }
.login-card button { width: 100%; margin-top: 20px; }

/* --------------------------------------------------------------- Layout */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 13px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-right { display: flex; align-items: center; gap: 12px; }
.brand { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.brand::before { content: "▚"; color: var(--accent); margin-right: 8px; }
.brand .back::before { content: none; }
.back { font-size: 14px; color: var(--muted); }
.back:hover { color: var(--text); }

.container {
  max-width: 1080px; margin: 22px auto; padding: 0 22px;
  display: grid; gap: 18px;
  /* Grid items default to min-width:auto, so a wide table inside a card
     stretches the card past the viewport instead of scrolling within its own
     .table-wrap — which drags the whole page into horizontal scroll. */
  grid-template-columns: minmax(0, 1fr);
}
.card { min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
}
.card h2 {
  margin: 0 0 4px; font-size: 15px; font-weight: 600;
  letter-spacing: 0.02em; text-transform: uppercase; color: var(--muted);
}
.section-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px; gap: 12px;
}

/* ---------------------------------------------------------------- Forms */
label { display: block; font-weight: 500; margin-bottom: 6px; }
label small { display: block; margin-top: 5px; font-weight: 400; color: var(--muted); }

/* Text-like fields only. A checkbox caught by this rule gets a text field's
   padding and background, which swallows the native control entirely — the
   box disappears and there's no way to tell a toggle's state. */
input:not([type="checkbox"]):not([type="radio"]) {
  margin-top: 6px; padding: 9px 11px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px; font-family: inherit;
  transition: border-color .15s, background-color .15s;
}
input:not([type="checkbox"]):not([type="radio"]):hover {
  border-color: var(--border-bright);
}
input:not([type="checkbox"]):not([type="radio"]):focus {
  outline: none; border-color: var(--accent); background: var(--surface-2);
}

/* Checkboxes keep their native rendering, tinted by accent-color. Sized up a
   little because the default is small for a control that carries state. */
input[type="checkbox"] {
  width: 16px; height: 16px; margin: 0;
  flex: 0 0 auto; cursor: pointer;
}

/* Values are data: give them the data typeface. */
input[type="text"], input[type="number"], input[type="password"] {
  font-family: var(--mono); font-size: 13px; font-variant-numeric: tabular-nums;
}

/* Flag a bad value only after the field has been interacted with, so a form
   doesn't greet you in red before you've typed anything. */
input:user-invalid { border-color: var(--danger); }

.run-form { display: grid; gap: 13px; margin-top: 12px; }
.run-form label input { width: 100%; }
.form-msg { font-size: 13px; }
.form-msg.ok { color: var(--ok); }
.form-msg.error { color: var(--danger); }

.field-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 13px;
}
.field-row label input { width: 100%; }
.btn-row { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; margin-top: 4px; }

/* Checkbox row: control and label on one line, the explanatory <small> wrapped
   onto its own line beneath, indented to line up with the label text.
   Grid rather than flex — with flex the <small> becomes a third item and gets
   squeezed into a column of its own instead of dropping below. */
.checkbox {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: baseline;
  column-gap: 9px; row-gap: 3px;
  font-weight: 500;
}
.checkbox input { margin: 0; width: 15px; height: 15px; align-self: center; }
.checkbox small {
  grid-column: 2;          /* under the label text, not under the box */
  font-weight: 400; margin-top: 0;
}

/* A toggled-off group dims itself. :has() lets the container react to its own
   checkbox, so the state lives in CSS instead of a JS class toggle. */
.toggle-group { transition: opacity .18s; }
.toggle-group:has(.group-switch:not(:checked)) .toggle-fields {
  opacity: .4;
}
@supports not selector(:has(*)) {
  .toggle-fields.is-off { opacity: .4; }
}

.sep { border: 0; border-top: 1px solid var(--border); margin: 18px 0 6px; }

/* The order of the pauses isn't obvious from the field names alone, so the
   settings page spells the cycle out once. */
.cycle-note {
  margin: 4px 0 14px; padding: 10px 12px;
  background: var(--bg); border: 1px solid var(--border);
  border-left: 2px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 12.5px; color: var(--muted); line-height: 1.7;
}
.cycle-pause {
  color: var(--accent); font-family: var(--mono); font-size: 12px;
}

code {
  font-family: var(--mono); font-size: 12px;
  background: var(--surface-2); padding: 1px 5px;
  border-radius: 4px; color: var(--accent);
}

/* -------------------------------------------------------------- Buttons */
.btn {
  display: inline-block; padding: 8px 15px;
  border: 1px solid var(--border-bright);
  border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
  cursor: pointer; font-size: 13px; font-family: inherit; font-weight: 500;
  transition: background-color .15s, border-color .15s, opacity .15s,
              transform .18s var(--spring);
}
.btn:hover:not(:disabled) { background: var(--border); border-color: var(--dim); }
.btn:active:not(:disabled) { transform: translateY(1px) scale(.985); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn.primary {
  background: var(--accent); border-color: var(--accent); color: #17120a;
  font-weight: 600;
}
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: var(--danger-soft); }

.btn.ghost { background: transparent; border-color: var(--border); color: var(--muted); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn.sm { padding: 4px 11px; font-size: 12px; }

/* `display` on .btn overrides the user-agent rule behind the `hidden`
   attribute, so a hidden button would still render. Restore it explicitly. */
[hidden] { display: none !important; }

/* --------------------------------------------------------------- Tables */
.table-wrap { overflow-x: auto; margin: 0 -22px; padding: 0 22px; }
table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left; padding: 9px 11px;
  border-bottom: 1px solid var(--border);
}
th {
  color: var(--dim); font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.06em;
  position: sticky; top: 0; background: var(--surface);
}
td { font-variant-numeric: tabular-nums; }
tbody tr { transition: background-color .12s; }
tbody tr:hover { background: var(--surface-2); }
tr:last-child td { border-bottom: none; }

/* A row that just arrived springs in from the top of the table. During a run
   this is the signal that an attempt completed — the table is polled, so
   without it rows would appear with no indication anything happened. */
@keyframes row-in {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: none; }
}
tbody tr.is-new { animation: row-in .42s var(--spring) both; }

/* The matched row is the whole point of a run — mark it unmistakably. */
.matched-row { background: var(--ok-soft); }
.matched-row:hover { background: rgba(61, 220, 151, 0.17); }
.matched-row td:first-child { box-shadow: inset 2px 0 0 var(--ok); }

/* --------------------------------------------------------------- Badges */
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
  font-family: var(--mono); letter-spacing: 0.01em;
  white-space: nowrap;
}
.badge.pending, .badge.tried, .badge.cancelled {
  background: var(--surface-2); color: var(--muted);
}
.badge.running {
  background: var(--accent-soft); color: var(--accent);
}
/* A live run gets a slow pulse: at a glance you can tell it's still working
   without reading the label. Respects reduced-motion below. */
.badge.running::before {
  content: "●"; margin-right: 5px; font-size: 8px; vertical-align: 1px;
  animation: pulse 1.8s ease-in-out infinite;
}
.badge.waiting_quota {
  background: var(--warn-soft); color: var(--warn);
}
.badge.waiting_quota::before { content: "◷"; margin-right: 5px; }
.badge.matched { background: var(--ok-soft); color: var(--ok); }
.badge.released, .badge.exhausted { background: var(--surface-2); color: var(--dim); }
.badge.failed { background: var(--danger-soft); color: var(--danger); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important;
      transition-duration: .01ms !important; }
}

/* --------------------------------------------------------------- Alerts */
.alert { padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13px; }
.alert.error {
  background: var(--danger-soft); color: var(--danger);
  border-left: 2px solid var(--danger);
}

/* -------------------------------------------------------------- Summary */
.summary-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 16px 18px;
}
/* Grid cells default to min-width:auto, so a long CIDR or region list refuses
   to shrink and overruns the neighbouring columns instead of wrapping. */
.summary-grid > div { min-width: 0; }
.summary-grid .label {
  display: block; color: var(--dim); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 5px;
}
/* Long comma-separated values wrap on the commas rather than pushing wide. */
.summary-grid .mono {
  display: block;
  overflow-wrap: anywhere;
  line-height: 1.45;
}
.summary-grid .full { grid-column: 1 / -1; }

/* Attempts counter is the number being watched — let it carry some weight. */
.summary-grid .metric {
  font-family: var(--mono); font-size: 17px; font-variant-numeric: tabular-nums;
  display: inline-block;  /* transform needs a block box */
}

/* The counter bumps when it changes. With a 3s poll and a delay of a minute
   between attempts, the number moves rarely — a small overshoot is what makes
   that moment legible without staring at it. */
@keyframes metric-bump {
  0%   { transform: none; }
  35%  { transform: scale(1.18); color: var(--accent); }
  100% { transform: none; }
}
.metric.is-bumping { animation: metric-bump .5s var(--spring); }

/* Finding a matching address is the entire point of the run, so it gets the
   one genuinely loud moment in the interface: a green ring pulsing outward. */
@keyframes match-flash {
  0%   { box-shadow: 0 0 0 0 rgba(61, 220, 151, .5); }
  70%  { box-shadow: 0 0 0 14px rgba(61, 220, 151, 0); }
  100% { box-shadow: 0 0 0 0 rgba(61, 220, 151, 0); }
}
.match-cell {
  display: inline-block; border-radius: var(--radius-sm);
  padding: 1px 6px; margin: -1px -6px;
}
.match-cell.is-flashing { animation: match-flash 1.1s ease-out 2; }

@media (max-width: 620px) {
  .container { padding: 0 15px; margin: 15px auto; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .topbar { padding: 11px 15px; }
}

/* Region picker on the run form */
.field-label { display: block; font-weight: 500; margin-bottom: 4px; }
.region-list {
  /* Fixed grid rather than flex-wrap: the chips are uniform, so equal columns
     stop them collapsing to content width — which is what shredded the labels
     into "gis-" / "1" fragments on narrower viewports. */
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(98px, 1fr));
  gap: 7px;
  margin-top: 9px;
  font-size: 13px;
}
.region-item {
  display: flex; align-items: center; gap: 7px;
  margin: 0; font-weight: 400; cursor: pointer;
  white-space: nowrap; overflow: hidden;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  transition: border-color .15s, background-color .15s,
              transform .22s var(--spring);
}
/* The global `input` rule adds margin-top and a monospace face meant for text
   fields; neither belongs on a checkbox sitting inline with its label.
   flex:0 0 auto keeps the box from being squeezed by the label beside it. */
.region-item input[type="checkbox"] {
  margin: 0; flex: 0 0 auto;
  width: 14px; height: 14px;
}
.region-item .mono { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; }
.region-item:hover { border-color: var(--border-bright); transform: translateY(-1px); }
.region-item:has(input:checked), .region-item.is-checked { transform: translateY(-1px); }
.region-item:has(input:checked) {
  border-color: var(--accent); background: var(--accent-soft);
}
/* Fallback for browsers without :has() — JS toggles this class instead. */
.region-item.is-checked {
  border-color: var(--accent); background: var(--accent-soft);
}
.region-cell { color: var(--muted); }
