/* Layout and app-specific components. All colours come from ca-chrome.css
   tokens or the semantic data colours — no raw hexes for chrome here. */

.app-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 14px;
  background: var(--vm-surface);
  border-bottom: 1px solid var(--vm-border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  font-size: 0.9231rem;
  font-weight: 700;
  letter-spacing: 1.2px;
  color: var(--vm-text-strong);
}
.brand span { color: var(--vm-accent); }

.head-status { display: flex; gap: 12px; flex: 1; flex-wrap: wrap; }
.head-status .tag { font-size: 0.7692rem; text-transform: uppercase; letter-spacing: .5px; }
.head-actions { display: flex; gap: 6px; }
.head-actions button { padding: 5px 9px; }
.head-actions button.off { opacity: .4; }

/* ── tabs ────────────────────────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 14px 0;
  background: var(--vm-surface);
  border-bottom: 1px solid var(--vm-border);
  overflow-x: auto;
}
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--vm-text-muted);
  padding: 7px 12px;
  font-size: 0.9231rem;
  white-space: nowrap;
}
.tab:hover { background: transparent; color: var(--vm-text); }
.tab.active { color: var(--vm-text-strong); border-bottom-color: var(--vm-accent); }

main { padding: 14px; max-width: 1100px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }
.view .card + .card { margin-top: 12px; }

/* ── forms ───────────────────────────────────────────────────────────────── */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.grid > div { min-width: 0; }
.btn-col { display: flex; align-items: flex-end; }
.btn-col button { width: 100%; }

.check {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--vm-text);
  font-size: 0.9231rem;
  margin: 0;
  padding: 6px 0;
}
.check input { width: auto; }

.channels { display: flex; flex-wrap: wrap; gap: 10px; padding: 6px 0; }
.channels label {
  display: flex;
  align-items: center;
  gap: 5px;
  margin: 0;
  color: var(--vm-text);
  font-size: 0.9231rem;
}

.cond-head { margin-top: 4px; }

.cond-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  padding: 10px;
  background: var(--vm-inset);
  border: 1px solid var(--vm-border-soft);
  border-radius: 7px;
  margin-bottom: 12px;
}
:root[data-theme="light"] .cond-row { background: var(--vm-surface-2); }

/* Each operand is one wrapping group. Without a floor on its width, three
   indicator operands in one row squeeze each other until the labels truncate
   ("Indi…", "BBAN…"), so the group wraps to its own line instead of shrinking. */
.operand {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: flex-end;
  flex: 1 1 260px;
  min-width: 260px;
  padding: 6px;
  background: var(--vm-surface);
  border: 1px solid var(--vm-border-soft);
  border-radius: 6px;
}
.operand > select { flex: 1 1 96px; min-width: 96px; }
.operand .ind { flex: 1 1 104px; min-width: 104px; }
.operand .line { flex: 0 1 92px; min-width: 82px; }
.operand .field { flex: 1 1 92px; min-width: 92px; }
.operand .value { flex: 1 1 90px; min-width: 80px; }
.operand .param { flex: 0 0 62px; max-width: 62px; }
.op-select { flex: 0 0 auto; min-width: 165px; }

.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-msg { font-size: 0.9231rem; }
.form-msg.ok { color: var(--up); }
.form-msg.err { color: var(--down); }
.small { font-size: 0.8462rem; margin: 0 0 10px; }
.hidden { display: none !important; }

.preview-out {
  margin-top: 12px;
  padding: 10px;
  background: var(--vm-inset);
  border: 1px solid var(--vm-border-soft);
  border-radius: 7px;
  font-size: 0.9231rem;
}
:root[data-theme="light"] .preview-out { background: var(--vm-surface-2); }
.preview-out .hit { font-family: ui-monospace, SFMono-Regular, monospace; font-size: 0.8462rem; }

/* ── tables ──────────────────────────────────────────────────────────────── */
.num { text-align: right; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 5px; justify-content: flex-end; }
.row-actions button { padding: 4px 8px; font-size: 0.8462rem; }
.cond-cell { color: var(--vm-text); font-size: 0.8462rem; }
.empty { color: var(--vm-text-muted); padding: 18px; text-align: center; font-size: 0.9231rem; }
.table-wrap { overflow-x: auto; }

/* ── firing overlay ──────────────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.overlay-card {
  background: var(--vm-surface);
  border: 1px solid var(--vm-accent);
  border-radius: 10px;
  padding: 22px;
  max-width: 460px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5);
}
.overlay-card.high { border-color: var(--down); }
.overlay-mark { font-size: 2.0rem; margin-bottom: 8px; color: var(--vm-accent); }
.overlay-card.high .overlay-mark { color: var(--down); }
.overlay-title {
  font-size: 1.1538rem;
  font-weight: 700;
  color: var(--vm-text-strong);
  margin-bottom: 8px;
}
.overlay-body {
  font-size: 1.0rem;
  color: var(--vm-text);
  white-space: pre-line;
  margin-bottom: 8px;
}
.overlay-note { font-size: 0.9231rem; margin-bottom: 14px; }
.overlay-actions button { padding: 8px 22px; }

@media (max-width: 640px) {
  main { padding: 10px; }
  .card { padding: 10px; }
  .op-select, .operand { min-width: 100%; }
  .head-status { order: 3; flex-basis: 100%; }
}

/* ── toast ───────────────────────────────────────────────────────────────── */
/* In-page, not native: WKWebView (which the desktop app uses) has no
   alert/confirm dialogs, so browser-provided ones silently no-op there. */

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%) translateY(14px);
  background: var(--vm-surface-2);
  border: 1px solid var(--vm-border);
  border-radius: 7px;
  padding: 9px 16px;
  font-size: 0.9231rem;
  color: var(--vm-text);
  box-shadow: 0 8px 26px rgba(0, 0, 0, .4);
  opacity: 0;
  pointer-events: none;
  transition: opacity .16s ease, transform .16s ease;
  z-index: 1200;
  max-width: 82vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok  { border-color: var(--up); color: var(--up); }
.toast.err { border-color: var(--down); color: var(--down); }


/* ── card header with a primary action ───────────────────────────────────── */
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.card-head .chart-title { margin-bottom: 0; }

/* The add button is the primary action on this screen, so it is sized and
   coloured to be found without hunting through tabs. Filled button text is
   always #fff — DESIGN.md §5. */
.btn-add {
  font-size: 0.9231rem;
  font-weight: 600;
  padding: 8px 16px;
  white-space: nowrap;
  color: #fff;
}
.btn-add-lg { font-size: 1.0rem; padding: 11px 22px; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 34px 18px 30px;
}
.empty-text { color: var(--vm-text-muted); font-size: 1.0rem; }

/* Symbol resolution feedback under the input. */
.resolve-hint {
  font-size: 0.8462rem;
  color: var(--vm-text-muted);
  margin-top: 5px;
  line-height: 1.35;
  word-break: break-word;
}
.resolve-hint.ok  { color: var(--vm-text); }
.resolve-hint.err { color: var(--down); }

/* An alert that cannot run says so in its own row. A red badge alone did not
   tell the user what was wrong. */
.row-error {
  color: var(--down);
  font-size: 0.8462rem;
  margin-top: 3px;
}

/* Fractional cooldowns are allowed (0.5 = 30 s), so the input must not snap
   to whole minutes. */

/* Coin-mode alerts watch several pairs; the row says how many so the token
   name does not hide what is actually being monitored. Hovering lists them. */
.coin-badge {
  display: inline-block;
  font-size: 0.7692rem;
  color: var(--vm-accent);
  background: var(--vm-accent-soft);
  border-radius: 5px;
  padding: 1px 6px;
  margin-top: 3px;
  cursor: help;
}

/* Explains which bar the bar-close checkbox refers to — the timeframe selector
   is several fields away, so "on bar close only" alone was ambiguous. */
.bar-note {
  font-size: 0.8462rem;
  color: var(--vm-text-muted);
  margin-top: 2px;
  line-height: 1.35;
}
.bar-note.live { color: var(--vm-accent); }
.check input:disabled + span,
.check input:disabled { opacity: .45; }
/* A transport failure is a warning, not a verdict about the symbol. */
.resolve-hint.warn { color: var(--stale); }

/* Edit sits first in the row because it is the action most likely wanted on an
   existing alert, and it is styled neutrally: it changes nothing by itself. */
.btn-edit { border-color: var(--vm-accent); color: var(--vm-accent); }

/* Subscription state of this specific device. */
.push-status { font-size: 0.8462rem; margin-top: 8px; line-height: 1.4; }
.push-status.ok { color: var(--up); }
.push-status.warn { color: var(--stale); }

/* Black box from the service worker: what happened while the app was closed. */
.sw-log {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 0.7692rem;
  line-height: 1.5;
  color: var(--vm-text-muted);
  background: var(--vm-inset);
  border: 1px solid var(--vm-border-soft);
  border-radius: 6px;
  padding: 8px;
  margin: 8px 0 6px;
  max-height: 190px;
  overflow: auto;
  white-space: pre-wrap;
}
:root[data-theme="light"] .sw-log { background: var(--vm-surface-2); }
.btn-small { font-size: 0.8462rem; padding: 5px 10px; }

/* ── phone layout for the alerts table ───────────────────────────────────── */
/* A seven-column table does not fit a phone: the action buttons ended up off
   screen and reachable only by horizontal scrolling. Below this width each row
   becomes a card with its actions underneath. */
@media (max-width: 720px) {
  /* History reads better as stacked cards too. */
  #historyTable thead { display: none; }
  #historyTable table, #historyTable tbody, #historyTable tr, #historyTable td { display: block; }
  #historyTable tr {
    border: 1px solid var(--vm-border);
    border-radius: 7px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  #historyTable td { border: none; padding: 2px 0; text-align: left; }
}

/* ── access key gate ─────────────────────────────────────────────────────── */
.key-gate {
  position: fixed;
  inset: 0;
  background: var(--vm-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 20px;
}
.key-card {
  background: var(--vm-surface);
  border: 1px solid var(--vm-border);
  border-radius: 10px;
  padding: 22px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--vm-shadow);
}
.key-card input { width: 100%; margin-bottom: 12px; }
.key-actions { display: flex; justify-content: flex-end; }


@media (max-width: 720px) {
  /* Rounded, clearly bounded cards — one alert reads as one object.
     Padding is set once, in the layout block above: repeating the shorthand
     here silently reset the left inset that makes room for the grip, and the
     card text ended up underneath it. */
  #alertsTable tr,
  #historyTable tr {
    border: 1px solid var(--vm-border);
    border-radius: 12px;
    background: var(--vm-surface-2);
    box-shadow: var(--vm-shadow);
  }
  #historyTable tr { padding: 12px 14px; }
  :root[data-theme="light"] #alertsTable tr,
  :root[data-theme="light"] #historyTable tr { background: var(--vm-surface); }
}

/* ── drag to reorder ─────────────────────────────────────────────────────── */
/* Classes come from SortableJS. The previous version drew a dashed outline that
   jumped between slots; here the gap is a quiet tinted card and the library
   animates the rest, so movement reads as sliding rather than snapping. */

.drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  color: var(--vm-text-muted);
  font-size: 1.15rem;
  line-height: 1;
  min-width: 32px;
  min-height: 32px;
  border-radius: 6px;
  margin-left: 6px;
}
.drag-handle:active { cursor: grabbing; }

/* The gap the card leaves behind. */
#alertsTable tr.card-ghost {
  opacity: .35;
  background: var(--vm-accent-soft);
}
#alertsTable tr.card-ghost > td { visibility: hidden; }

/* The card while it is held. */
#alertsTable tr.card-chosen { cursor: grabbing; }
#alertsTable tr.card-dragging {
  opacity: .96;
  box-shadow: 0 14px 40px rgba(0, 0, 0, .5);
  border-color: var(--vm-accent) !important;
}

body.reordering { cursor: grabbing; user-select: none; }

@media (max-width: 720px) {
  /* The grip spans the full height of the card at its left edge: a large,
     unmissable target that never overlaps the scrollbar. */
}


/* ── one card per symbol, triggers inside ────────────────────────────────── */
.groups { display: flex; flex-direction: column; gap: 10px; }

.alert-group {
  border: 1px solid var(--vm-border);
  border-radius: 12px;
  background: var(--vm-surface);
  overflow: hidden;
}

.group-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--vm-surface-2);
  border-bottom: 1px solid var(--vm-border-soft);
}
.group-title { display: flex; align-items: center; gap: 8px; flex: 1; flex-wrap: wrap; min-width: 0; }
.group-title b { font-size: 1.08rem; color: var(--vm-text-strong); }
.trigger-count { font-size: 0.8462rem; white-space: nowrap; }
.group-actions { display: flex; gap: 6px; flex-shrink: 0; }

.btn-small { font-size: 0.8462rem; padding: 6px 12px; }
.btn-primary.btn-small { color: #fff; }

/* Triggers read as a stack of conditions, one per line. */
.triggers { display: flex; flex-direction: column; }
.trigger {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-top: 1px solid var(--vm-border-soft);
}
.trigger:first-child { border-top: none; }
.trigger-main { flex: 1; min-width: 0; }
.trigger-cond { color: var(--vm-text); font-size: 0.9231rem; word-break: break-word; }
.trigger-meta { font-size: 0.8462rem; margin-top: 3px; line-height: 1.5; }
.trigger-meta .badge { vertical-align: middle; }

/* The grip is a bar down the left of the card header, well away from the
   scrollbar on the right — reaching for it there caught the scrollbar instead
   and the card could never be picked up. */
.alert-group .drag-handle {
  flex-shrink: 0;
  width: 32px;
  min-height: 32px;
  margin: 0;
}

@media (max-width: 720px) {
  /* The grip and the symbol share the first line; the buttons take the second.
     Letting everything wrap freely left the grip alone and centred above the
     symbol, which read as a decoration rather than a handle. */
  .group-head { flex-wrap: wrap; row-gap: 8px; }
  .group-title { flex: 1 1 auto; }
  .group-actions { width: 100%; justify-content: flex-start; }
  .alert-group .drag-handle {
    flex: 0 0 40px;
    width: 40px;
    min-height: 40px;
    font-size: 1.4rem;
    background: var(--vm-surface);
    border: 1px solid var(--vm-border-soft);
    border-radius: 8px;
  }

  /* Actions below the condition rather than beside it: side by side they
     squeezed the condition into an unreadable column. */
  .trigger { flex-direction: column; gap: 8px; }
  .trigger .row-actions { justify-content: flex-start; flex-wrap: wrap; gap: 6px; width: 100%; }
  .trigger .row-actions button { flex: 1 1 auto; min-width: 62px; padding: 7px 8px; }
}

/* Market label on a card. Spot carries none — it is the default, and labelling
   it would put a badge on nearly every card for no information. */
.market-badge {
  font-size: 0.7692rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .4px;
  color: var(--vm-orange);
  background: var(--vm-orange-soft);
  border-radius: 5px;
  padding: 2px 7px;
  white-space: nowrap;
}

/* Service version in the header — a support question is unanswerable without
   knowing what the customer is actually running. */
.version {
  font-size: 0.7692rem;
  color: var(--vm-text-muted);
  font-variant-numeric: tabular-nums;
  align-self: center;
  padding-right: 4px;
}
.btn-link {
  color: var(--vm-accent);
  text-decoration: none;
  font-size: 0.9231rem;
  align-self: center;
  margin-right: auto;
}
