:root {
  /* Spacing */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;

  /* Typography */
  --font-mono: 'Roboto Mono', 'Courier New', monospace;
  --font-sans: system-ui, -apple-system, sans-serif;
  --font-display: var(--font-mono);

  /* Radii */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 16px;

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-slow: 600ms ease;

  /* Traffic light colours (consistent across themes) */
  --color-green: #22c55e;
  --color-yellow: #eab308;
  --color-red: #ef4444;
  --color-overtime: #dc2626;

  /* Z-indices */
  --z-overlay: 100;
  --z-modal: 200;
  --z-toast: 300;
}

/* Dark theme (default) */
[data-theme="dark"], :root {
  --bg-primary: #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-tertiary: #2a2a2a;
  --bg-elevated: #333333;

  --text-primary: #f0f0f0;
  --text-secondary: #a0a0a0;
  --text-muted: #606060;

  --border-color: #333333;
  --border-subtle: #222222;

  --accent: #4f8ef7;
  --accent-hover: #6fa3ff;

  --btn-danger: #ef4444;
  --btn-danger-hover: #dc2626;
  --btn-success: #22c55e;
  --btn-success-hover: #16a34a;
  --btn-neutral: #404040;
  --btn-neutral-hover: #505050;
}

/* Light theme */
[data-theme="light"] {
  --bg-primary: #f8f8f8;
  --bg-secondary: #ffffff;
  --bg-tertiary: #e8e8e8;
  --bg-elevated: #d8d8d8;

  --text-primary: #111111;
  --text-secondary: #444444;
  --text-muted: #888888;

  --border-color: #cccccc;
  --border-subtle: #e0e0e0;

  --accent: #2563eb;
  --accent-hover: #1d4ed8;

  --btn-danger: #ef4444;
  --btn-danger-hover: #dc2626;
  --btn-success: #16a34a;
  --btn-success-hover: #15803d;
  --btn-neutral: #d1d5db;
  --btn-neutral-hover: #9ca3af;
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100dvh;
  line-height: 1.5;
  overflow-x: hidden;
}

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

button {
  font-family: inherit;
  font-size: 1rem;
  cursor: pointer;
  border: none;
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  transition: background var(--transition-fast), opacity var(--transition-fast);
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
}
button:disabled { opacity: 0.4; cursor: not-allowed; }

input, select, textarea {
  font-family: inherit;
  font-size: 1rem;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: var(--space-sm);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: var(--space-sm); border-bottom: 1px solid var(--border-subtle); }
th { color: var(--text-secondary); font-size: 0.85rem; font-weight: 600; }

/* Utility classes */
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

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

.btn-danger {
  background: var(--btn-danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) { background: var(--btn-danger-hover); }

.btn-success {
  background: var(--btn-success);
  color: #fff;
}
.btn-success:hover:not(:disabled) { background: var(--btn-success-hover); }

.btn-neutral {
  background: var(--btn-neutral);
  color: var(--text-primary);
}
.btn-neutral:hover:not(:disabled) { background: var(--btn-neutral-hover); }

.btn-icon {
  background: none;
  color: var(--text-secondary);
  padding: var(--space-xs);
  font-size: 1.1rem;
}
.btn-icon:hover:not(:disabled) { color: var(--text-primary); background: var(--bg-tertiary); }

/* Panels */
#display-panel, #controller-panel {
  width: 100%;
}

/* Role visibility */
body[data-role="display"] #controller-panel { display: none; }
body[data-role="controller"] #display-panel { display: none; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: var(--z-modal);
}
.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  max-width: 480px;
  width: 90%;
  max-height: 90dvh;
  overflow-y: auto;
}

.modal h2 { margin-bottom: var(--space-lg); font-size: 1.25rem; }

.modal-actions {
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
  margin-top: var(--space-lg);
}

/* Toast */
#toast-container {
  position: fixed;
  bottom: var(--space-lg);
  right: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  z-index: var(--z-toast);
  pointer-events: none;
}

.toast {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-left: 4px solid var(--accent);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  animation: slideIn 200ms ease, fadeOut 400ms ease 3s forwards;
}
.toast.error { border-left-color: var(--btn-danger); }
.toast.success { border-left-color: var(--btn-success); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}
@keyframes fadeOut {
  from { opacity: 1; }
  to   { opacity: 0; pointer-events: none; }
}
