:root {
  color-scheme: light;
  --bg: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --hover: #f1f5f9;
  --tooltip-bg: #0f172a;
  --danger: #ef4444;
  --success: #10b981;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #090d16;
  --card: #131b2e;
  --border: #222f49;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-soft: #1e293b;
  --hover: #1c263d;
  --tooltip-bg: #020617;
  --danger: #f87171;
  --success: #34d399;
}

html {
  scrollbar-gutter: stable;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: system-ui, sans-serif;
}

body {
  background: var(--bg);
  color: var(--text);
  padding: 1rem 0.5rem;
  display: flex;
  justify-content: center;
  font-size: 0.85rem;
}

.container {
  width: 100%;
  max-width: 1050px;
}

/* Base form controls matching cjfh.uk */
input,
select,
button {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 0.5rem;
  height: 28px;
  font-size: 0.85rem;
  line-height: 26px;
  outline: none;
  vertical-align: middle;
  accent-color: var(--accent);
  color-scheme: inherit;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

button {
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  white-space: nowrap;
}

button:hover {
  background: var(--hover);
}

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

.btn-primary:hover {
  filter: brightness(1.1);
  background: var(--accent);
}

.btn-danger {
  background: var(--danger);
  color: #ffffff;
  border-color: var(--danger);
}

.btn-danger:hover {
  filter: brightness(1.1);
}

/* Typography & utility helpers */
.sub {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 0.15rem;
}

.bold {
  font-weight: 600;
}

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

.flex {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.flex-col {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.flex-wrap {
  flex-wrap: wrap;
}

.top-bar {
  margin-bottom: 0.75rem;
  padding: 0 0.25rem;
}

h1,
h2 {
  font-size: 1.5rem;
  font-weight: 700;
}

h3 {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 1.25rem 0 0.65rem;
  padding: 0 0.25rem;
}

/* Master connection card (sticky header matching rhythm-game-speed) */
.master-card {
  position: sticky;
  top: 0.5rem;
  z-index: 500;
  backdrop-filter: blur(10px);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 1rem;
  margin-bottom: 0.85rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  background: var(--muted);
}

.status-dot.connected {
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
}

.status-dot.connecting {
  background: #f59e0b;
  animation: pulse 1s infinite alternate;
}

@keyframes pulse {
  from { opacity: 0.5; }
  to { opacity: 1; }
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.table-scroll-container {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

.table-scroll-container table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
}

/* Section cards & tables matching cjfh.uk */
.table-container {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: visible;
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
}

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

th,
td {
  padding: 0.55rem 0.75rem;
  text-align: left;
}

thead th {
  background: var(--hover);
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

tbody tr {
  border-bottom: 1px solid var(--border);
}

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

tbody tr:hover {
  background: var(--hover);
}

/* Form inputs & grids */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem 1.25rem;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.setting-item:last-child {
  border-bottom: none;
}

.setting-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

/* Mode pills & radio tabs */
.pill-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: var(--card);
}

.pill-btn {
  border: none;
  border-radius: 0;
  border-right: 1px solid var(--border);
  padding: 0 0.75rem;
  height: 28px;
  font-size: 0.85rem;
  color: var(--text);
  background: var(--card);
}

.pill-btn:last-child {
  border-right: none;
}

.pill-btn.active {
  background: var(--accent);
  color: #ffffff;
}

/* Equalizer Canvas & Sliders */
.eq-canvas-container {
  position: relative;
  width: 100%;
  height: 200px;
  background: color-mix(in srgb, var(--card) 95%, var(--border));
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 0.85rem;
  overflow: hidden;
  touch-action: none;
}

#eq-canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

.eq-rack {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 0.4rem;
  align-items: center;
  text-align: center;
  margin-top: 0.5rem;
}

.eq-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
}

.eq-slider {
  -webkit-appearance: slider-vertical;
  writing-mode: bt-lr;
  width: 22px;
  height: 110px;
  padding: 0;
  cursor: pointer;
}

.eq-gain-text {
  font-size: 0.75rem;
  font-weight: 600;
  min-width: 42px;
}

.eq-freq-text {
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
}

/* Touch Control Mapping Cards */
.touch-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
}

.touch-card {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  background: var(--hover);
}

.touch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}

.touch-row:last-child {
  border-bottom: none;
}

/* Log output terminal */
.log-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.75rem;
  line-height: 1.4;
  height: 140px;
  overflow-y: auto;
  padding: 0.5rem 0.75rem;
  white-space: pre-wrap;
  word-break: break-all;
}

/* Footer matching cjfh.uk */
.footer {
  display: block;
  width: 100%;
  text-align: center;
  margin: 1.5rem 0 0.5rem;
  clear: both;
}

.footer p {
  display: inline-block;
  margin: 0 auto;
}

.footer a {
  color: var(--muted);
  text-decoration: none;
}

.footer a:hover {
  color: var(--accent);
  text-decoration: underline;
}

/* Mobile responsive layout (< 680px) */
@media (max-width: 680px) {
  body {
    padding: 0.5rem 0.25rem 2rem;
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .top-bar {
    flex-direction: column;
    align-items: stretch;
    margin-bottom: 0.5rem;
  }

  .master-card {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }

  .master-card .flex:not(.status-indicator) {
    justify-content: space-between;
  }

  .master-card .status-indicator {
    justify-content: flex-start;
    gap: 0.5rem;
  }

  #eq-profile-actions {
    width: 100%;
    justify-content: flex-start !important;
  }

  .eq-rack {
    gap: 0.15rem;
  }

  .eq-slider {
    height: 90px;
  }

  .eq-freq-text {
    font-size: 0.65rem;
  }
}
