:root {
  --bg: #0a0a0a;
  --surface: #111111;
  --surface-2: #161616;
  --border: #262626;
  --border-hover: #333333;
  --text: #ededed;
  --muted: #888888;
  --accent: #0070f3;
  --accent-hover: #0060df;
  --success: #50e3c2;
  --warn: #f5a623;
  --error: #ff5555;
  --radius: 8px;
  --font: "Geist", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --mono: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

body {
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  width: 80%;
  max-width: 80%;
  height: 100vh;
  margin: 0 auto;
  padding: 16px 24px 24px;
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(180px, auto) 1fr minmax(340px, auto);
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  object-fit: contain;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.topbar-meta-slot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  min-width: 340px;
  min-height: 32px;
  justify-self: end;
}

.topbar-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}

.topbar-meta[hidden] {
  display: none !important;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.badge-ok {
  border-color: rgba(80, 227, 194, 0.3);
  color: var(--success);
}

.badge-warn {
  border-color: rgba(245, 166, 35, 0.3);
  color: var(--warn);
}

.badge-muted {
  color: var(--muted);
}

.badge-error {
  border-color: rgba(255, 85, 85, 0.3);
  color: var(--error);
}

.badge-clickable {
  appearance: none;
  font: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.badge-clickable:hover {
  border-color: var(--border-hover);
  background: var(--surface-2);
}

.badge-clickable:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px rgba(0, 112, 243, 0.25);
}

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.modal-panel {
  position: relative;
  z-index: 1;
  width: min(420px, 100%);
  max-height: min(70vh, 560px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.modal-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.modal-close {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.modal-close:hover {
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.55);
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.5);
}

.modal-search {
  display: block;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.modal-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
}

.modal-list li {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  word-break: break-all;
}

.modal-list li:last-child {
  border-bottom: none;
}

.modal-list-empty {
  color: var(--muted);
  text-align: center;
  padding: 28px 12px !important;
}

.modal-panel-wide {
  width: min(720px, 100%);
  max-height: min(85vh, 720px);
}

.modal-subtitle {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
  font-weight: 400;
}

.analytics-body {
  overflow: auto;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.analytics-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.analytics-engine {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 12px;
}

.analytics-engine h3 {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 600;
}

.analytics-engine-status {
  margin: 0 0 10px;
  font-size: 12px;
  color: var(--muted);
}

.analytics-engine-status.is-ok {
  color: var(--ok, #3ecf8e);
}

.analytics-engine-status.is-warn {
  color: var(--warn, #f5a524);
}

.analytics-engine-status.is-error {
  color: var(--error, #f87171);
}

.analytics-table-wrap {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  max-height: 220px;
}

.analytics-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.analytics-table th,
.analytics-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.analytics-table th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--muted);
  font-weight: 500;
  z-index: 1;
}

.analytics-table td.query-cell {
  white-space: normal;
  word-break: break-word;
  max-width: 280px;
}

.analytics-table tr:last-child td {
  border-bottom: none;
}

.analytics-loading,
.analytics-empty {
  padding: 28px 12px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.zone-row-clickable {
  cursor: pointer;
}

.zone-row-clickable:hover {
  background: rgba(255, 255, 255, 0.03);
}

.zone-row-clickable:focus-visible {
  outline: 2px solid var(--accent, #0070f3);
  outline-offset: -2px;
}

.status-pill-active {
  background: rgba(80, 227, 194, 0.1);
  color: var(--success);
  border: 1px solid rgba(80, 227, 194, 0.2);
}

.status-pill-offline {
  background: rgba(255, 85, 85, 0.1);
  color: var(--error);
  border: 1px solid rgba(255, 85, 85, 0.25);
}

.status-pill-unknown {
  background: rgba(136, 136, 136, 0.1);
  color: var(--muted);
  border: 1px solid var(--border);
}

.grid {
  display: grid;
  grid-template-columns: 280px 1fr 300px;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 16px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.card h2 {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-desc {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.card-header h2 {
  margin: 0;
}

.actions-card {
  grid-row: span 2;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.zones-card {
  grid-column: 2;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.zones-card .card-header,
.zones-card .zones-toolbar {
  flex-shrink: 0;
}

.zones-toolbar {
  margin-bottom: 12px;
}

.zone-search {
  display: block;
}

.zone-search-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  line-height: 1.4;
}

.zone-search-input::placeholder {
  color: var(--muted);
}

.zone-search-input:hover {
  border-color: var(--border-hover);
}

.zone-search-input:focus {
  outline: none;
  border-color: rgba(0, 112, 243, 0.5);
  box-shadow: 0 0 0 2px rgba(0, 112, 243, 0.15);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.rules-card {
  grid-column: 3;
  grid-row: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.rules-card h2 {
  flex-shrink: 0;
}

.log-card {
  grid-column: 2 / 4;
  flex-shrink: 0;
}

.log-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
  margin-top: 8px;
}

.log-split-title {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.status-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 200px;
  padding: 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: center;
}

.status-panel[data-state="fetching"],
.status-panel[data-state="applying"] {
  border-color: rgba(0, 112, 243, 0.35);
}

.status-panel[data-state="done"] {
  border-color: rgba(80, 227, 194, 0.35);
}

.status-panel[data-state="failed"] {
  border-color: rgba(255, 85, 85, 0.35);
}

.status-gif {
  max-width: 160px;
  max-height: 120px;
  width: 100%;
  object-fit: contain;
}

.status-gif[hidden] {
  display: none;
}

.status-label {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.status-panel[data-state="done"] .status-label {
  color: var(--success);
}

.status-panel[data-state="failed"] .status-label {
  color: var(--error);
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

.btn-primary:hover:not(:disabled) {
  background: #fff;
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--border-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--border-hover);
}

.job-status {
  margin: 16px 0 0;
  font-size: 12px;
  color: var(--muted);
  font-family: var(--mono);
}

.health-log-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
  flex: 1;
  min-height: 0;
}

.health-log-title {
  margin: 0;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  flex-shrink: 0;
}

.health-log-panel {
  margin: 0;
  flex: 1;
  min-height: 120px;
  max-height: none;
  overflow: auto;
  padding: 10px;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.count-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}

.table-wrap {
  overflow: auto;
  flex: 1;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

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

th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

tr:last-child td {
  border-bottom: none;
}

tr:hover td {
  background: rgba(255, 255, 255, 0.02);
}

.empty-row td {
  color: var(--muted);
  text-align: center;
  padding: 32px;
}

.zone-id {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}

.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
}

.rules-cell {
  min-width: 180px;
}

.rules-cell-inner {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rules-summary {
  display: inline-flex;
  align-self: flex-start;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border: 1px solid var(--border);
}

.rules-summary-applied {
  color: var(--success);
  border-color: rgba(80, 227, 194, 0.25);
  background: rgba(80, 227, 194, 0.08);
}

.rules-summary-partial {
  color: var(--warn);
  border-color: rgba(245, 166, 35, 0.25);
  background: rgba(245, 166, 35, 0.08);
}

.rules-summary-pending {
  color: var(--muted);
}

.zone-rules {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.rule-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 500;
  text-transform: uppercase;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
}

.rule-active {
  color: var(--success);
  border-color: rgba(80, 227, 194, 0.3);
  background: rgba(80, 227, 194, 0.1);
}

.rule-missing {
  color: var(--error);
  border-color: rgba(255, 85, 85, 0.3);
  background: rgba(255, 85, 85, 0.08);
}

.rule-na {
  color: var(--muted);
  opacity: 0.8;
}

.rule-unknown {
  color: var(--muted);
  opacity: 0.5;
}

.rules-empty {
  color: var(--muted);
  font-size: 12px;
}

.rules-list {
  list-style: none;
  margin: 0;
  padding: 8px 4px 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  scrollbar-color: var(--border) transparent;
  scrollbar-width: thin;
}

.rules-list::-webkit-scrollbar {
  width: 8px;
}

.rules-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

.rules-list::-webkit-scrollbar-thumb:hover {
  background: var(--border-hover);
}

.rules-list li {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
}

.rule-phase {
  display: inline-block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 4px;
}

.rule-desc {
  font-size: 13px;
  margin-bottom: 4px;
}

.rule-action {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}

.log-panel {
  margin: 0;
  height: 200px;
  overflow: auto;
  padding: 12px;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.log-split .log-panel {
  height: 168px;
}

.log-line-info {
  color: #aaa;
}

.log-line-plain {
  color: var(--text);
}

.log-line-success {
  color: var(--success);
}

.log-line-warn {
  color: var(--warn);
}

.log-line-error {
  color: var(--error);
}

@media (max-width: 960px) {
  body {
    overflow: auto;
  }

  .app {
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: 100vh;
    overflow: visible;
    padding: 12px 14px 20px;
  }

  .grid {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    overflow: visible;
  }

  .actions-card,
  .zones-card,
  .rules-card,
  .log-card {
    grid-column: 1;
    grid-row: auto;
  }

  .rules-card {
    overflow: visible;
    height: auto;
  }

  .zones-card {
    height: auto;
  }

  .table-wrap,
  .rules-list {
    min-height: 280px;
  }

  .log-layout {
    grid-template-columns: 1fr;
  }
}
