@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

/* ─── Design Tokens ──────────────────────────────────────────────── */
:root {
  --bg-primary: #0f1117;
  --bg-secondary: #1a1d27;
  --bg-card: rgba(26, 29, 39, 0.85);
  --bg-glass: rgba(255, 255, 255, 0.04);
  --bg-hover: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(99, 102, 241, 0.5);
  --text-primary: #e4e4e7;
  --text-secondary: #a1a1aa;
  --text-muted: #71717a;
  --accent: #6366f1;
  --accent-glow: rgba(99, 102, 241, 0.15);
  --success: #22c55e;
  --warning: #f59e0b;
  --size-L: #3b82f6;
  --size-M: #22c55e;
  --size-S: #f59e0b;
  --size-XS: #f97316;
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
}

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

body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ─── Layout ─────────────────────────────────────────────────────── */
.app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

/* ─── Header ─────────────────────────────────────────────────────── */
.header {
  text-align: center;
  padding: 40px 20px 32px;
  margin-bottom: 24px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.08) 0%, rgba(139, 92, 246, 0.05) 100%);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.header h1 {
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #818cf8, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.header .subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 400;
}

/* ─── Stats Cards ────────────────────────────────────────────────── */
.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  text-align: center;
  backdrop-filter: blur(12px);
  transition: transform var(--transition), border-color var(--transition);
}

.stat-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}

.stat-card .stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent);
}

.stat-card .stat-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-top: 4px;
}

.stat-card.size-L .stat-value { color: var(--size-L); }
.stat-card.size-M .stat-value { color: var(--size-M); }
.stat-card.size-S .stat-value { color: var(--size-S); }
.stat-card.size-XS .stat-value { color: var(--size-XS); }

.stat-card.clickable { cursor: pointer; }

.stat-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow), 0 0 16px var(--accent-glow);
  transform: translateY(-2px);
}

.stat-card.active.size-L  { border-color: var(--size-L); box-shadow: 0 0 0 2px rgba(59,130,246,0.2), 0 0 16px rgba(59,130,246,0.1); }
.stat-card.active.size-M  { border-color: var(--size-M); box-shadow: 0 0 0 2px rgba(34,197,94,0.2), 0 0 16px rgba(34,197,94,0.1); }
.stat-card.active.size-S  { border-color: var(--size-S); box-shadow: 0 0 0 2px rgba(245,158,11,0.2), 0 0 16px rgba(245,158,11,0.1); }
.stat-card.active.size-XS { border-color: var(--size-XS); box-shadow: 0 0 0 2px rgba(249,115,22,0.2), 0 0 16px rgba(249,115,22,0.1); }

/* ─── Search & Filters ───────────────────────────────────────────── */
.controls {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
  align-items: center;
}

.search-box {
  flex: 1;
  min-width: 280px;
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 12px 16px 12px 44px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.search-box input::placeholder { color: var(--text-muted); }

.search-box input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.search-box .search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 16px;
  pointer-events: none;
}

.filter-select {
  padding: 12px 32px 12px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2371717a' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  min-width: 130px;
  transition: border-color var(--transition);
}

.filter-select:focus { border-color: var(--border-focus); }

.filter-select option { background: var(--bg-secondary); color: var(--text-primary); }

/* ─── Tabs ───────────────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  padding: 4px;
  border: 1px solid var(--border);
}

.tab-btn {
  flex: 1;
  padding: 10px 16px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.tab-btn:hover { color: var(--text-primary); background: var(--bg-glass); }
.tab-btn.active { background: var(--accent); color: #fff; }

/* ─── Table ──────────────────────────────────────────────────────── */
.table-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  backdrop-filter: blur(12px);
}

.table-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-secondary);
  flex-wrap: wrap;
  gap: 8px;
}

/* Export buttons */
.export-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.export-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.export-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 12px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.export-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-glow);
}

.export-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.export-btn:disabled {
  opacity: 0.5;
  cursor: wait;
}

.export-icon {
  font-size: 13px;
}

.results-table {
  width: 100%;
  border-collapse: collapse;
}

.results-table th {
  padding: 12px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: color var(--transition);
}

.results-table th:hover { color: var(--accent); }
.results-table th.sorted { color: var(--accent); }
.results-table th .sort-arrow { margin-left: 4px; font-size: 10px; }

.results-table td {
  padding: 10px 14px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.results-table tr { transition: background var(--transition); }
.results-table tbody tr:hover { background: var(--bg-hover); cursor: pointer; }
.results-table tbody tr:last-child td { border-bottom: none; }

/* Size badge */
.size-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.size-badge.L  { background: rgba(59,130,246,0.15); color: var(--size-L); }
.size-badge.M  { background: rgba(34,197,94,0.15);  color: var(--size-M); }
.size-badge.S  { background: rgba(245,158,11,0.15); color: var(--size-S); }
.size-badge.XS { background: rgba(249,115,22,0.15); color: var(--size-XS); }

/* Pagination */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 16px;
  border-top: 1px solid var(--border);
}

.pagination button {
  padding: 6px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
}

.pagination button:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.pagination button:disabled { opacity: 0.3; cursor: not-allowed; }
.pagination .page-info { color: var(--text-muted); font-size: 13px; }

/* ─── Detail Panel ───────────────────────────────────────────────── */
.detail-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: none;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.2s ease;
}

.detail-overlay.show { display: flex; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.detail-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 90%;
  max-width: 640px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 28px;
  box-shadow: var(--shadow);
  animation: slideUp 0.25s ease;
}

.detail-card .close-btn {
  float: right;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
  transition: color var(--transition);
}

.detail-card .close-btn:hover { color: var(--text-primary); }

.detail-card h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}

.detail-card .detail-code {
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 20px;
}

.detail-section {
  margin-bottom: 20px;
}

.detail-section h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.detail-grid {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 8px 12px;
  font-size: 13px;
}

.detail-grid .label { color: var(--text-muted); }
.detail-grid .value { color: var(--text-primary); font-weight: 500; }

/* Group popup table */
.group-popup-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.group-popup-table th {
  text-align: left;
  padding: 8px 10px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.group-popup-table td {
  padding: 10px;
  border-bottom: 1px solid var(--border);
}

.group-popup-table tr.group-row {
  cursor: pointer;
  transition: background var(--transition);
}

.group-popup-table tr.group-row:hover {
  background: var(--bg-hover);
}

.group-popup-table tbody tr:last-child td {
  border-bottom: none;
}

/* Hierarchy path in detail */
.hierarchy-path {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--bg-glass);
  border-radius: var(--radius-sm);
}

.path-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding-left: calc(var(--depth, 0) * 20px);
}

.path-item .path-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.path-item .path-type {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  min-width: 60px;
}

/* ─── Hierarchy Tree Tab ─────────────────────────────────────────── */
.tree-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
  backdrop-filter: blur(12px);
}

.tree-node {
  margin-left: 16px;
}

.tree-node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: background var(--transition);
}

.tree-node-header:hover { background: var(--bg-hover); }

.tree-toggle {
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform var(--transition);
  flex-shrink: 0;
}

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

.tree-type-badge {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 4px;
  font-weight: 600;
  text-transform: uppercase;
}

.tree-type-badge.group8 { background: rgba(99,102,241,0.15); color: #818cf8; }
.tree-type-badge.group17 { background: rgba(139,92,246,0.15); color: #a78bfa; }
.tree-type-badge.group43 { background: rgba(59,130,246,0.15); color: #60a5fa; }
.tree-type-badge.office { background: rgba(34,197,94,0.12); color: #4ade80; }

.tree-children {
  display: none;
}

.tree-children.open {
  display: block;
}

/* ─── Loading & Empty ────────────────────────────────────────────── */
.loading, .empty-state, .error-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

.loading .spinner {
  display: inline-block;
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.error-state { color: #ef4444; }

/* ─── Responsive ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .app { padding: 12px; }
  .header { padding: 24px 16px 20px; }
  .header h1 { font-size: 22px; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .controls { flex-direction: column; }
  .search-box { min-width: 100%; }
  .filter-select { width: 100%; }
  .results-table td, .results-table th { padding: 8px 10px; font-size: 12px; }
  .detail-card { padding: 20px; width: 95%; }
}

/* ─── Scrollbar ──────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ─── Tools Button ───────────────────────────────────────────────── */
.tools-btn {
  position: absolute;
  top: 18px;
  right: 24px;
  background: var(--bg-glass);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
}
.tools-btn:hover {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--text-primary);
}
.header { position: relative; }

/* ─── Group Tool Modal ───────────────────────────────────────────── */
.group-tool-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  width: min(540px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
}
.group-tool-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.group-tool-header h2 {
  font-size: 1.2rem;
  color: var(--text-primary);
  margin: 0;
}
.group-tool-desc {
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin: 0 0 12px;
}
.group-input {
  width: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-family: 'Inter', sans-serif;
  font-size: 0.95rem;
  padding: 12px;
  resize: vertical;
  box-sizing: border-box;
}
.group-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.group-submit-btn {
  width: 100%;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 12px;
  transition: opacity 0.2s;
}
.group-submit-btn:hover { opacity: 0.9; }
.group-submit-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.group-result {
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
.group-output-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  color: var(--text-secondary);
  font-size: 0.85rem;
}
.copy-btn {
  background: var(--bg-glass);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.2s;
}
.copy-btn:hover {
  background: var(--accent-glow);
  color: var(--text-primary);
}
.group-output {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  color: var(--accent);
  font-size: 1.05rem;
  font-weight: 600;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  line-height: 1.6;
}
.group-detail {
  margin-top: 12px;
  color: var(--text-muted);
  font-size: 0.8rem;
}
.group-detail .notfound {
  color: var(--warning);
  margin-top: 6px;
}

