/*
 * Searchling admin console.
 *
 * Every rule is nested under `.adm` (the console shell) or `.auth` (the sign-in
 * card) on purpose: propshaft's `stylesheet_link_tag :app` bundles every file in
 * this directory, so an unscoped `table {}` here would repaint the public search
 * page. Hand-written, no framework, dark mode via prefers-color-scheme plus an
 * explicit [data-theme] override.
 */

.adm,
.auth {
  color-scheme: light;

  --surface-0: #f3f3f1;
  --surface-1: #ffffff;
  --surface-2: #f8f8f6;
  --surface-3: #eeeeea;
  --border: #e2e1db;
  --border-strong: #cdccc4;

  --text-1: #16160f;
  --text-2: #53524d;
  --text-3: #85847b;

  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;

  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --info: #2a78d6;
  --muted-status: #8d8c83;

  --sidebar-bg: #1c1c1a;
  --sidebar-ink: #d8d7cd;
  --sidebar-ink-dim: #8f8e84;
  --sidebar-active: #2a2a27;

  --radius: 8px;
  --radius-sm: 5px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 1px 1px rgb(0 0 0 / 4%);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) :is(.adm, .auth) {
    color-scheme: dark;

    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #212120;
    --surface-3: #2a2a28;
    --border: #33332f;
    --border-strong: #45443f;

    --text-1: #f6f5f0;
    --text-2: #c3c2b7;
    --text-3: #8e8d84;

    --accent: #3987e5;
    --series-1: #3987e5;
    --info: #3987e5;
    --muted-status: #7d7c74;

    --sidebar-bg: #171716;
    --sidebar-active: #262624;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

:root[data-theme="dark"] :is(.adm, .auth) {
  color-scheme: dark;

  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #212120;
  --surface-3: #2a2a28;
  --border: #33332f;
  --border-strong: #45443f;

  --text-1: #f6f5f0;
  --text-2: #c3c2b7;
  --text-3: #8e8d84;

  --accent: #3987e5;
  --series-1: #3987e5;
  --info: #3987e5;
  --muted-status: #7d7c74;

  --sidebar-bg: #171716;
  --sidebar-active: #262624;
}

/* ---------------------------------------------------------------- shells */

body.adm {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-1);
  font-family: var(--sans);
  font-size: 13.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.adm {
  .skip-link {
    position: absolute;
    left: -9999px;

    &:focus {
      left: 8px;
      top: 8px;
      z-index: 10;
      padding: 8px 12px;
      background: var(--surface-1);
      border: 1px solid var(--border-strong);
      border-radius: var(--radius-sm);
    }
  }

  .adm-shell {
    display: grid;
    grid-template-columns: 216px minmax(0, 1fr);
    min-height: 100vh;
  }

  /* ------------------------------------------------------------ sidebar */

  .adm-sidebar {
    display: flex;
    flex-direction: column;
    gap: 18px;
    padding: 16px 12px;
    background: var(--sidebar-bg);
    color: var(--sidebar-ink);
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
  }

  .adm-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 8px 12px;
    border-bottom: 1px solid rgb(255 255 255 / 8%);
  }

  .adm-brand-mark {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 7px;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
  }

  .adm-brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.15;

    strong { color: #fff; font-size: 14px; letter-spacing: 0.01em; }
    small { color: var(--sidebar-ink-dim); font-size: 11px; text-transform: uppercase; letter-spacing: 0.09em; }
  }

  .adm-nav {
    display: flex;
    flex-direction: column;
    gap: 1px;
  }

  .adm-nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 7px 10px;
    border-radius: var(--radius-sm);
    color: var(--sidebar-ink);
    text-decoration: none;
    font-size: 13px;

    &:hover { background: var(--sidebar-active); color: #fff; }

    &.is-active {
      background: var(--sidebar-active);
      color: #fff;
      box-shadow: inset 2px 0 0 var(--accent);
      font-weight: 600;
    }
  }

  .adm-nav-icon {
    width: 16px;
    text-align: center;
    color: var(--sidebar-ink-dim);
    font-size: 12px;
  }

  .adm-sidebar-foot {
    margin-top: auto;
    padding-top: 10px;
    border-top: 1px solid rgb(255 255 255 / 8%);
  }

  .adm-sidebar-link {
    color: var(--sidebar-ink-dim);
    font-size: 12px;
    text-decoration: none;
    padding: 4px 10px;
    display: block;

    &:hover { color: #fff; }
  }

  /* ------------------------------------------------------------- topbar */

  .adm-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  .adm-topbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    padding: 14px 20px;
    background: var(--surface-1);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 5;

    h1 {
      margin: 0;
      font-size: 17px;
      letter-spacing: -0.01em;
    }
  }

  .adm-topbar-sub {
    margin: 2px 0 0;
    color: var(--text-3);
    font-size: 12px;
  }

  .adm-topbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;

    form { margin: 0; }
  }

  .adm-identity {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .adm-identity-email {
    color: var(--text-2);
    font-size: 12px;
  }

  .adm-flashes {
    padding: 12px 20px 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .adm-content {
    padding: 16px 20px 40px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 0;
  }
}

/* ------------------------------------------------------- shared widgets */

:is(.adm, .auth) {
  a { color: var(--accent); }

  a.plain { color: inherit; text-decoration: none; }
  a.plain:hover { text-decoration: underline; }

  .mono { font-family: var(--mono); font-size: 12px; }
  .muted { color: var(--text-3); }
  .bad { color: var(--critical); font-weight: 600; }
  .nowrap { white-space: nowrap; }
  .num { text-align: right; font-variant-numeric: tabular-nums; }
  .note { font-size: 12px; margin: 0; }

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

  .card {
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 16px;
    min-width: 0;
  }

  .narrow-card { max-width: 460px; }

  .card-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 10px;

    h2 {
      margin: 0;
      font-size: 13px;
      text-transform: uppercase;
      letter-spacing: 0.07em;
      color: var(--text-2);
    }

    .muted { font-size: 12px; }
  }

  .card-link { font-size: 12px; text-decoration: none; }
  .card-link:hover { text-decoration: underline; }
  .card-note { margin: 10px 0 0; font-size: 12px; }

  h3.sub {
    margin: 14px 0 6px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-3);
  }

  .cta {
    border-left: 3px solid var(--accent);

    h2 { margin: 0 0 6px; font-size: 15px; }
    p { margin: 0 0 12px; color: var(--text-2); max-width: 62ch; }
  }

  .grid { display: grid; gap: 16px; }
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }

  /* stat tiles -------------------------------------------------------- */

  .tiles {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 10px;
  }

  .tile {
    display: flex;
    flex-direction: column;
    gap: 2px;
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 10px 12px;
  }

  .tile-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-3);
  }

  .tile-value {
    font-size: 24px;
    font-weight: 650;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
    line-height: 1.15;
  }

  .tile-foot { font-size: 11.5px; color: var(--text-3); }

  /* health ------------------------------------------------------------ */

  .health {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 8px;
  }

  .health-item {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-left-width: 3px;
    border-radius: var(--radius-sm);
    background: var(--surface-1);
    font-size: 12.5px;

    strong { display: block; }
    .muted { font-size: 12px; }
  }

  .health-icon {
    font-weight: 700;
    line-height: 1.3;
  }

  .health-good { border-left-color: var(--good); .health-icon { color: var(--good); } }
  .health-info { border-left-color: var(--info); .health-icon { color: var(--info); } }
  .health-warning { border-left-color: var(--warning); .health-icon { color: var(--warning); } }
  .health-serious { border-left-color: var(--serious); .health-icon { color: var(--serious); } }
  .health-critical { border-left-color: var(--critical); .health-icon { color: var(--critical); } }

  /* tables ------------------------------------------------------------ */

  .data {
    width: 100%;
    border-collapse: collapse;
    font-size: 12.5px;
    display: table;

    th, td {
      text-align: left;
      padding: 7px 10px;
      border-bottom: 1px solid var(--border);
      vertical-align: top;
    }

    thead th {
      position: sticky;
      top: 0;
      background: var(--surface-2);
      color: var(--text-3);
      font-size: 11px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.06em;
      white-space: nowrap;
      border-bottom: 1px solid var(--border-strong);
    }

    tbody tr:hover { background: var(--surface-2); }
    tbody tr:last-child td { border-bottom: none; }

    &.compact {
      th, td { padding: 5px 8px; }
    }

    &.kv {
      th {
        width: 38%;
        color: var(--text-3);
        font-weight: 500;
        white-space: nowrap;
      }
    }

    .fill { width: 34%; }
    .actions { white-space: nowrap; text-align: right; }
    .actions form { display: inline-block; margin: 0 0 0 4px; }
  }

  .row-zero td { background: color-mix(in oklab, var(--critical) 7%, transparent); }
  .row-zero:hover td { background: color-mix(in oklab, var(--critical) 12%, transparent); }

  /* Wide tables scroll inside the card, never the page. */
  .card:has(> .data),
  .card:has(> form + .data) { overflow-x: auto; }

  /* pills ------------------------------------------------------------- */

  .pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 1px 7px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.6;
    white-space: nowrap;
    border: 1px solid transparent;
  }

  .pill-icon { font-size: 10px; }

  .pill-good {
    background: color-mix(in oklab, var(--good) 15%, transparent);
    border-color: color-mix(in oklab, var(--good) 35%, transparent);
    color: color-mix(in oklab, var(--good) 78%, var(--text-1));
  }

  .pill-info {
    background: color-mix(in oklab, var(--info) 14%, transparent);
    border-color: color-mix(in oklab, var(--info) 34%, transparent);
    color: color-mix(in oklab, var(--info) 78%, var(--text-1));
  }

  .pill-warning {
    background: color-mix(in oklab, var(--warning) 22%, transparent);
    border-color: color-mix(in oklab, var(--warning) 45%, transparent);
    color: color-mix(in oklab, var(--warning) 55%, var(--text-1));
  }

  .pill-serious {
    background: color-mix(in oklab, var(--serious) 20%, transparent);
    border-color: color-mix(in oklab, var(--serious) 42%, transparent);
    color: color-mix(in oklab, var(--serious) 65%, var(--text-1));
  }

  .pill-critical {
    background: color-mix(in oklab, var(--critical) 15%, transparent);
    border-color: color-mix(in oklab, var(--critical) 38%, transparent);
    color: color-mix(in oklab, var(--critical) 78%, var(--text-1));
  }

  .pill-muted {
    background: var(--surface-3);
    border-color: var(--border);
    color: var(--text-3);
  }

  /* meters ------------------------------------------------------------ */

  .meter {
    display: block;
    height: 6px;
    min-width: 40px;
    border-radius: 3px;
    background: var(--surface-3);
    overflow: hidden;
    margin-top: 5px;
  }

  .meter-fill {
    display: block;
    height: 100%;
    border-radius: 3px;
    background: var(--series-1);
  }

  /* charts ------------------------------------------------------------ */

  .chart {
    width: 100%;
    height: auto;
    display: block;
    overflow: visible;
  }

  .chart-bar path,
  .chart-bar rect { fill: var(--series-1); }
  .chart-bar:hover path,
  .chart-bar:hover rect { fill: color-mix(in oklab, var(--series-1) 78%, var(--text-1)); }

  .chart-bar-zero path,
  .chart-bar-zero rect { fill: var(--border-strong); }

  .chart-grid { stroke: var(--border); stroke-width: 1; }
  .chart-baseline { stroke: var(--border-strong); stroke-width: 1; }
  .chart-axis {
    fill: var(--text-3);
    font-family: var(--sans);
    font-size: 10px;
  }
  .chart-empty { padding: 24px 0; text-align: center; }

  /* feed -------------------------------------------------------------- */

  .feed {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
  }

  .feed-item {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
    align-items: baseline;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 12.5px;

    &:last-child { border-bottom: none; }
  }

  .feed-body {
    display: flex;
    flex-direction: column;
    min-width: 0;

    > span {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .feed-meta { font-size: 11.5px; }

  .linklist {
    list-style: none;
    margin: 0;
    padding: 0;
    font-size: 12.5px;

    li {
      padding: 4px 0;
      border-bottom: 1px solid var(--border);
      display: flex;
      gap: 6px;
      align-items: center;
      flex-wrap: wrap;

      &:last-child { border-bottom: none; }
    }
  }

  .preview {
    margin: 0;
    padding: 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--mono);
    font-size: 12px;
    line-height: 1.55;
    max-height: 340px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
  }

  /* forms ------------------------------------------------------------- */

  .filters {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
  }

  .stack {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .row {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
  }

  .field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--text-2);
  }

  .check {
    display: flex;
    gap: 8px;
    align-items: flex-start;
    font-size: 12.5px;
    color: var(--text-2);
  }

  .inline-form { display: inline-block; margin: 0; }

  .input {
    font: inherit;
    font-size: 13px;
    color: var(--text-1);
    background: var(--surface-1);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 6px 9px;
    min-width: 0;

    &:focus-visible {
      outline: 2px solid color-mix(in oklab, var(--accent) 60%, transparent);
      outline-offset: 1px;
      border-color: var(--accent);
    }

    &.mono { font-family: var(--mono); }
    &.narrow { max-width: 160px; }
  }

  textarea.input { resize: vertical; width: 100%; }

  .filters .input { min-width: 150px; }
  .filters input[type="text"] { flex: 1 1 220px; }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font: inherit;
    font-size: 12.5px;
    font-weight: 550;
    line-height: 1.4;
    padding: 6px 11px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--surface-1);
    color: var(--text-1);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;

    &:hover { background: var(--surface-3); }

    &:focus-visible {
      outline: 2px solid color-mix(in oklab, var(--accent) 60%, transparent);
      outline-offset: 1px;
    }
  }

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

    &:hover { background: color-mix(in oklab, var(--accent) 85%, black); }
  }

  .btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-2);

    &:hover { background: var(--surface-3); color: var(--text-1); }
  }

  .btn-danger {
    color: var(--critical);
    border-color: color-mix(in oklab, var(--critical) 40%, var(--border));

    &:hover { background: color-mix(in oklab, var(--critical) 12%, transparent); }
  }

  .btn-warning {
    color: color-mix(in oklab, var(--serious) 65%, var(--text-1));
    border-color: color-mix(in oklab, var(--serious) 45%, var(--border));

    &:hover { background: color-mix(in oklab, var(--serious) 14%, transparent); }
  }

  .btn-mini { font-size: 11.5px; padding: 3px 8px; }
  .btn-block { width: 100%; justify-content: center; padding: 9px 12px; }

  /* flashes ----------------------------------------------------------- */

  .flash {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    font-size: 12.5px;
    background: var(--surface-1);
  }

  .flash-icon { font-weight: 700; }

  .flash-good {
    border-color: color-mix(in oklab, var(--good) 40%, transparent);
    background: color-mix(in oklab, var(--good) 10%, var(--surface-1));
    .flash-icon { color: var(--good); }
  }

  .flash-critical {
    border-color: color-mix(in oklab, var(--critical) 40%, transparent);
    background: color-mix(in oklab, var(--critical) 10%, var(--surface-1));
    .flash-icon { color: var(--critical); }
  }

  /* pager ------------------------------------------------------------- */

  .pager {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-top: 12px;
    margin-top: 4px;
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-3);
    flex-wrap: wrap;
  }

  .pager-summary { margin-right: auto; }

  .pager-link {
    padding: 4px 9px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    text-decoration: none;

    &.disabled { opacity: 0.4; pointer-events: none; }
  }

  .pager-current { color: var(--text-2); }
}

/* --------------------------------------------------------- sign-in card */

.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background: var(--surface-0);
  color: var(--text-1);
  font-family: var(--sans);
  font-size: 13.5px;
  line-height: 1.45;

  .auth-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgb(0 0 0 / 8%), var(--shadow);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .auth-brand {
    display: flex;
    align-items: center;
    gap: 10px;

    strong { display: block; font-size: 16px; }
    small { color: var(--text-3); font-size: 12px; }
  }

  .auth-mark {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 9px;
    background: var(--accent);
    color: #fff;
    font-size: 19px;
  }

  .auth-foot {
    border-top: 1px solid var(--border);
    padding-top: 12px;
    font-size: 12px;
    text-align: center;
  }
}

/* ---------------------------------------------------------- responsive */

@media (max-width: 1100px) {
  .adm .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .adm {
    .adm-shell { grid-template-columns: minmax(0, 1fr); }

    .adm-sidebar {
      position: static;
      height: auto;
      flex-direction: row;
      align-items: center;
      gap: 12px;
      overflow-x: auto;
      padding: 10px 12px;
    }

    .adm-brand { border-bottom: none; padding: 0 8px 0 0; }
    .adm-nav { flex-direction: row; gap: 4px; }
    .adm-nav-link { white-space: nowrap; }
    .adm-nav-link.is-active { box-shadow: inset 0 -2px 0 var(--accent); }
    .adm-sidebar-foot { margin: 0; border-top: none; padding-top: 0; }
    .adm-topbar { position: static; }
    .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
  }
}

@media (max-width: 620px) {
  .adm .adm-content { padding: 12px 12px 32px; }
  .adm .adm-topbar { padding: 12px; }
}
