:root {
  --bg: #f5f6f8;
  --card-bg: #ffffff;
  --primary: #1976d2;
  --primary-dark: #115293;
  --danger: #d32f2f;
  --success: #388e3c;
  --warning: #f57c00;
  --text: #1a1a2e;
  --muted: #6b7280;
  --border: #d1d5db;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

h1, h2, h3, h4 { margin: 0 0 0.75rem; }

a { color: var(--primary); text-decoration: none; }

.navbar {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-brand a { font-weight: 700; font-size: 1.2rem; color: var(--text); }

.nav-menu { display: flex; align-items: center; gap: 1rem; }
.nav-menu a { font-size: 0.95rem; padding: 0.25rem 0.5rem; border-radius: var(--radius); }
.nav-menu a.active { background: var(--primary); color: #fff; font-weight: 600; }

.user-pill {
  background: #e3f2fd;
  color: var(--primary-dark);
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
}

.logout-form { display: inline; }
.logout-form button {
  background: transparent;
  border: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
}

.container { padding: 1.5rem; max-width: 1400px; margin: 0 auto; }

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

.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  text-decoration: none;
  min-height: 36px;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: #2e7d32; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #b71c1c; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.8rem; min-height: 30px; }
.btn-lg { padding: 0.75rem 1.5rem; font-size: 1rem; }
.btn-xs { padding: 0.15rem 0.4rem; font-size: 0.7rem; min-height: 22px; }

input, select, textarea {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  min-height: 36px;
}

label { display: block; margin-bottom: 0.5rem; font-weight: 500; }

.form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: end;
}

.form-stack {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  align-items: stretch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.data-table th, .data-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.data-table th { background: #f3f4f6; font-weight: 600; }
.data-table tr:last-child td { border-bottom: none; }

.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}
.alert-info { background: #e3f2fd; color: #0d47a1; }
.alert-error { background: #ffebee; color: #b71c1c; }

.role-badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  text-transform: uppercase;
  font-weight: 600;
}
.role-admin { background: #e3f2fd; color: var(--primary-dark); }
.role-dispatcher { background: #e8f5e9; color: var(--success); }
.role-viewonly { background: #fff3e0; color: var(--warning); }

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  background: var(--card-bg);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 360px;
}
.login-card h1 { margin-bottom: 1.25rem; text-align: center; }
.login-card label { margin-bottom: 0.75rem; }
.login-card input { width: 100%; }
.login-card button { width: 100%; margin-top: 0.5rem; }

.designer-shell {
  overflow: auto;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: #e5e7eb;
  padding: 1rem;
}

#designer-board {
  position: relative;
  background: #fff;
  touch-action: none;
  display: block;
}

.board-bg {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  object-fit: contain;
  pointer-events: none;
  opacity: 0.9;
  z-index: 0;
}

#designer-rooms,
#designer-board .board-text-boxes {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}
#designer-board .board-text-boxes {
  pointer-events: none;
}

.board-text-box {
  position: absolute;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 0.35rem;
  cursor: move;
  user-select: none;
  overflow: hidden;
  display: flex;
  align-items: center;
  z-index: 1;
  pointer-events: auto;
}
.board-text-box.selected { outline: 3px dashed var(--warning); z-index: 10; }
.board-text-box:hover .text-box-actions,
.board-text-box.selected .text-box-actions { opacity: 1; }

.text-box-content {
  flex: 1;
  word-break: break-word;
  pointer-events: none;
}

.text-box-actions {
  position: absolute;
  top: 2px; right: 2px;
  display: flex;
  gap: 0.25rem;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 2;
}
.text-box-actions button {
  pointer-events: auto;
}

.board-room {
  position: absolute;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  background: rgba(227, 242, 253, 0.85);
  min-width: 80px; min-height: 60px;
  display: flex;
  flex-direction: column;
  padding: 0.5rem;
  cursor: move;
  user-select: none;
  overflow: visible;
  z-index: 1;
  pointer-events: auto;
}

.board-room.selected {
  outline: 3px dashed var(--warning);
  z-index: 10;
}

.room-name {
  font-weight: 700;
  font-size: 0.85rem;
  pointer-events: none;
  margin-bottom: auto;
}

.room-actions {
  display: flex;
  gap: 0.25rem;
  align-self: flex-end;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.room-actions button {
  pointer-events: auto;
}
.board-room:hover .room-actions,
.board-room.selected .room-actions { opacity: 1; }

.resize-handle {
  position: absolute;
  right: -4px; bottom: -4px;
  width: 22px; height: 22px;
  cursor: se-resize;
  background: linear-gradient(135deg, transparent 50%, var(--primary) 50%);
  border: 2px solid var(--primary);
  border-radius: 0 0 var(--radius) 0;
  pointer-events: auto;
  z-index: 30;
}

/* Event setup */
.setup-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
}
.setup-grid .full-width { grid-column: 1 / -1; }

.setup-list { list-style: none; padding: 0; margin: 0.75rem 0 0; }
.setup-list li {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.members-list { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }

.chip-list { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chip-list li {
  background: #e3f2fd;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  font-size: 0.9rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.roster-tabs { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1rem; }
.roster-section { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }

.pill {
  background: #e8f5e9;
  color: #1b5e20;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  font-size: 0.8rem;
}

/* Live board */
.board-app { display: flex; flex-direction: column; min-height: 100vh; }

.board-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.board-toolbar h1 { font-size: 1.1rem; margin: 0; }

.toolbar-actions { display: flex; gap: 0.35rem; }

.board-main {
  height: 60vh;
  flex: none;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

.token-tray {
  width: 200px;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  overflow-y: auto;
  transition: width 0.2s ease, padding 0.2s ease;
  flex: none;
}
.token-tray.collapsed {
  width: 48px;
  padding: 0.5rem 0.25rem;
  overflow: hidden;
}
.token-tray.collapsed .tray-content,
.token-tray.collapsed .tray-header h2 { display: none; }
.token-tray.collapsed .tray-header {
  justify-content: center;
}
.token-tray.collapsed #toggle-tray {
  transform: rotate(180deg);
}

.tray-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.tray-header h2 { font-size: 0.95rem; margin: 0; }

.token-tray.drag-over { background: #e3f2fd; }
.token-tray h3 { font-size: 0.8rem; color: var(--muted); margin: 0.5rem 0 0.25rem; }

.token-list { display: flex; flex-direction: column; gap: 0.35rem; }

.token {
  background: #fff;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.5rem;
  cursor: grab;
  box-shadow: var(--shadow);
  position: relative;
  font-size: 0.85rem;
}
.token:active { cursor: grabbing; }
.token-supervisor { background: #fff3e0; border-color: var(--warning); }

.token-name { font-weight: 700; font-size: 0.85rem; }
.token-members { font-size: 0.7rem; color: var(--muted); margin-top: 0.15rem; }
.token-role { font-size: 0.65rem; color: var(--primary-dark); }

.board-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.canvas-zoom {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.35rem;
  padding: 0.25rem 0.5rem;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.canvas-zoom span { font-size: 0.8rem; min-width: 3rem; text-align: center; }

.board-canvas-shell {
  flex: 1;
  position: relative;
  overflow: auto;
  background: #e5e7eb;
  padding: 0.5rem;
  min-height: 0;
}

.board-canvas {
  position: relative;
  background: #fff;
  box-shadow: var(--shadow);
  display: block;
}
.board-canvas .board-bg {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  object-fit: contain;
  pointer-events: none;
  opacity: 0.9;
  z-index: 0;
}
.board-canvas .board-text-boxes {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}
.board-canvas .board-text-box {
  position: absolute;
  border-radius: var(--radius);
  padding: 0.35rem;
  overflow: hidden;
  display: flex;
  align-items: center;
  pointer-events: none;
}
.board-text-box-content {
  flex: 1;
  word-break: break-word;
}

.board-canvas .board-rooms {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.room-zone {
  position: absolute;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  background: rgba(227, 242, 253, 0.65);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
}

.room-zone.drag-over {
  background: rgba(25, 118, 210, 0.25);
  border-style: dashed;
}

.room-zone h4 {
  margin: 0 0 0.25rem;
  font-size: 0.85rem;
  font-weight: 700;
}

.room-tokens {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  min-height: 40px;
  align-content: flex-start;
}

.active-runs-panel {
  width: 240px;
  background: var(--card-bg);
  border-left: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  flex: none;
}
.active-runs-panel h2 { font-size: 0.95rem; margin-bottom: 0.5rem; }

.run-zone {
  width: 100%;
  min-height: 80px;
  border: 3px dashed var(--danger);
  border-radius: var(--radius);
  background: rgba(255, 235, 238, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--danger);
  font-weight: 600;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
}
.run-zone.drag-over { background: rgba(239, 83, 80, 0.15); }

.active-runs-list { flex: 1; overflow-y: auto; min-height: 0; }

.run-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem;
  margin-bottom: 0.35rem;
  font-size: 0.85rem;
}
.run-card.alerting { animation: pulse 1.2s infinite; border-color: var(--danger); }
.run-card.drag-over { outline: 3px dashed var(--warning); }

.run-status { font-weight: 700; text-transform: capitalize; font-size: 0.85rem; }
.run-timers { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-top: 0.2rem; }
.run-timer { font-size: 0.75rem; color: var(--muted); }
.run-timer.total { font-weight: 700; color: var(--text); }
.run-timer.timer-stalled { color: var(--danger); font-weight: 700; }
.run-location { font-size: 0.8rem; margin: 0.15rem 0; }

.run-actions { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.35rem; }
.run-actions .btn { padding: 0.25rem 0.5rem; font-size: 0.75rem; }

.activity-log {
  height: 50vh;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex: none;
}
.activity-log.collapsed { height: 40px; }

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex: none;
}
.log-header h2 { font-size: 0.9rem; margin: 0; }

.log-table-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 0 0.75rem;
}

.log-table { width: 100%; font-size: 0.8rem; border-collapse: collapse; }
.log-table th, .log-table td { padding: 0.35rem 0.4rem; border-bottom: 1px solid var(--border); }
.log-table th { background: #f3f4f6; text-align: left; position: sticky; top: 0; }
.log-action-on_a_run { color: var(--warning); font-weight: 600; }
.log-action-on_scene { color: var(--success); font-weight: 600; }
.log-action-returning { color: var(--primary); font-weight: 600; }
.log-action-back_in_service { color: var(--muted); }
.log-action-cancelled { color: var(--danger); font-weight: 600; }
.log-action-transfer_cfd { color: var(--success); font-weight: 700; }

.status-transfer_cfd { color: var(--success); }

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

dialog {
  border: none;
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
  padding: 1.5rem;
  min-width: 320px;
}
dialog::backdrop { background: rgba(0,0,0,0.35); }

.hint { font-size: 0.85rem; color: var(--muted); margin: 0.5rem 0 0; }

/* Responsive */
@media (max-width: 900px) {
  .board-main { flex-direction: column; overflow: visible; height: auto; }
  .token-tray,
  .active-runs-panel {
    width: 100%;
    border: none;
    border-bottom: 1px solid var(--border);
    max-height: 160px;
    flex: none;
  }
  .token-list { flex-direction: row; flex-wrap: wrap; }
  .token { min-width: 120px; }
  .board-canvas-shell { padding: 0.5rem; height: auto; min-height: 360px; }
  .board-canvas { min-width: unset; min-height: unset; }
  .active-runs-panel { order: -1; border-bottom: none; border-bottom: 1px solid var(--border); }
  .run-zone { width: auto; margin: 0.5rem; min-height: 60px; }
  .activity-log { height: 160px; }
  .token-members { display: none; }
}

@media (max-width: 600px) {
  .navbar { flex-direction: column; align-items: stretch; gap: 0.5rem; }
  .nav-menu { flex-wrap: wrap; justify-content: space-between; }
  .container { padding: 0.75rem; }
  .page-header { flex-direction: column; align-items: flex-start; gap: 0.5rem; }
  .token { padding: 0.35rem; }
}
