:root {
  color-scheme: dark;
  --bg: #141018;
  --bg-2: #1c1824;
  --bg-3: #2a2434;
  --fg: #e8e4ef;
  --fg-2: #a39caf;
  --accent: #cba6ff;
  --ok: #74e0a3;
  --warn: #ffd36b;
  --bad: #ff6b8b;
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, monospace;
  /* spacing scale — names the paddings/gaps already in use */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --pad-panel: 14px;
  /* type scale */
  --text-xs: 11px;
  --text-sm: 12px;
  --text-base: 13px;
  --text-lg: 14px;
  /* minimum touch target (Apple HIG / WCAG 2.5.8) */
  --tap: 44px;
  /* Safe-area insets: 0 everywhere except notched mobile devices — needs
     viewport-fit=cover in the viewport meta to be non-zero at all. */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

/* Breakpoints (custom properties can't appear in @media — keep in sync):
     narrow: width < 780px  == tauri.conf.json minWidth, so it never fires in
                               the desktop app at zoom 1.0
     phone:  width < 420px */

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  color: var(--fg);
  font: 13px/1.5 system-ui, -apple-system, sans-serif;
}

/* Transparent window: html AND body stay transparent so the body background
   isn't propagated to the (rectangular) canvas — the rounded opaque surface is
   the inner `#app` wrapper, and the transparent corners let the OS draw a
   shadow that follows the rounded shape. */
html,
body {
  background: transparent;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Track the dynamic viewport on mobile (URL-bar collapse); resolves the
     same as the 100% fallback above under desktop Tauri. */
  height: 100dvh;
  /* The app is a fixed frame with internal scroll panes — don't let Android
     pull-to-refresh rubber-band the whole frame. */
  overscroll-behavior: none;
}

#app {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  border-radius: 12px;
  /* hairline edge for definition on top of the OS shadow */
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.07);
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--bg-3);
}
header[data-tauri-drag-region],
header [data-tauri-drag-region] {
  /* draggable empty space in the custom title bar */
  cursor: default;
}

/* Custom window controls (traffic lights) replacing the native decorations. */
#window-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}
#window-controls button {
  width: 12px;
  height: 12px;
  padding: 0;
  border-radius: 50%;
  border: none;
  background: var(--bg-3);
  cursor: pointer;
  font-size: 8px;
  line-height: 1;
  color: transparent;
  transition: filter 0.1s;
}
@media (hover: hover) {
  #window-controls:hover button {
    color: rgba(0, 0, 0, 0.55);
  }
}
#win-close {
  background: #ff5f57;
}
#win-min {
  background: #febc2e;
}
#win-max {
  background: #28c840;
}
#win-close::before {
  content: "\00d7";
}
#win-min::before {
  content: "\2013";
}
#win-max::before {
  content: "+";
}
#window-controls button:active {
  filter: brightness(0.85);
}

/* The traffic lights only make sense under desktop Tauri; the web shim marks
   the document so they disappear in the browser/PWA. A future mobile shell
   sets its own data-host and reuses this gate. */
html[data-host="web"] #window-controls {
  display: none;
}

/* The desktop Rescan (Spotlight re-enumeration) has no web meaning — the
   shim-injected Open/Clear controls replace it there. */
html[data-host="web"] #rescan {
  display: none;
}

header h1 {
  font-size: 13px;
  font-weight: 600;
  margin: 0;
  color: var(--accent);
  letter-spacing: 0.05em;
}

#status {
  font-size: 12px;
  color: var(--fg-2);
  flex: 1;
}

#os-badge {
  font-size: 12px;
  color: var(--fg-2);
}
#os-badge.os-alert {
  color: var(--bad);
  border-color: var(--bad);
  font-weight: 600;
}

button {
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}

@media (hover: hover) {
  button:hover { border-color: var(--accent); }
  button:disabled:hover { border-color: transparent; }
}

button:disabled { opacity: 0.5; cursor: default; }

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

#update-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--accent);
  font-size: 12px;
}

#update-banner[hidden] { display: none; }

#update-text {
  flex: 1;
  color: var(--fg);
}

#update-install { border-color: var(--accent); }

main {
  display: grid;
  grid-template-columns: 1fr;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

main:has(#detail-panel:not([hidden])) {
  grid-template-columns: 1fr 420px;
}

#list-panel { overflow: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 12px;
}

thead th {
  background: var(--bg-2);
  text-align: left;
  padding: 6px 10px;
  font-weight: 600;
  color: var(--fg-2);
  border-bottom: 1px solid var(--bg-3);
}

/* Stickiness is app-table-only: nested data tables in the detail pane (e.g.
   .crypto-dests) would otherwise pin their headers inside the scrolling pane. */
#apps thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

/* Header filter controls. `position: sticky` already establishes a containing
   block, so the per-column popover anchors to its th. */
.th-inner {
  display: flex;
  align-items: center;
  gap: 4px;
}
.th-label { flex: 1; }

.th-filter-btn,
.th-clear-btn {
  background: transparent;
  border: none;
  border-radius: 3px;
  color: var(--fg-2);
  padding: 0 3px;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
@media (hover: hover) {
  .th-filter-btn:hover,
  .th-clear-btn:hover {
    border: none;
    color: var(--accent);
  }
}

thead th.filtered { color: var(--accent); }
thead th.filtered .th-filter-btn { color: var(--accent); }

/* Floated above the trigger button; coordinates are set by positionPopover()
   in main.js so it can escape the scroll container's clipping. */
.th-popover {
  position: fixed;
  padding: 6px;
  background: var(--bg-3);
  border: 1px solid var(--accent);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  z-index: 10;
}
.th-popover[hidden] { display: none; }

.th-popover input,
.th-popover select {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--bg-3);
  border-radius: 3px;
  padding: 3px 6px;
  font-family: var(--mono);
  font-size: 12px;
}

tbody td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--bg-2);
}

/* Inset the last column so its filter trigger isn't overlapped by the
   vertical scrollbar of #list-panel. */
#apps thead th:last-child,
#apps tbody td:last-child {
  padding-right: 20px;
}

/* Row interactivity belongs to the app table only — .crypto-dests rows in the
   detail pane are plain data, not clickable. */
#apps tbody tr { cursor: pointer; }
@media (hover: hover) {
  #apps tbody tr:hover { background: var(--bg-2); }
}
#apps tbody tr.selected { background: var(--bg-3); }

td.name {
  font-family: system-ui, sans-serif;
  color: var(--fg);
}

td.version.stale { color: var(--warn); }
td.version.very-stale { color: var(--bad); }

.framework-tag {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 3px;
  font-size: 11px;
  background: var(--bg-3);
}
/* settings dialog */
dialog {
  padding: 0;
  border: 1px solid var(--bg-3);
  border-radius: 6px;
  background: var(--bg-2);
  color: var(--fg);
  max-width: 520px;
  width: 90vw;
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}
#settings-form {
  display: flex;
  flex-direction: column;
}
#settings-form > header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--bg-3);
  display: flex;
  align-items: center;
  gap: 10px;
}
#settings-form h2 {
  margin: 0;
  font-size: 13px;
  color: var(--accent);
  letter-spacing: 0.05em;
  flex: 1;
}
#settings-close {
  background: transparent;
  border: none;
  color: var(--fg-2);
  font-size: 18px;
  cursor: pointer;
  padding: 0 4px;
}
#settings-form .source {
  padding: 12px 14px;
  border-bottom: 1px solid var(--bg-3);
}
#settings-form .source--primary {
  background: linear-gradient(
    180deg,
    rgba(203, 166, 255, 0.08),
    rgba(203, 166, 255, 0.02)
  );
  border-left: 2px solid var(--accent);
}
#settings-form .source label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
#settings-form .source label.field {
  margin-top: 8px;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 8px;
  align-items: center;
}
#settings-form .source label.field span {
  color: var(--fg-2);
  font-size: 12px;
}
#settings-form input[type="text"],
#settings-form input[type="password"] {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--bg-3);
  border-radius: 3px;
  padding: 4px 8px;
  font-family: var(--mono);
  font-size: 12px;
  width: 100%;
}
#settings-form p.muted {
  color: var(--fg-2);
  font-size: 11px;
  margin: 4px 0 0;
}
#settings-path {
  padding: 10px 14px 0;
  font-size: 11px;
  font-family: var(--mono);
  word-break: break-all;
}
#settings-form > footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--bg-3);
}
.muted { color: var(--fg-2); }

.framework-tag.electron { background: #2a3a60; color: #9dc9ff; }
.framework-tag.tauri { background: #2a4030; color: #9dffc9; }
.framework-tag.cef { background: #4a2a3d; color: #ffc9e0; }
.framework-tag.chromiumbrowser { background: #2a3a55; color: #b5d3ff; }
.framework-tag.nwjs { background: #603820; color: #ffc79d; }
.framework-tag.flutter { background: #184a60; color: #9de0ff; }
.framework-tag.qt { background: #204a2a; color: #b5ffaa; }
.framework-tag.reactnative { background: #204a55; color: #9deff5; }
.framework-tag.wails { background: #60282a; color: #ffaab0; }
.framework-tag.sciter { background: #3a2060; color: #d5aaff; }
.framework-tag.java { background: #5a4018; color: #ffd69d; }
.framework-tag.safari { background: #18425a; color: #9ddbff; }
.framework-tag.native { background: var(--bg-3); color: var(--fg-2); }
.framework-tag.unknown { background: var(--bg-3); color: var(--fg-2); }

.risk {
  display: inline-block;
  min-width: 4em;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  text-align: center;
  font-weight: 600;
}
.risk.ok { background: #1a3a28; color: var(--ok); }
.risk.warn { background: #3d321a; color: var(--warn); }
.risk.bad { background: #3a1a28; color: var(--bad); }
.risk.unknown { background: var(--bg-3); color: var(--fg-2); }

#detail-panel {
  background: var(--bg-2);
  border-left: 1px solid var(--bg-3);
  overflow: auto;
  display: flex;
  flex-direction: column;
}

/* `display: flex` above would silently override the [hidden] attribute's
   default. Restore correct hiding so a closed panel is fully removed from
   layout (no reserved column, no residual flex box). */
#detail-panel[hidden] {
  display: none !important;
}

#detail-panel header {
  border-bottom: 1px solid var(--bg-3);
}

#detail-panel h2 {
  flex: 1;
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

#detail-body {
  padding: 14px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
}

#detail-body h3 {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin: 16px 0 6px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
#detail-body h3:first-child { margin-top: 0; }

#detail-body dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 12px;
  row-gap: 3px;
  margin: 0;
}

#detail-body dt {
  color: var(--fg-2);
}
#detail-body dd {
  margin: 0;
  word-break: break-all;
}

#detail-body .ok { color: var(--ok); }
#detail-body .warn { color: var(--warn); }
#detail-body .bad { color: var(--bad); }

#detail-body ul {
  margin: 4px 0;
  padding-left: 18px;
}

.binmeta-list {
  margin: 3px 0;
}
.binmeta-list > summary {
  cursor: pointer;
}
.binmeta-list ul {
  margin: 4px 0;
  padding-left: 18px;
  max-height: 340px;
  overflow: auto;
}
.binmeta-list li {
  font-size: 11px;
  line-height: 1.6;
}

#detail-body .advisory {
  margin: 4px 0;
  padding: 6px 8px;
  background: var(--bg-3);
  border-radius: 3px;
  border-left: 2px solid var(--fg-2);
  /* Rule ids, asar file paths and code samples are long unbroken tokens; wrap
     them so a finding can't push the detail panel into horizontal scroll. */
  overflow-wrap: anywhere;
}
#detail-body .advisory.high { border-left-color: var(--bad); }
#detail-body .advisory.critical { border-left-color: var(--bad); }
#detail-body .advisory.medium { border-left-color: var(--warn); }

/* Collapsible advisories: the CVE id shows in the summary; the description
   reveals on expand. */
#detail-body details.advisory > summary {
  cursor: pointer;
  user-select: none;
  list-style: none;
}
#detail-body details.advisory > summary::-webkit-details-marker { display: none; }
#detail-body details.advisory > summary::before {
  content: "▸ ";
  color: var(--fg-2);
}
#detail-body details.advisory[open] > summary::before { content: "▾ "; }
#detail-body .advisory-body {
  margin-top: 6px;
  color: var(--fg-2);
  white-space: pre-wrap;
}

/* Runtime / dependency CVE tabs. The list can run to hundreds of entries, so
   only the active panel is shown at a time. */
#detail-body .cve-tabs {
  display: flex;
  gap: 4px;
  margin: 0 0 8px;
  border-bottom: 1px solid var(--bg-3);
}
#detail-body .cve-tab {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 5px 8px;
  margin-bottom: -1px;
  color: var(--fg-2);
  font-family: var(--mono);
}
@media (hover: hover) {
  #detail-body .cve-tab:hover {
    border-color: transparent;
    color: var(--fg);
  }
}
#detail-body .cve-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
#detail-body .cve-panel { display: none; }
#detail-body .cve-panel.active { display: block; }

/* Briefly highlight an advisory a background EUVD update just added to the open
   app, so a newly-published vulnerability is noticeable without the pane
   jumping. */
@keyframes achilles-flash-new {
  from { background: color-mix(in srgb, var(--accent) 38%, transparent); }
  to { background: transparent; }
}
#detail-body .advisory.flash-new { animation: achilles-flash-new 2.4s ease-out; }

@media (prefers-reduced-motion: reduce) {
  #window-controls button { transition: none; }
  #detail-body .advisory.flash-new {
    animation: none;
    background: color-mix(in srgb, var(--accent) 20%, transparent);
  }
}

/* Web-only EUVD snapshot controls, nested under the EUVD source entry in the
   settings dialog and collapsed (hidden) when EUVD is unchecked. */
#euvd-snapshot-settings {
  margin-top: 10px;
  padding-left: 12px;
  border-left: 2px solid var(--bg-3);
}
#euvd-snapshot-settings[hidden] { display: none; }
#euvd-snapshot-settings label { display: block; margin: 6px 0; }
#euvd-snapshot-settings p { margin: 6px 0; }

/* ==================== narrow viewports (width < 780px) ====================
   780px is the desktop Tauri window's minWidth, so nothing in here fires in
   the desktop app at zoom 1.0 — the browser/PWA build is the only consumer.
   Desktop rendering stays untouched by design. */

@media (width < 780px) {
  /* Rounded desktop window chrome is meaningless in a browser tab. */
  #app {
    border-radius: 0;
    box-shadow: none;
  }

  /* The title bar can't fit controls and status side by side: wrap, and give
     the status line (scan progress, every error message, the iOS wasm notice)
     its own full-width row at the end. */
  #app > header {
    flex-wrap: wrap;
    row-gap: var(--space-2);
    padding: calc(10px + var(--safe-top)) calc(var(--pad-panel) + var(--safe-right))
      10px calc(var(--pad-panel) + var(--safe-left));
  }
  /* The title takes its own row, so the buttons flow as one uniform
     wrapping row beneath it instead of splitting raggedly around it. */
  #app > header h1 {
    flex: 1 1 100%;
  }
  #status {
    flex: 1 1 100%;
    order: 10;
    min-width: 0;
  }

  /* List ↔ detail becomes show-one-at-a-time: both panels share the single
     grid cell, and the list keeps its scroll position while hidden behind
     the detail takeover. */
  main:has(#detail-panel:not([hidden])) {
    grid-template-columns: 1fr;
  }
  main > #list-panel,
  main > #detail-panel {
    grid-area: 1 / 1;
  }
  main:has(#detail-panel:not([hidden])) > #list-panel {
    visibility: hidden;
  }
  #list-panel {
    overscroll-behavior: contain;
    padding-bottom: var(--safe-bottom);
  }
  #detail-panel {
    border-left: none;
    padding-bottom: var(--safe-bottom);
  }
  /* Promote the × into a leading back/close control. */
  #detail-panel header {
    padding-left: calc(var(--pad-panel) + var(--safe-left));
  }
  #close-detail {
    order: -1;
    min-width: var(--tap);
    min-height: var(--tap);
    font-size: 18px;
  }
  #detail-panel h2 {
    min-width: 0;
    overflow-wrap: anywhere;
  }
  .binmeta-list ul {
    max-height: 45dvh; /* was a desktop-sized 340px */
  }

  /* The 9-column table becomes a card list. The runtime-version columns are
     sparse, so empty cells collapse and each card shows only the versions the
     app actually carries. Placement keys off :has() and the existing semantic
     classes — no nth-child — so the same markup can later move into a Lit
     template untouched. */
  #apps,
  #apps tbody {
    display: block;
  }
  #apps tbody tr {
    display: grid;
    grid-template-columns: max-content 1fr max-content;
    gap: 2px 10px;
    align-items: center;
    padding: var(--space-3) calc(var(--pad-panel) + var(--safe-right))
      var(--space-3) calc(var(--pad-panel) + var(--safe-left));
    border-bottom: 1px solid var(--bg-2);
  }
  #apps tbody td {
    display: block;
    padding: 0;
    border: 0;
    min-width: 0;
  }
  #apps tbody td:empty {
    display: none;
  }
  #apps tbody td:has(.risk) {
    grid-area: 1 / 1;
  }
  #apps tbody td.name {
    grid-area: 1 / 2;
    font-size: var(--text-lg);
    overflow-wrap: anywhere;
  }
  #apps tbody td:has(.framework-tag) {
    grid-area: 1 / 3;
  }
  #apps tbody td.version {
    grid-column: 1 / -1;
  }
  #apps tbody td.version[data-label]::before {
    content: attr(data-label) ": ";
    color: var(--fg-2);
  }

  /* The header row becomes a sticky bar of filter chips; the per-column
     popovers (position:fixed, viewport-clamped in main.js) work unchanged. */
  #apps thead {
    display: block;
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--bg-2);
    padding: var(--space-2) calc(var(--space-2) + var(--safe-right))
      var(--space-2) calc(var(--space-2) + var(--safe-left));
    border-bottom: 1px solid var(--bg-3);
  }
  #apps thead tr {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
  }
  #apps thead th {
    position: static;
    display: inline-flex;
    padding: 6px 10px;
    border: 1px solid var(--bg-3);
    border-radius: 999px;
  }
  /* Neutralize the desktop scrollbar inset (cards/chips manage their own
     edge padding). */
  #apps thead th:last-child {
    padding-right: 10px;
  }
  #apps tbody td:last-child {
    padding-right: 0;
  }

  /* Settings: labels stack above their inputs — the max-content label column
     otherwise squeezes inputs to nothing. Number rows ("Reassess every [n]
     minutes") keep the input and its trailing unit on one wrappable line. */
  #settings-form .source label.field {
    grid-template-columns: 1fr;
    gap: 4px;
  }
  #settings-form .source label.field:has(input[type="number"]) {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
  }
}

/* Phones: the settings dialog takes the whole screen, header and actions
   pinned while the sources scroll between them. */
@media (width < 420px) {
  dialog {
    width: 100vw;
    max-width: none;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border: 0;
    border-radius: 0;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
  }
  #settings-form {
    height: 100%;
    overflow-y: auto;
  }
  #settings-form > header {
    position: sticky;
    top: 0;
    background: var(--bg-2);
    z-index: 1;
  }
  #settings-form > footer {
    position: sticky;
    bottom: 0;
    background: var(--bg-2);
    border-top: 1px solid var(--bg-3);
  }
}

/* Touch ergonomics — keyed to the pointer, not the width, so touch-primary
   desktops get them too. Mouse-driven desktops are unaffected. */
@media (pointer: coarse) {
  #app > header button,
  #close-detail,
  #export-detail,
  #update-banner button,
  #settings-form > footer button,
  #detail-body .cve-tab {
    min-height: var(--tap);
    padding-top: var(--space-2);
    padding-bottom: var(--space-2);
  }
  #settings-close {
    min-width: var(--tap);
    min-height: var(--tap);
  }
  /* Filter chips stay visually compact; extend their hit area invisibly. */
  .th-filter-btn,
  .th-clear-btn {
    position: relative;
    min-width: 24px;
    min-height: 24px;
  }
  .th-filter-btn::after,
  .th-clear-btn::after {
    content: "";
    position: absolute;
    inset: -10px;
  }
  /* Disclosure rows: keep display:list-item (preserves the marker), grow the
     tappable box instead. */
  .binmeta-list > summary,
  #detail-body details.advisory > summary {
    min-height: var(--tap);
    padding: var(--space-2) 0;
  }
  /* iOS Safari auto-zooms the page when focusing inputs smaller than 16px. */
  .th-popover input,
  .th-popover select,
  #settings-form input,
  #settings-form select {
    font-size: 16px;
  }
}

/* Wide touch screens still show the real table — give its rows a tap-friendly
   height there. (Below 780px the card layout brings its own padding.) */
@media (pointer: coarse) and (width >= 780px) {
  #apps tbody td {
    padding-top: 10px;
    padding-bottom: 10px;
  }
}
