*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #fafafa;
  --surface: #fff;
  --border: #ddd;
  --text: #222;
  --text-muted: #666;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #ca8a04;
  --font-mono: "SF Mono", "Cascadia Code", "Fira Code", "Consolas", monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, sans-serif;
  --radius: 6px;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding: 1.5rem;
  max-width: 720px;
  margin: 0 auto;
}

header {
  margin-bottom: 1.5rem;
}

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

h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

/* ---- Connection controls ---- */

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

.control-row label {
  font-size: 0.875rem;
  font-weight: 500;
}

.control-row select {
  padding: 0.375rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-family: var(--font-sans);
  background: var(--surface);
}

button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-family: var(--font-sans);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#connect-btn {
  background: var(--accent);
  color: white;
}

#connect-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

#disconnect-btn {
  background: var(--danger);
  color: white;
}

#disconnect-btn:hover:not(:disabled) {
  background: #b91c1c;
}

#status-bar {
  margin-top: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

#state-indicator { font-weight: 600; }
.state-closed  { color: var(--text-muted); }
.state-opening { color: var(--warning); }
.state-open    { color: var(--success); }
.state-closing { color: var(--warning); }

#browser-warning {
  margin-top: 0.75rem;
  padding: 0.75rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  color: var(--danger);
  font-size: 0.875rem;
}

/* ---- Color section (wheel + controls) ---- */

#color-layout {
  display: flex;
  gap: 1.25rem;
  align-items: center;
}

/* Hue wheel */
#wheel-container {
  position: relative;
  width: 240px;
  height: 240px;
  flex-shrink: 0;
}

#hue-wheel {
  width: 100%;
  height: 100%;
  cursor: pointer;
  touch-action: none;
}

#wheel-indicator {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 1.5px #555, 0 1px 4px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  transform: translate(-50%, -50%);
  transition: left 0.03s, top 0.03s;
}

/* Controls column (swatch + slider) */
#controls-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

#color-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

#color-swatch {
  width: 100%;
  height: 80px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: hsl(0, 100%, 50%);
  transition: background-color 0.05s;
}

#color-values {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

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

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.slider-label-row label {
  font-size: 0.8125rem;
  font-weight: 500;
}

.slider-label-row output {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--text-muted);
  min-width: 3.5em;
  text-align: right;
}

/* --- Range slider styling --- */

.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 12px;
  border-radius: 6px;
  outline: none;
  cursor: pointer;
}

/* Hue slider: rainbow gradient */
.slider-hue {
  background: linear-gradient(to right,
    hsl(0, 100%, 50%),
    hsl(60, 100%, 50%),
    hsl(120, 100%, 50%),
    hsl(180, 100%, 50%),
    hsl(240, 100%, 50%),
    hsl(300, 100%, 50%),
    hsl(360, 100%, 50%)
  );
}

/* Thumb styling */
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: white;
  border: 2px solid #999;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: white;
  border: 2px solid #999;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

/* Serial format preview */
#serial-format {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  font-size: 0.8125rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.format-label {
  color: var(--text-muted);
}

#serial-format code {
  font-family: var(--font-mono);
  background: #f3f4f6;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8125rem;
}

/* ---- Serial log ---- */

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.section-header h2 {
  margin-bottom: 0;
}

#clear-log-btn {
  background: var(--accent);
  color: white;
  padding: 0.25rem 0.75rem;
  font-size: 0.8125rem;
}

#clear-log-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

#serial-log {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.6;
  background: #111;
  color: #e5e5e5;
  padding: 0.75rem;
  border-radius: var(--radius);
  height: 150px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---- Responsive ---- */

@media (max-width: 500px) {
  #color-layout {
    flex-direction: column;
    align-items: center;
  }

  #wheel-container {
    width: 200px;
    height: 200px;
  }

  #controls-column {
    width: 100%;
  }
}
