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

:root {
  --bg-page: #F5F5F0;
  --bg-panel: #FFFFFF;
  --border-color: #D9D9D4;
  --text-primary: #1a1a1a;
  --text-secondary: #666;
  --text-muted: #999;
  --accent: #5B7FC7;
  --accent-hover: #4a6db5;
  --success: #2EAD6A;
  --warning: #D4952B;
  --danger: #D43E3E;
  --header-height: 56px;
  --tree-height: 140px;
  --bottom-height: 160px;
  --radius: 8px;
  --radius-sm: 4px;

  /* Skill colors */
  --skill-1: #5B7FC7;
  --skill-2: #2BA5A5;
  --skill-3: #D4952B;
  --skill-4: #6558CF;
  --skill-5: #C7622A;
  --skill-6: #2EAD6A;
  --skill-7: #C94F6D;
  --skill-8: #9055D4;
  --skill-9: #D43E3E;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.4;
  color: var(--text-primary);
  background: var(--bg-page);
}

/* ===== Page Grid ===== */
body {
  display: grid;
  grid-template-rows: var(--header-height) 1fr var(--tree-height) var(--bottom-height);
  grid-template-columns: 1fr;
  grid-template-areas:
    "header"
    "stage"
    "tree"
    "bottom";
  gap: 0;
}

/* ===== Header ===== */
#app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  z-index: 10;
}

.header-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  white-space: nowrap;
}

.round-counter {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-page);
  padding: 4px 12px;
  border-radius: 12px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

#round-num {
  font-family: 'SF Mono', ui-monospace, 'Cascadia Code', 'Fira Code', monospace;
  color: var(--accent);
  min-width: 2ch;
  display: inline-block;
  text-align: center;
}

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

/* Control Buttons */
.ctrl-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-panel);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.ctrl-btn:hover:not(:disabled) {
  background: var(--bg-page);
  color: var(--text-primary);
  border-color: #bbb;
}

.ctrl-btn:active:not(:disabled) {
  transform: scale(0.95);
}

.ctrl-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.ctrl-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Speed Control */
.speed-control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: 12px;
  padding-left: 12px;
  border-left: 1px solid var(--border-color);
}

.speed-control label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#speed {
  -webkit-appearance: none;
  appearance: none;
  width: 80px;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

#speed::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-panel);
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  cursor: pointer;
  transition: transform 0.1s;
}

#speed::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

#speed::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-panel);
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  cursor: pointer;
}

#speed-label {
  font-size: 12px;
  font-weight: 600;
  font-family: 'SF Mono', ui-monospace, monospace;
  color: var(--text-secondary);
  min-width: 2.5ch;
  text-align: right;
}

/* ===== Main Stage ===== */
#main-stage {
  grid-area: stage;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  overflow: hidden;
  position: relative;
}

#stage-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* SVG Region Backgrounds */
.region-bg {
  fill: var(--bg-panel);
  stroke: none;
}

.region-bg.region-teams {
  fill: #FAFAF8;
}

.region-bg.region-work {
  fill: var(--bg-panel);
}

.region-bg.region-market {
  fill: #FAFAF8;
}

.region-separator {
  stroke: var(--border-color);
  stroke-width: 1;
  stroke-dasharray: 4 4;
}

.region-label {
  fill: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  text-anchor: middle;
  dominant-baseline: middle;
}

/* ===== Upgrade Tree Strip ===== */
#tree-strip {
  grid-area: tree;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  overflow: hidden;
  padding: 0 20px;
}

#tree-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* ===== Bottom Panels ===== */
#bottom-panels {
  grid-area: bottom;
  display: grid;
  grid-template-columns: 40% 60%;
  gap: 0;
  overflow: hidden;
}

.panel {
  background: var(--bg-panel);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#stats-panel {
  border-right: 1px solid var(--border-color);
}

.panel-heading {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

/* Stats Table */
#stats-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

#stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

#stats-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
}

#stats-table th {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  background: var(--bg-page);
  padding: 4px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}

#stats-table th:last-child {
  text-align: right;
}

#stats-table td {
  padding: 4px 10px;
  border-bottom: 1px solid #f0f0ec;
  vertical-align: middle;
}

#stats-table td:last-child {
  text-align: right;
  font-family: 'SF Mono', ui-monospace, monospace;
  font-weight: 600;
}

#stats-table tr:hover td {
  background: #FAFAF8;
}

.skill-digits {
  font-family: 'SF Mono', ui-monospace, monospace;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 1px;
}

.team-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 8px;
  background: var(--bg-page);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

/* Event Log */
#log-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0;
  display: flex;
  flex-direction: column;
}

.log-entry {
  padding: 3px 14px;
  font-size: 11px;
  line-height: 1.5;
  color: var(--text-secondary);
  border-bottom: 1px solid #f5f5f2;
  animation: log-slide-in 0.2s ease-out;
}

.log-entry:first-child {
  background: #FAFAF8;
}

.log-round {
  font-family: 'SF Mono', ui-monospace, monospace;
  font-weight: 600;
  font-size: 10px;
  color: var(--text-muted);
  margin-right: 6px;
}

.log-type {
  font-weight: 600;
  margin-right: 4px;
}

.log-type.project-appeared { color: var(--accent); }
.log-type.project-claimed { color: var(--skill-2); }
.log-type.work-progress { color: var(--text-muted); }
.log-type.project-completed { color: var(--success); }
.log-type.project-expired { color: var(--danger); }
.log-type.skill-upgraded { color: var(--skill-8); }
.log-type.team-formed { color: var(--skill-4); }
.log-type.team-dissolved { color: var(--text-muted); }

@keyframes log-slide-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ===== SVG Element Styles ===== */

/* Person icon */
.person-group {
  cursor: default;
}

.person-head {
  fill: #555;
  stroke: none;
}

.person-body {
  fill: #555;
  stroke: none;
}

.person-skill-label {
  font-family: 'SF Mono', ui-monospace, monospace;
  font-size: 11px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: auto;
  letter-spacing: 1.5px;
}

.person-name {
  font-size: 9px;
  fill: var(--text-muted);
  text-anchor: middle;
  dominant-baseline: hanging;
}

/* Team cluster */
.team-bg {
  fill: var(--bg-panel);
  stroke: var(--border-color);
  stroke-width: 1.5;
  stroke-dasharray: 5 3;
  rx: 10;
  ry: 10;
}

.team-label-pill {
  rx: 8;
  ry: 8;
}

.team-label-text {
  font-size: 10px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
}

/* Project card */
.project-card-bg {
  rx: 6;
  ry: 6;
  stroke-width: 1.5;
}

.project-digits {
  font-family: 'SF Mono', ui-monospace, monospace;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 2px;
  dominant-baseline: central;
}

.project-progress-bg {
  fill: #eee;
  rx: 2;
  ry: 2;
}

.project-progress-fill {
  rx: 2;
  ry: 2;
  transition: width 0.3s ease;
}

.project-points-label {
  font-size: 9px;
  font-weight: 600;
  fill: var(--text-muted);
  text-anchor: end;
  dominant-baseline: central;
}

.project-expiry-label {
  font-size: 8px;
  fill: var(--text-muted);
  text-anchor: end;
  dominant-baseline: central;
}

/* Work connections */
.work-connection {
  fill: none;
  stroke-width: 2;
  stroke-dasharray: 8 4;
  opacity: 0.7;
}

/* Floating points animation */
.float-points {
  font-size: 16px;
  font-weight: 800;
  fill: var(--success);
  text-anchor: middle;
  dominant-baseline: auto;
  pointer-events: none;
}

/* Upgrade tree */
.tree-node-bg {
  stroke-width: 2;
}

.tree-node-label {
  font-family: 'SF Mono', ui-monospace, monospace;
  font-size: 14px;
  font-weight: 800;
  text-anchor: middle;
  dominant-baseline: central;
}

.tree-edge {
  fill: none;
  stroke: var(--border-color);
  stroke-width: 1.5;
  marker-end: url(#tree-arrow);
}

/* ===== Scrollbar Styling ===== */
#stats-content::-webkit-scrollbar,
#log-content::-webkit-scrollbar {
  width: 6px;
}

#stats-content::-webkit-scrollbar-track,
#log-content::-webkit-scrollbar-track {
  background: transparent;
}

#stats-content::-webkit-scrollbar-thumb,
#log-content::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

#stats-content::-webkit-scrollbar-thumb:hover,
#log-content::-webkit-scrollbar-thumb:hover {
  background: #bbb;
}

/* ===== Tooltip ===== */
.svg-tooltip {
  position: fixed;
  pointer-events: none;
  background: rgba(0,0,0,0.82);
  color: #fff;
  font-size: 11px;
  font-family: 'SF Mono', ui-monospace, monospace;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.15s;
}

.svg-tooltip.visible {
  opacity: 1;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  body {
    grid-template-rows: var(--header-height) 50vh var(--tree-height) auto;
    overflow-y: auto;
  }

  #bottom-panels {
    grid-template-columns: 1fr;
    grid-template-rows: 160px 160px;
    height: auto;
  }

  #stats-panel {
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  .controls {
    flex-wrap: wrap;
  }

  .speed-control {
    margin-left: 0;
    padding-left: 0;
    border-left: none;
  }
}

@media (max-width: 600px) {
  #app-header {
    padding: 0 10px;
    gap: 10px;
    flex-wrap: wrap;
    height: auto;
    min-height: var(--header-height);
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .header-title {
    font-size: 15px;
  }
}
