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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  height: 100vh;
  overflow: hidden;
  padding: 20px;
}

button,
select,
input {
  font: inherit;
}

[hidden] {
  display: none !important;
}

.container {
  width: 100%;
  height: calc(100vh - 40px);
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
  min-height: 0;
}

body.has-data .container {
  grid-template-columns: minmax(220px, 280px) minmax(0, 1fr);
}

body.has-data.department-sidebar-collapsed .container {
  grid-template-columns: 44px minmax(0, 1fr);
}

.main-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

body:not(.has-data) .main-panel {
  align-items: center;
  justify-content: center;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 12px;
  flex: 0 0 auto;
}

.stat-card,
.card,
.controls-bar,
.department-sidebar {
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.stat-card {
  padding: 16px;
  transition: transform 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-card .label {
  font-size: 0.8rem;
  color: #666;
  margin-bottom: 6px;
  text-align: center;
}

.stat-card .value {
  font-size: 1.5rem;
  font-weight: 700;
  color: #333;
  text-align: center;
  line-height: 1.4;
}

.stat-card .value .join {
  color: #10b981;
}

.stat-card .value .leave {
  color: #ef4444;
}

.controls-bar {
  padding: 14px 20px;
  margin-bottom: 12px;
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
  align-items: center;
  flex: 0 0 auto;
}

.control-label {
  font-size: 0.9rem;
  color: #555;
}

.chart-toggle-buttons {
  display: inline-flex;
  gap: 2px;
}

.chart-toggle-buttons input[type="radio"] {
  display: none;
}

.chart-toggle-btn,
.secondary-btn {
  border: 1px solid #ddd;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s ease;
}

.chart-toggle-btn {
  display: inline-block;
  padding: 6px 16px;
  background: #f0f0f0;
  color: #666;
  user-select: none;
}

.chart-toggle-btn:hover,
.secondary-btn:hover {
  border-color: #667eea;
}

.chart-toggle-btn:first-of-type {
  border-radius: 6px 0 0 6px;
}

.chart-toggle-btn:last-of-type {
  border-radius: 0 6px 6px 0;
}

.chart-toggle-buttons input[type="radio"]:checked + .chart-toggle-btn {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.secondary-btn {
  padding: 8px 14px;
  background: white;
  color: #555;
}

.controls-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.file-hint {
  font-size: 0.8rem;
  color: #888;
  white-space: nowrap;
}

.department-sidebar {
  padding: 14px 12px;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  transition: padding 0.2s ease;
}

.sidebar-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 4px 10px;
}

.sidebar-toggle-btn {
  width: 26px;
  height: 26px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: white;
  color: #4f46e5;
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0;
  flex: 0 0 auto;
  transition: all 0.2s ease;
}

.sidebar-toggle-btn:hover {
  border-color: #667eea;
  background: #f4f6ff;
}

.sidebar-title {
  flex: 1;
  color: #333;
  font-size: 0.95rem;
  font-weight: 700;
}

.sidebar-clear-btn {
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: white;
  color: #555;
  cursor: pointer;
  font-size: 0.78rem;
  line-height: 1;
  padding: 5px 8px;
  transition: all 0.2s ease;
}

.sidebar-clear-btn:hover:not(:disabled) {
  border-color: #667eea;
  color: #667eea;
}

.sidebar-clear-btn:disabled {
  cursor: default;
  opacity: 0.45;
}

.dept-tree {
  min-height: 0;
  overflow-y: auto;
  overflow-x: auto;
  padding: 2px 2px 8px;
  font-size: 0.9rem;
}

.dept-tree-item {
  position: relative;
  padding: 5px 6px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.dept-tree-item:hover {
  background-color: #f5f5f5;
}

.dept-tree-item.selected {
  background-color: #e8f0fe;
  color: #667eea;
}

.dept-tree-item.partially-selected {
  background-color: #f4f6ff;
  color: #4f46e5;
}

.dept-tree-toggle {
  width: 18px;
  height: 18px;
  border: 0;
  background: transparent;
  cursor: pointer;
  color: #888;
  font-size: 10px;
  line-height: 18px;
  padding: 0;
  transition: transform 0.2s ease;
  flex: 0 0 auto;
}

.dept-tree-checkbox {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  accent-color: #667eea;
}

.dept-tree-toggle.expanded {
  transform: rotate(90deg);
}

.hidden-toggle {
  visibility: hidden;
}

.dept-tree-title {
  flex: 1;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dept-tree-item::after {
  content: attr(data-tooltip);
  position: absolute;
  right: 6px;
  top: 50%;
  z-index: 20;
  width: max-content;
  padding: 3px 6px;
  border-radius: 999px;
  background: #eef2ff;
  color: #4f46e5;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.16);
  opacity: 0;
  pointer-events: none;
  transform: translateY(-50%);
}

.dept-tree-item:hover::after,
.dept-tree-item:focus-within::after {
  opacity: 1;
}

.dept-tree-children {
  display: none;
  margin-left: 18px;
  border-left: 1px solid #eee;
  padding-left: 4px;
}

.dept-tree-children.visible {
  display: block;
}

.department-sidebar.is-collapsed {
  padding: 8px;
  align-items: center;
}

.department-sidebar.is-collapsed .sidebar-header {
  justify-content: center;
  padding: 0;
}

.department-sidebar.is-collapsed .sidebar-title,
.department-sidebar.is-collapsed .sidebar-clear-btn,
.department-sidebar.is-collapsed .dept-tree {
  display: none;
}

.card {
  padding: 20px;
  margin-bottom: 0;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

.chart-container {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}

.retention-chart-container {
  min-height: 0;
}

.department-range-controls {
  display: grid;
  grid-template-columns: 246px minmax(260px, 1fr) 150px;
  gap: 16px;
  align-items: end;
  margin-bottom: 18px;
  color: #374151;
  font-size: 0.85rem;
}

.department-range-date {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 600;
}

.department-start-row {
  display: grid;
  grid-template-columns: 150px 88px;
  gap: 8px;
}

.department-range-controls input[type="date"],
.department-range-controls select {
  height: 34px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 4px 8px;
  color: #111827;
  min-width: 0;
}

.department-range-controls select {
  background: white;
}

.department-range-slider {
  --range-start: 0%;
  --range-end: 100%;
  position: relative;
  height: 34px;
  align-self: end;
}

.department-range-slider::before,
.department-range-slider::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 6px;
  transform: translateY(-50%);
  border-radius: 999px;
}

.department-range-slider::before {
  background: #e5e7eb;
}

.department-range-slider::after {
  left: var(--range-start);
  right: calc(100% - var(--range-end));
  background: #667eea;
}

.department-range-slider input[type="range"] {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 34px;
  margin: 0;
  appearance: none;
  background: transparent;
  pointer-events: none;
}

.department-range-slider input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  background: transparent;
}

.department-range-slider input[type="range"]::-webkit-slider-thumb {
  width: 18px;
  height: 18px;
  margin-top: -6px;
  appearance: none;
  border: 2px solid #667eea;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 4px rgba(17, 24, 39, 0.2);
  cursor: pointer;
  pointer-events: auto;
}

.department-range-slider input[type="range"]::-moz-range-track {
  height: 6px;
  background: transparent;
}

.department-range-slider input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 2px solid #667eea;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 4px rgba(17, 24, 39, 0.2);
  cursor: pointer;
  pointer-events: auto;
}

.department-analysis-layout {
  --department-table-share: 64%;
  display: grid;
  grid-template-columns: minmax(0, var(--department-table-share)) 10px minmax(220px, 1fr);
  gap: 10px;
  align-items: stretch;
  margin-bottom: 16px;
  flex: 1 1 auto;
  min-height: 0;
}

.department-layout-splitter {
  position: relative;
  align-self: stretch;
  border-radius: 999px;
  cursor: col-resize;
  touch-action: none;
}

.department-layout-splitter::before {
  content: "";
  position: absolute;
  top: 12px;
  bottom: 12px;
  left: 50%;
  width: 3px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: #d1d5db;
  transition: background 0.15s ease, width 0.15s ease;
}

.department-layout-splitter:hover::before,
.department-layout-splitter:focus-visible::before,
.department-analysis-layout.is-resizing .department-layout-splitter::before {
  width: 4px;
  background: #667eea;
}

.department-layout-splitter:focus-visible {
  outline: 2px solid rgba(102, 126, 234, 0.35);
  outline-offset: 2px;
}

.department-chart-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.department-table-wrap {
  min-width: 0;
  min-height: 0;
  overflow: auto;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.department-table {
  width: 100%;
  min-width: 980px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.84rem;
}

.department-table th,
.department-table td {
  padding: 8px 10px;
  border-bottom: 1px solid #e5e7eb;
  text-align: right;
  white-space: nowrap;
}

.department-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f9fafb;
  color: #374151;
  cursor: pointer;
  font-weight: 700;
  user-select: none;
}

.department-table thead tr:first-child th {
  height: 31px;
  padding-top: 6px;
  padding-bottom: 6px;
}

.department-table thead tr:nth-child(2) th {
  top: 31px;
}

.department-table .department-group-header {
  text-align: center;
  cursor: default;
  font-size: 0.76rem;
  letter-spacing: 0.04em;
  color: #1f2937;
  border-bottom-color: #d1d5db;
}

.department-table .department-group-count {
  background: #eff6ff;
}

.department-table .department-group-ratio {
  background: #ecfdf5;
}

.department-table .department-group-tenure {
  background: #fff7ed;
}

.department-table th.department-column-header {
  top: 0;
  cursor: default;
}

.department-table th::after {
  content: "";
  display: inline-block;
  width: 12px;
}

.department-table th[data-sort-direction="asc"]::after {
  content: "▲";
  color: #667eea;
  font-size: 0.7rem;
  margin-left: 4px;
}

.department-table th[data-sort-direction="desc"]::after {
  content: "▼";
  color: #667eea;
  font-size: 0.7rem;
  margin-left: 4px;
}

.metric-help {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  margin-left: 5px;
  border: 1px solid #9ca3af;
  border-radius: 50%;
  color: #4b5563;
  background: #ffffff;
  font-size: 0.65rem;
  line-height: 1;
  cursor: help;
}

.metric-help::after {
  content: attr(data-tooltip);
  position: absolute;
  right: calc(100% + 8px);
  top: 50%;
  z-index: 12;
  width: max-content;
  max-width: 280px;
  padding: 7px 9px;
  border-radius: 6px;
  background: #111827;
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.4;
  text-align: left;
  white-space: normal;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.18);
  opacity: 0;
  pointer-events: none;
  transform: translate(4px, -50%);
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.metric-help:hover::after,
.metric-help:focus-visible::after {
  opacity: 1;
  transform: translate(0, -50%);
}

.department-table th.department-column-header,
.department-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 3;
  text-align: left;
  background: white;
}

.department-table th.department-column-header::after,
.department-table .department-group-header::after {
  content: none;
}

.department-table th.department-column-header {
  z-index: 4;
  background: #f9fafb;
}

.department-table tbody tr[data-depth="0"] td {
  background: #ffffff;
}

.department-table tbody tr[data-depth="1"] td {
  background: #f9fafb;
}

.department-table tbody tr[data-depth="2"] td {
  background: #f3f4f6;
}

.department-table tbody tr[data-depth="3"] td {
  background: #e5e7eb;
}

.department-table tbody tr[data-depth="4"] td {
  background: #d1d5db;
}

.department-table tbody tr[data-depth="5"] td,
.department-table tbody tr[data-depth="6"] td,
.department-table tbody tr[data-depth="7"] td,
.department-table tbody tr[data-depth="8"] td {
  background: #cbd5e1;
}

.department-table tbody tr:hover td,
.department-table tbody tr:hover td:first-child {
  background: #eef2ff;
}

.department-table td.department-rank-gold {
  background: #fef3c7 !important;
  color: #78350f;
  font-weight: 700;
}

.department-table td.department-rank-silver {
  background: #e5e7eb !important;
  color: #374151;
  font-weight: 700;
}

.department-table td.department-rank-bronze {
  background: #fed7aa !important;
  color: #7c2d12;
  font-weight: 700;
}

.department-table tbody tr:hover td.department-rank-gold {
  background: #fde68a !important;
}

.department-table tbody tr:hover td.department-rank-silver {
  background: #d1d5db !important;
}

.department-table tbody tr:hover td.department-rank-bronze {
  background: #fdba74 !important;
}

.department-table tbody tr.department-row-highlight td,
.department-table tbody tr.department-row-highlight td:first-child,
.department-table tbody tr.department-row-highlight td.department-rank-gold,
.department-table tbody tr.department-row-highlight td.department-rank-silver,
.department-table tbody tr.department-row-highlight td.department-rank-bronze {
  background: #dbeafe !important;
}

.department-tooltip-table {
  margin-top: 6px;
  border-collapse: collapse;
  font-size: 0.72rem;
}

.department-tooltip-range {
  margin-top: 3px;
  font-size: 0.7rem;
  font-weight: 500;
  opacity: 0.8;
  white-space: nowrap;
}

.department-tooltip-table th,
.department-tooltip-table td {
  padding: 2px 8px 2px 0;
  border: none;
  color: inherit;
  line-height: 1.35;
  white-space: nowrap;
}

.department-tooltip-table th {
  font-weight: 500;
  text-align: left;
  opacity: 0.78;
}

.department-tooltip-table td {
  min-width: 54px;
  font-weight: 700;
  text-align: right;
}

.department-name-cell {
  padding-left: calc(10px + var(--depth, 0) * 18px) !important;
  font-weight: 600;
  color: #111827;
  display: flex;
  align-items: center;
}

.department-row-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  margin-right: 3px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #4b5563;
  cursor: pointer;
  text-align: center;
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1;
  transition: background 0.15s ease, color 0.15s ease, transform 0.16s ease;
}

.department-row-toggle:hover:not(:disabled),
.department-row-toggle:focus-visible:not(:disabled) {
  background: #e0e7ff;
  color: #3730a3;
}

.department-row-toggle.expanded {
  transform: rotate(90deg);
}

.department-row-toggle:disabled {
  cursor: default;
  color: transparent;
}

.chart {
  height: 100%;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  z-index: 10;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.loading-overlay p {
  margin-top: 12px;
  color: #666;
  font-size: 0.9rem;
}

.upload-section {
  width: min(360px, 100%);
  flex: 0 0 auto;
  text-align: center;
  padding: 44px 28px;
}

.upload-btn {
  width: auto;
  min-width: 160px;
  padding: 12px 24px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s ease;
}

.upload-btn:hover {
  background: #5a6fd6;
}

.upload-btn:disabled {
  cursor: wait;
  opacity: 0.65;
}

.upload-loading {
  display: inline-flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  margin-top: 16px;
  color: #666;
  font-size: 0.9rem;
}

.upload-loading .spinner {
  width: 22px;
  height: 22px;
}

.upload-hint {
  margin-top: 12px;
  font-size: 0.85rem;
  color: #888;
}

.legend-note {
  font-size: 0.8rem;
  color: #666;
  margin-top: 10px;
  text-align: center;
}

.legend-note-emphasis {
  color: #dc2626;
  font-weight: 700;
}

@media (max-width: 1100px) {
  body.has-data .container {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(140px, 24vh) minmax(0, 1fr);
  }

  body.has-data.department-sidebar-collapsed .container {
    grid-template-columns: 1fr;
    grid-template-rows: 46px minmax(0, 1fr);
  }

  .department-analysis-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  }

  .department-layout-splitter {
    display: none;
  }
}

@media (max-width: 768px) {
  body {
    padding: 12px;
  }

  .container {
    height: calc(100vh - 24px);
  }

  body.has-data .container {
    grid-template-rows: minmax(120px, 22vh) minmax(0, 1fr);
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .department-range-controls {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .controls-bar {
    flex-direction: column;
    align-items: flex-start;
  }

  .controls-actions {
    width: 100%;
    justify-content: flex-end;
    margin-left: 0;
  }
}

