:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #dde1e6;
  --text: #1f2430;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --today: #fef3c7;
  --weekend: #f1f2f5;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  white-space: nowrap;
}

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }

.tab-btn {
  padding: 8px 14px;
  border: none;
  background: transparent;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
}
.tab-btn:hover { background: var(--bg); }
.tab-btn.active { background: var(--primary); color: white; }

main { padding: 16px 20px 60px; max-width: 1400px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.spacer { flex: 1; }
.range-label { font-weight: 600; min-width: 160px; text-align: center; }
.filter-label { font-size: 14px; color: var(--text-muted); }

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 7px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text);
}
button:hover { background: var(--bg); }
button.primary { background: var(--primary); color: white; border-color: var(--primary); }
button.primary:hover { background: var(--primary-dark); }
button.ghost { background: transparent; border-color: transparent; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: #fee2e2; }

/* ---- Tables ---- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
  margin-bottom: 24px;
}
.data-table th, .data-table td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.data-table th { color: var(--text-muted); font-weight: 600; background: #fafbfc; }
.data-table tbody tr:hover { background: #f9fafb; cursor: pointer; }
.group-title { font-size: 15px; margin: 18px 0 8px; color: var(--text-muted); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.badge.aktiv { background: #dcfce7; color: #166534; }
.badge.avslutad { background: #f1f2f5; color: var(--text-muted); }

/* ---- Legend ---- */
.legend { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; font-size: 12px; }
.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 2px; }

/* ---- Timeline ---- */
.timeline-wrap {
  background: var(--panel);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow-x: auto;
}
.timeline-inner {
  position: relative;
}
.timeline-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.timeline-grid {
  display: grid;
  position: relative;
}
.tl-row-label {
  position: sticky;
  left: 0;
  background: var(--panel);
  padding: 6px 10px;
  font-size: 13px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  white-space: nowrap;
  z-index: 2;
}
.tl-row-label.group-header {
  background: #f2f4f7;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.tl-day-header {
  padding: 4px 0;
  font-size: 11px;
  text-align: center;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid #eceff2;
  color: var(--text-muted);
  background: var(--panel);
}
.tl-day-header.weekend { background: var(--weekend); }
.tl-day-header.today { background: var(--today); }
.tl-day-header .dow { display: block; font-size: 9px; }
.tl-cell {
  border-right: 1px solid #eceff2;
  border-bottom: 1px solid var(--border);
  position: relative;
  min-height: 30px;
}
.tl-cell.weekend { background: var(--weekend); }
.tl-cell.today { background: var(--today); }
.tl-cell:hover { background: #eef2ff; cursor: pointer; }
.tl-bar {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 1px;
  right: 1px;
  border-radius: 4px;
  color: white;
  font-size: 11px;
  padding: 2px 5px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
  z-index: 1;
}
.tl-start-marker {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 0;
  border-left: 2px dashed #ef4444;
  z-index: 3;
  pointer-events: none;
}
.tl-start-flag {
  position: absolute;
  top: -2px;
  transform: translateX(2px);
  background: #ef4444;
  color: white;
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 3px;
  white-space: nowrap;
  z-index: 4;
}

/* ---- Month calendar ---- */
.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.month-dow {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  font-weight: 600;
  padding-bottom: 4px;
}
.month-day {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 100px;
  padding: 6px;
  font-size: 12px;
  cursor: pointer;
}
.month-day:hover { border-color: var(--primary); }
.month-day.other-month { opacity: 0.4; }
.month-day.today { border-color: var(--primary); background: #eff6ff; }
.month-day .day-num { font-weight: 600; margin-bottom: 4px; }
.month-day .chip {
  display: block;
  background: #e0e7ff;
  color: #3730a3;
  border-radius: 4px;
  padding: 1px 5px;
  margin-bottom: 2px;
  font-size: 11px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.month-day .chip.start { background: #fee2e2; color: #991b1b; font-weight: 600; }

/* ---- Modal ---- */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 30, 0.4);
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--panel);
  border-radius: 10px;
  padding: 22px;
  width: 420px;
  max-width: 92vw;
  max-height: 88vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.modal h2 { margin: 0 0 6px; font-size: 17px; }
.modal label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); }
.modal input, .modal select, .modal textarea {
  font-size: 14px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  color: var(--text);
}
.modal .row-2 { display: flex; gap: 10px; }
.modal .row-2 label { flex: 1; }
.modal .checkbox-label { flex-direction: row; align-items: center; gap: 6px; }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
