:root {
  --bg: #0a0e14; --bg2: #11161f; --bg3: #1a2030;
  --fg: #e6e6e6; --fg-dim: #8b96a8; --muted: #5a6478;
  --accent: #6cf; --green: #4dd87a; --orange: #f5a623; --red: #e25c5c;
  --border: #232b3a;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; background: var(--bg); color: var(--fg); -webkit-text-size-adjust: 100%; }
header { display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: var(--bg2); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
header h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: 0.5px; }
header .status { display: flex; gap: 14px; font-size: 12px; color: var(--fg-dim); flex-wrap: wrap; }
header .status .pill { padding: 2px 8px; border-radius: 10px; background: var(--bg3); }
header .status .pill.ok { color: var(--green); }
header .status .pill.bad { color: var(--red); }
header .filter { margin-left: auto; }
header .filter input { background: var(--bg3); color: var(--fg); border: 1px solid var(--border); padding: 6px 10px; border-radius: 6px; min-width: 180px; font-size: 13px; }
main { padding: 8px; padding-bottom: 24px; }
body.flash-open main { padding-bottom: 340px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 13px; }
thead { position: sticky; top: 51px; background: var(--bg2); z-index: 5; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-weight: 600; color: var(--fg-dim); cursor: pointer; user-select: none; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; }
th.sort-asc::after { content: " ▲"; color: var(--accent); }
th.sort-desc::after { content: " ▼"; color: var(--accent); }
tbody tr:hover { background: var(--bg2); }
tbody tr.offline td { opacity: 0.55; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.dot.online { background: var(--green); box-shadow: 0 0 4px var(--green); }
.dot.stale { background: var(--orange); }
.dot.offline { background: var(--red); }
.mdns-badge { display: inline-block; padding: 1px 5px; border-radius: 8px; background: var(--bg3); color: var(--accent); font-size: 10px; margin-left: 6px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.rssi-good { color: var(--green); }
.rssi-bad { color: var(--orange); }
.ota-latest { color: var(--green); font-size: 12px; }
.flash-btn { display: inline-block; background: var(--orange); color: #1a1100; padding: 2px 8px; border-radius: 4px; font-weight: 600; font-size: 12px; text-decoration: none; cursor: pointer; border: none; }
.flash-btn:hover { background: #ffbe40; text-decoration: none; }
.flash-btn:disabled { background: var(--muted); color: #222; cursor: wait; }
.ago { font-variant-numeric: tabular-nums; }

/* === Toasts === */
#toasts { position: fixed; top: 60px; right: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { pointer-events: auto; padding: 10px 16px; background: var(--bg3); color: var(--fg); border-radius: 6px; border-left: 4px solid var(--accent); font-size: 13px; opacity: 0; transform: translateX(110%); transition: all 250ms ease; box-shadow: 0 6px 20px rgba(0,0,0,0.55); max-width: 360px; }
.toast.show { opacity: 1; transform: translateX(0); }
.toast.ok { border-color: var(--green); }
.toast.bad { border-color: var(--red); }
.toast.warn { border-color: var(--orange); }

/* === Flash streaming console === */
#flash-console { position: fixed; left: 0; right: 0; bottom: 0; height: 320px; background: #050810; border-top: 2px solid var(--accent); z-index: 50; display: flex; flex-direction: column; transform: translateY(100%); transition: transform 250ms ease; }
#flash-console.open { transform: translateY(0); }
.flash-header { padding: 8px 14px; background: var(--bg3); display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); }
#flash-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
#flash-dot.running { background: var(--orange); box-shadow: 0 0 8px var(--orange); animation: pulse 1s infinite; }
#flash-dot.ok { background: var(--green); box-shadow: 0 0 6px var(--green); }
#flash-dot.bad { background: var(--red); box-shadow: 0 0 6px var(--red); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
#flash-title { flex: 1; font-weight: 600; font-size: 13px; }
.flash-controls button { background: transparent; border: 1px solid var(--border); color: var(--fg); padding: 3px 10px; border-radius: 4px; cursor: pointer; margin-left: 6px; font-size: 12px; }
.flash-controls button:hover { background: var(--bg2); }
.flash-controls button:disabled { opacity: 0.4; cursor: default; }
#flash-body { flex: 1; margin: 0; padding: 10px 14px; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; line-height: 1.45; color: #b0c4de; background: #050810; overflow-y: auto; white-space: pre-wrap; word-break: break-word; }

@media (max-width: 720px) {
  th.hide-mobile, td.hide-mobile { display: none; }
  header { flex-wrap: wrap; }
  header .filter { width: 100%; margin-left: 0; }
  header .filter input { width: 100%; }
  #flash-console { height: 50vh; }
  body.flash-open main { padding-bottom: calc(50vh + 20px); }
}

.last-log-btn { cursor: pointer; border: 1px solid var(--border); background: var(--bg3); color: var(--accent); font-family: inherit; font-size: 12px; }
.last-log-btn:hover { background: var(--bg2); border-color: var(--accent); }
.flash-when { color: var(--fg-dim); font-size: 11px; font-variant-numeric: tabular-nums; }

/* === History dropdown menu === */
.history-wrap { position: relative; display: inline-block; }
.history-menu {
  position: absolute; top: calc(100% + 6px); right: 0;
  min-width: 320px; max-width: 480px; max-height: 60vh;
  overflow-y: auto;
  background: var(--bg2); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.7);
  z-index: 50; padding: 4px 0; font-size: 13px;
}
.hist-empty { padding: 16px; color: var(--fg-dim); text-align: center; }
.hist-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.hist-item:last-child { border-bottom: none; }
.hist-item:hover { background: var(--bg3); }
.hist-icon { width: 18px; text-align: center; font-size: 14px; flex-shrink: 0; }
.hist-icon.ok { color: var(--green); }
.hist-icon.bad { color: var(--red); }
.hist-icon.warn { color: var(--orange); }
.hist-icon.running { color: var(--orange); animation: pulse 1.5s infinite; }
.hist-meta { flex: 1; min-width: 0; }
.hist-name { color: var(--fg); font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-sub { color: var(--fg-dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-when { color: var(--fg-dim); font-size: 11px; font-variant-numeric: tabular-nums; flex-shrink: 0; min-width: 32px; text-align: right; }
.hist-del {
  background: transparent; border: 1px solid var(--border); color: var(--fg-dim);
  width: 22px; height: 22px; border-radius: 4px; cursor: pointer;
  font-size: 14px; padding: 0; line-height: 1; flex-shrink: 0;
}
.hist-del:hover { background: var(--red); color: #fff; border-color: var(--red); }

/* === Phase 2: grouping, actions, notes, mDNS-only === */
.chk-pill { cursor: pointer; user-select: none; display: inline-flex; align-items: center; gap: 4px; }
.chk-pill input { margin: 0; cursor: pointer; }
.group-hdr-row td { padding: 0; border-bottom: none; background: var(--bg2); }
.group-hdr {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; cursor: pointer;
  background: linear-gradient(to right, var(--bg3) 0%, var(--bg2) 100%);
  border-bottom: 1px solid var(--border); border-top: 1px solid var(--border);
  font-size: 13px; font-weight: 600; letter-spacing: 0.5px;
}
.group-hdr:hover { background: var(--bg3); }
.group-arrow { color: var(--accent); width: 12px; font-size: 10px; }
.group-name { color: var(--fg); text-transform: uppercase; flex-shrink: 0; }
.group-count { color: var(--fg-dim); font-weight: 400; font-size: 11px; }
.group-stat { font-size: 11px; font-weight: 400; }
.group-online { color: var(--green); }
.group-stale { color: var(--orange); }
.row-btn {
  background: transparent; border: 1px solid var(--border); color: var(--fg-dim);
  width: 24px; height: 22px; padding: 0; margin-right: 4px;
  border-radius: 4px; cursor: pointer; font-size: 12px; line-height: 1;
}
.row-btn:hover { background: var(--bg3); color: var(--fg); border-color: var(--accent); }
.reboot-btn:hover { background: var(--orange); color: #1a1100; border-color: var(--orange); }
.note-btn:hover { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.actions-cell { white-space: nowrap; }
.note-badge { font-size: 11px; margin-left: 4px; cursor: help; }
.mdns-only-tag {
  display: inline-block; padding: 1px 5px; border-radius: 8px;
  background: var(--orange); color: #1a1100; font-size: 10px; font-weight: 600;
  margin-left: 6px;
}

/* === Phase 3: bulk flash button === */
.bulk-flash-btn { background: var(--orange); color: #1a1100; font-weight: 600; cursor: pointer; border: none; font-family: inherit; font-size: 12px; }
.bulk-flash-btn:hover { background: #ffbe40; }

/* === Phase 4: scheduled btn, serial drawer, sparkline === */
.sched-btn { background: var(--accent); color: var(--bg); font-weight: 600; cursor: pointer; border: none; font-family: inherit; font-size: 12px; }
.sched-btn:hover { background: #88e0ff; }
.spark-cell { padding: 2px 4px; width: 56px; }
canvas.spark { display: block; image-rendering: crisp-edges; }

#serial-drawer {
  position: fixed; top: 51px; right: 0; bottom: 0;
  width: 480px; max-width: 80vw;
  background: #050810; border-left: 2px solid var(--accent);
  z-index: 40;
  display: flex; flex-direction: column;
  transform: translateX(110%); transition: transform 250ms ease;
}
#serial-drawer.open { transform: translateX(0); }
body.serial-open main { padding-right: 480px; }
@media (max-width: 720px) { body.serial-open main { padding-right: 0; } #serial-drawer { width: 100vw; max-width: 100vw; } }
.serial-header { padding: 8px 14px; background: var(--bg3); display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); }
.serial-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--green); box-shadow: 0 0 4px var(--green); animation: pulse 2s infinite; flex-shrink: 0; }
#serial-title { flex: 1; font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#serial-close { background: transparent; border: 1px solid var(--border); color: var(--fg); padding: 3px 10px; border-radius: 4px; cursor: pointer; font-size: 12px; }
#serial-close:hover { background: var(--bg2); }
#serial-body { flex: 1; overflow-y: auto; padding: 8px 12px; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11px; line-height: 1.5; color: #b0c4de; }
.serial-line { white-space: pre-wrap; word-break: break-word; padding: 1px 0; border-bottom: 1px dotted #1a2030; }
.serial-ts { color: var(--accent); font-weight: 600; user-select: none; }
.build-btn:hover { background: var(--green); color: #002211; border-color: var(--green); }
.serial-btn:hover { background: var(--bg3); color: var(--accent); border-color: var(--accent); }
.sched-flash-btn:hover { background: var(--accent); color: var(--bg); border-color: var(--accent); }

/* === Mobile polish (Phase 5) === */
@media (max-width: 720px) {
  /* Fix #1: touch targets — row buttons 36×36 (Apple HIG min 44 but 36 acceptable at this density) */
  .row-btn {
    min-width: 36px;
    min-height: 36px;
    width: 36px;
    height: 36px;
    font-size: 16px;
    padding: 0;
    margin-right: 4px;
  }
  /* Fix #4: flash button bigger */
  .flash-btn {
    padding: 8px 12px;
    min-height: 36px;
    font-size: 13px;
    line-height: 1.2;
    display: inline-block;
  }
  /* Fix #2: history dropdown — centered fixed overlay on mobile (not right-anchored) */
  .history-menu {
    position: fixed;
    top: 60px;
    left: 8px;
    right: 8px;
    width: auto;
    min-width: 0;
    max-width: none;
    max-height: calc(100vh - 80px);
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.85);
  }
  .hist-item { padding: 10px 12px; min-height: 44px; }
  .hist-del { width: 32px; height: 32px; font-size: 18px; }
  /* Fix #3: compact header — smaller pills + tighter gaps */
  header { padding: 6px 10px; gap: 6px; }
  header h1 { font-size: 14px; }
  header .status { gap: 5px; }
  header .pill { padding: 4px 8px; font-size: 11px; }
  /* Hide less-essential status pills on phone (status visible in toast on events) */
  #st-ws, #st-mdns, #st-ota { display: none; }
  /* Big action buttons (bulk flash, sched) — icon + count only, narrower */
  .bulk-flash-btn, .sched-btn, .last-log-btn { font-size: 11px; padding: 6px 10px; min-height: 36px; line-height: 1.2; }
  /* Filter input — full-width input is already correct from base css */
  header .filter input { padding: 8px 10px; font-size: 14px; min-height: 40px; }
  /* Fix #5: simpler group header — hide stats, smaller padding */
  .group-hdr { gap: 8px; padding: 10px 10px; }
  .group-name { font-size: 13px; }
  .group-stat { display: none; }
  .group-count { font-size: 11px; margin-left: auto; }
  /* Row height bigger for tap targets */
  tbody td { padding: 10px 6px; font-size: 12px; line-height: 1.3; }
  tbody tr { min-height: 44px; }
  /* Actions cell — flex-wrap for very small screens */
  .actions-cell { white-space: normal; }
  /* Table column tightening on mobile */
  thead th { padding: 8px 4px; font-size: 10px; }
  /* mDNS-only checkbox label — more touchable */
  .chk-pill { min-height: 36px; padding: 4px 10px; }
  .chk-pill input { width: 18px; height: 18px; }
  /* Flash console — bigger button targets */
  .flash-controls button { padding: 8px 12px; min-height: 36px; font-size: 13px; }
  /* Serial drawer — full width already correct from base */
  .serial-header { padding: 10px 14px; }
  #serial-close { padding: 8px 12px; min-height: 36px; font-size: 13px; }
}

/* Very small phone (< 380px) — collapse pill text further */
@media (max-width: 380px) {
  header h1 { font-size: 13px; }
  header .pill { padding: 4px 6px; font-size: 10px; }
  .bulk-flash-btn, .sched-btn, .last-log-btn { font-size: 10px; padding: 4px 8px; }
}

/* === Action sheet (Phase 6 — replaces per-row 5 buttons with single ⋯) === */
.actions-menu-btn {
  font-size: 18px !important;
  letter-spacing: 1px;
  font-weight: 700;
}
.actions-menu-btn:hover { background: var(--accent); color: var(--bg); border-color: var(--accent); }

#action-sheet {
  position: fixed; inset: 0; z-index: 100;
  display: none;
  background: rgba(0, 0, 0, 0.6);
  align-items: flex-end; justify-content: center;
}
#action-sheet.open { display: flex; }
.action-sheet-inner {
  background: var(--bg2);
  border-radius: 16px 16px 0 0;
  border-top: 2px solid var(--accent);
  width: 100%; max-width: 560px;
  max-height: 80vh;
  display: flex; flex-direction: column;
  box-shadow: 0 -10px 40px rgba(0,0,0,0.7);
  animation: sheetSlideUp 200ms ease-out;
}
@keyframes sheetSlideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.action-sheet-header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg3);
  border-radius: 16px 16px 0 0;
}
.action-sheet-meta { flex: 1; min-width: 0; }
.action-sheet-title {
  font-size: 16px; font-weight: 600; color: var(--fg);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.action-sheet-sub {
  font-size: 12px; color: var(--fg-dim); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.action-sheet-close {
  background: transparent; border: 1px solid var(--border); color: var(--fg);
  width: 36px; height: 36px; border-radius: 8px;
  font-size: 20px; cursor: pointer; padding: 0; line-height: 1;
}
.action-sheet-close:hover { background: var(--bg); }
.action-sheet-items {
  flex: 1; overflow-y: auto; padding: 8px 0;
}
.action-item {
  display: flex; align-items: center; gap: 14px;
  width: 100%; padding: 12px 18px; min-height: 56px;
  background: transparent; border: none; cursor: pointer;
  color: var(--fg); text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px; font-family: inherit;
}
.action-item:last-child { border-bottom: none; }
.action-item:hover, .action-item:focus { background: var(--bg3); }
.action-item.highlight { background: rgba(245, 166, 35, 0.08); border-left: 3px solid var(--orange); }
.action-item.highlight:hover { background: rgba(245, 166, 35, 0.18); }
.action-item.danger:hover { background: rgba(226, 92, 92, 0.15); color: var(--red); }
.action-icon {
  font-size: 22px; width: 32px; text-align: center; flex-shrink: 0;
}
.action-label {
  flex: 1; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.action-sub {
  color: var(--fg-dim); font-size: 11px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 45%;
}

/* On desktop: centered modal instead of bottom sheet */
@media (min-width: 720px) {
  #action-sheet { align-items: center; }
  .action-sheet-inner {
    border-radius: 16px;
    border: 2px solid var(--accent);
    border-top-width: 2px;
    max-width: 480px; min-width: 360px;
    animation: sheetFadeIn 150ms ease-out;
  }
  @keyframes sheetFadeIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }
  .action-sheet-header { border-radius: 14px 14px 0 0; }
}

/* On phone: row no longer has 5 buttons → revert tall rows */
@media (max-width: 720px) {
  tbody td { padding: 6px 6px; font-size: 12px; }
  tbody tr { min-height: 36px; }
  .actions-cell { white-space: nowrap; }
  .actions-menu-btn { font-size: 20px !important; }
}
