/* WSJT-X Relay Monitor Styles */
/* Sections: theme, base, header, controls, layout, tables, tags, responsive */

/* Theme */
:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #11151e;
  --border: #2a2f3a;
  --row-border: #202633;
  --text: #e8edf5;
  --muted: #aeb8c8;
  --accent: #4ea1ff;
  --ok: #16c784;
  --danger-bg: #6b1b24;
  --stripe-even: #1b2230;
  --input-bg: #0b0e13;
  --dot-off: #667085;
}

:root[data-theme='light'] {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --panel-2: #eef2f7;
  --border: #d7dee8;
  --row-border: #e5ebf3;
  --text: #111827;
  --muted: #5f6b7a;
  --accent: #2563eb;
  --ok: #16a34a;
  --danger-bg: #f8b4bd;
  --stripe-even: #f2f6fb;
  --input-bg: #ffffff;
  --dot-off: #9aa5b5;
}

/* Base */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

/* Top header */
.header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #000;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

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

.brand img {
  display: block;
  height: 48px;
  width: auto;
}

.brand-title {
  color: #fff;
  font-size: clamp(1.25rem, 2.4vw, 1.85rem);
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
}

.header-right {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.theme-toggle {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.theme-toggle:hover {
  border-color: var(--accent);
}

.status {
  color: var(--muted);
  font-size: 0.92rem;
}

.status .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--dot-off);
  margin-right: 6px;
  vertical-align: middle;
}

.status.connected .dot {
  background: var(--ok);
}

/* Shared controls */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.filters label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

input[type='text'],
select {
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
}

/* Panel layout */
.content {
  display: grid;
  grid-template-columns: minmax(0, 0.75fr) minmax(0, 1.45fr) minmax(0, 1.2fr);
  grid-template-areas: 'activity qso active';
  gap: 12px;
  padding: 12px;
  min-height: 0;
  align-items: stretch;
  overflow: auto;
  position: relative;
}

.layout-splitter {
  display: none;
}

.panel-activity {
  grid-area: activity;
}

.panel-qso {
  grid-area: qso;
}

.panel-active-users {
  grid-area: active;
}

.panel {
  min-width: 0;
  min-height: 0;
  max-height: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  display: grid;
  grid-template-rows: auto 1fr;
}

.panel h2 {
  margin: 0;
  font-size: 1rem;
}

.panel-header {
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  align-content: flex-start;
  min-height: 74px;
  gap: 6px 10px;
}

.panel-header > * {
  min-width: 0;
}

.panel-header .filters {
  font-size: 0.84rem;
}

/* QSO/Active header summaries */
.qso-summary {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

.qso-summary .val,
.active-summary .val {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 800;
}

.active-summary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.qso-actions {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
  max-width: 100%;
}

.qso-actions .filters {
  min-width: 0;
}

.qso-actions .filters input {
  width: 140px;
  max-width: 100%;
}

.export-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.export-btn:hover {
  border-color: var(--accent);
}

.btn-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  filter: brightness(0.95);
}

.qso-sound-toggle {
  position: relative;
  width: 34px;
  min-width: 34px;
  height: 34px;
  padding: 0;
}

.qso-sound-icon {
  width: 21px;
  height: 21px;
  display: block;
  margin: auto;
  color: currentColor;
}

.qso-sound-fill {
  fill: currentColor;
}

.qso-sound-wave {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.qso-sound-slash {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.25;
  stroke-linecap: round;
  opacity: 0;
}

.qso-sound-toggle.is-muted {
  border-color: var(--border);
  background: var(--panel-2);
  color: var(--muted);
}

.qso-sound-toggle.is-muted .qso-sound-slash {
  opacity: 1;
}

/* Tables: Shared Foundation */
.table-wrap {
  overflow-y: auto;
  overflow-x: auto;
  min-height: 0;
}

.table-wrap.hide-x-scroll {
  overflow-x: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-2);
  color: var(--muted);
}

#activeUsersHead th {
  cursor: pointer;
  user-select: none;
}

th,
td {
  border-bottom: 1px solid var(--row-border);
  text-align: left;
  vertical-align: top;
  padding: 6px 8px;
  white-space: nowrap;
}

td.summary {
  white-space: normal;
  min-width: 200px;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* Panel: Activity Log */
.activity-table {
  font-size: 0.8rem;
  min-width: 0;
}

.activity-table th,
.activity-table td {
  padding: 5px 4px;
}

.activity-table td.summary {
  min-width: 120px;
}

/* Panel: QSO Log */
.qso-log-table {
  min-width: 1020px;
}

#qsoBody tr:nth-child(even) td {
  background: var(--stripe-even);
}

.qso-log-table th:last-child,
.qso-log-table td:last-child {
  width: 62px;
  padding-left: 4px;
  padding-right: 4px;
}

/* QSO tags and tooltips */
.qso-tags {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.qso-tag-tooltip {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: help;
}

.qso-tag-tooltip::after {
  content: attr(data-popup);
  position: absolute;
  right: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  z-index: 10;
}

.qso-tag-tooltip:hover::after,
.qso-tag-tooltip:focus-within::after {
  opacity: 1;
  visibility: visible;
}

.qso-tag-pota {
  font-size: 1rem;
  line-height: 1;
}

.qso-tag-dupe {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 999px;
  background: #f59e0b;
  color: #111827;
  font-size: 0.74rem;
  font-weight: 700;
  line-height: 1.2;
}

/* Band badges */
.band-badge {
  display: inline-block;
  min-width: 3.2em;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
}

.band-badge[data-band='160m'] { background: #7f1d1d; color: #ffffff; }
.band-badge[data-band='80m'] { background: #dc2626; color: #ffffff; }
.band-badge[data-band='40m'] { background: #ea580c; color: #ffffff; }
.band-badge[data-band='30m'] { background: #facc15; color: #111827; }
.band-badge[data-band='20m'] { background: #16a34a; color: #ffffff; }
.band-badge[data-band='17m'] { background: #86efac; color: #111827; }
.band-badge[data-band='15m'] { background: #2563eb; color: #ffffff; }
.band-badge[data-band='12m'] { background: #7dd3fc; color: #111827; }
.band-badge[data-band='10m'] { background: #9333ea; color: #ffffff; }
.band-badge[data-band='6m'] { background: #7dd3fc; color: #111827; }
.band-badge[data-band='2m'] { background: #581c87; color: #ffffff; }
.band-badge[data-band='70cm'] { background: #dc2626; color: #ffffff; }

.pwr-badge {
  display: inline-block;
  min-width: 2.8em;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 400;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  filter: brightness(1.25);
  border-style: solid;
  border-width: 1px;
  border-color: var(--text);
}

.band-badge[data-band='30m'],
.band-badge[data-band='17m'],
.band-badge[data-band='12m'],
.band-badge[data-band='6m'] {
  border: 1px solid rgba(17, 24, 39, 0.28);
}

/* Panel: Active Users */
.active-users-table {
  font-size: 0.8rem;
  min-width: 860px;
}

.active-users-table th,
.active-users-table td {
  padding: 5px 6px;
}

.active-users-table th:nth-child(1) { width: 28px; }
.active-users-table th:nth-child(2) { width: 96px; }
.active-users-table th:nth-child(3) { width: 72px; }
.active-users-table th:nth-child(4) { width: 64px; }
.active-users-table th:nth-child(5) { width: 70px; }
.active-users-table th:nth-child(6) { width: 58px; }
.active-users-table th:nth-child(7) { width: 86px; }
.active-users-table th:nth-child(8),
.active-users-table th:nth-child(9) { width: 102px; }
.active-users-table th:nth-child(10),
.active-users-table th:nth-child(11) { width: 72px; }

#activeUsersBody tr.is-transmitting td:not(.status-cell) {
  background: var(--danger-bg);
}

#activeUsersBody tr:nth-child(even):not(.is-transmitting) td {
  background: var(--stripe-even);
}

/* Active Users status indicator */
.status-cell {
  text-align: center;
  padding: 6px 4px !important;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 20px;
  width: 20px;
  max-width: 20px;
}

.status-ball {
  flex-shrink: 0;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: block;
}

.status-ball.status-green {
  background-color: #22c55e;
  box-shadow: 0 0 4px rgba(34, 197, 94, 0.6);
}

.status-ball.status-yellow {
  background-color: #eab308;
  box-shadow: 0 0 4px rgba(234, 179, 8, 0.6);
}

.status-ball.status-red {
  background-color: #ef4444;
  box-shadow: 0 0 4px rgba(239, 68, 68, 0.6);
}

.status-ball.status-unknown {
  background-color: var(--dot-off);
}

/* Responsive */
@media (max-width: 1366px) {
  .content {
    --two-col-left: 52%;
    --two-col-right: 48%;
    --two-row-top: 50%;
    --two-row-bottom: 50%;
    grid-template-columns: minmax(280px, var(--two-col-left)) minmax(280px, var(--two-col-right));
    grid-template-rows: minmax(220px, var(--two-row-top)) minmax(220px, var(--two-row-bottom));
    grid-template-areas:
      'activity active'
      'activity qso';
  }

  /* Explicit placement keeps panel order stable in iframe/browser edge cases. */
  .panel-activity {
    grid-column: 1;
    grid-row: 1 / span 2;
  }

  .panel-active-users {
    grid-column: 2;
    grid-row: 1;
  }

  .panel-qso {
    grid-column: 2;
    grid-row: 2;
  }

  .layout-splitter {
    display: block;
    position: absolute;
    z-index: 10;
    touch-action: none;
    border-radius: 8px;
  }

  .layout-splitter::before {
    content: '';
    position: absolute;
    inset: 0;
    border: 1px solid var(--border);
    border-radius: inherit;
    background: var(--accent);
    opacity: 0.35;
  }

  .layout-splitter::after {
    content: '';
    position: absolute;
    inset: 2px;
    border-radius: inherit;
    background-image: radial-gradient(circle, rgba(255, 255, 255, 0.9) 1.2px, transparent 1.3px);
    background-size: 8px 8px;
    opacity: 0.95;
  }

  .layout-splitter:hover::before,
  .layout-splitter:active::before {
    border-color: var(--accent);
    opacity: 0.7;
  }

  .layout-splitter-col {
    top: 12px;
    bottom: 12px;
    left: var(--two-col-left);
    width: 16px;
    transform: translateX(-50%);
    cursor: col-resize;
  }

  .layout-splitter-row {
    left: calc(var(--two-col-left) + 8px);
    right: 12px;
    top: var(--two-row-top);
    height: 16px;
    transform: translateY(-50%);
    cursor: row-resize;
  }
}

@media (max-width: 980px) {
  .brand img {
    height: 38px;
  }

  .brand-title {
    font-size: 1.05rem;
  }

  .content {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    grid-template-areas:
      'active'
      'activity'
      'qso';
  }

  .panel-active-users,
  .panel-activity,
  .panel-qso {
    grid-column: 1;
  }

  .panel-active-users {
    grid-row: 1;
  }

  .panel-activity {
    grid-row: 2;
  }

  .panel-qso {
    grid-row: 3;
  }

  .panel {
    max-height: calc(100vh - 120px);
  }

  .layout-splitter {
    display: none;
  }

  .panel-header {
    min-height: 0;
  }
}
