:root {
  --bg-body: #0f172a;
  --bg-body-gradient: radial-gradient(at 0% 0%, rgba(56, 189, 248, 0.15) 0px, transparent 50%),
                      radial-gradient(at 100% 0%, rgba(236, 72, 153, 0.15) 0px, transparent 50%),
                      radial-gradient(at 100% 100%, rgba(14, 165, 233, 0.15) 0px, transparent 50%),
                      radial-gradient(at 0% 100%, rgba(232, 121, 249, 0.15) 0px, transparent 50%);
  --bg-card: rgba(30, 41, 59, 0.7);
  --bg-input: rgba(15, 23, 42, 0.6);
  --border-color: rgba(148, 163, 184, 0.1);
  --border-color-hover: rgba(148, 163, 184, 0.2);
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-glow: rgba(59, 130, 246, 0.5);
  --success: #10b981;
  --success-bg: rgba(16, 185, 129, 0.2);
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --danger-bg: rgba(239, 68, 68, 0.2);
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 24px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --shadow-glow: 0 0 20px rgba(59, 130, 246, 0.15);
  --blur-md: blur(12px);
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { overflow-x: hidden; width: 100%; }

body {
  font-family: var(--font-sans);
  background-color: var(--bg-body);
  background-image: var(--bg-body-gradient);
  background-attachment: fixed;
  color: var(--text-primary);
  min-height: 100vh;
  padding: 20px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;
  touch-action: manipulation;
}

/* Orbs */
.orb { position: fixed; border-radius: 50%; filter: blur(80px); opacity: 0.4; z-index: -1; pointer-events: none; }
.orb-1 { width: 500px; height: 500px; background: rgba(59,130,246,0.4); top: -100px; left: -100px; animation: float-1 25s infinite ease-in-out; }
.orb-2 { width: 400px; height: 400px; background: rgba(236,72,153,0.3); bottom: -100px; right: -100px; animation: float-2 30s infinite ease-in-out; }
@keyframes float-1 { 0%,100%{transform:translate(0,0) scale(1)} 50%{transform:translate(200px,50px) scale(1.25)} }
@keyframes float-2 { 0%,100%{transform:translate(0,0) scale(1)} 50%{transform:translate(-180px,-60px) scale(.75)} }

.container { max-width: 1600px; margin: 0 auto; width: 100%; overflow-x: hidden; }

/* Header */
.header { text-align: center; margin-bottom: 24px; }
.header h1 {
  font-size: 2.5rem; font-weight: 700; margin-bottom: 6px;
  background: linear-gradient(to right, #38bdf8, #818cf8);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  letter-spacing: -0.025em;
}
.header p { font-size: 1.05rem; color: var(--text-secondary); }

.back-link {
  display: inline-block; margin-bottom: 8px;
  color: var(--text-muted); text-decoration: none;
  font-size: 0.85rem; font-weight: 500;
  transition: color 0.2s;
}
.back-link:hover { color: var(--primary); }
.back-link::before { content: '\2190 '; }

/* Layout */
.main-content {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 20px;
  align-items: start;
}

.canvas-column { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

/* Progress bar */
.progress-bar-container {
  position: relative; height: 24px;
  background: var(--bg-card); border-radius: var(--radius-sm);
  border: 1px solid var(--border-color); overflow: hidden;
}
.progress-bar {
  height: 100%; width: 0%; border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--primary), #818cf8);
  transition: width 0.3s ease;
}
.progress-label {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem; font-weight: 600; color: var(--text-primary);
  pointer-events: none; text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

/* Canvas */
.canvas-container {
  background: var(--bg-card); backdrop-filter: var(--blur-md);
  border-radius: var(--radius-lg); padding: 16px;
  box-shadow: var(--shadow-lg); border: 1px solid var(--border-color);
  transition: var(--transition); overflow: hidden;
}
.canvas-container:hover { border-color: var(--border-color-hover); box-shadow: var(--shadow-glow); }

#simulationCanvas {
  width: 100%; height: 600px;
  border-radius: var(--radius-md);
  background: #0f172a; display: block; will-change: transform; touch-action: none;
}

/* Charts row */
.charts-row {
  display: grid; grid-template-columns: 3fr 2fr; gap: 16px;
}
.chart-card {
  background: var(--bg-card); backdrop-filter: var(--blur-md);
  border-radius: var(--radius-md); padding: 16px;
  border: 1px solid var(--border-color);
  transition: var(--transition);
  min-width: 0;
}
.chart-card:hover { border-color: var(--border-color-hover); }
.chart-card h3 {
  font-size: 0.8rem; font-weight: 600;
  color: var(--text-secondary); margin-bottom: 10px;
  text-transform: uppercase; letter-spacing: 0.04em;
}
.chart-card canvas {
  width: 100%; height: 180px;
  border-radius: var(--radius-sm); display: block;
}

/* Control panel */
.control-panel {
  background: var(--bg-card); backdrop-filter: var(--blur-md);
  border-radius: var(--radius-lg); padding: 22px;
  box-shadow: var(--shadow-lg); border: 1px solid var(--border-color);
  max-height: calc(100vh - 60px); overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; scroll-behavior: smooth;
}
.control-panel:hover { border-color: var(--border-color-hover); }

.panel-section {
  margin-bottom: 22px; padding-bottom: 22px;
  border-bottom: 1px solid var(--border-color);
}
.panel-section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.panel-section h2 {
  font-size: 1.15rem; margin-bottom: 12px;
  color: var(--text-primary); font-weight: 600; letter-spacing: -0.025em;
}

.control-group { margin-bottom: 12px; }
.control-group label {
  display: block; font-weight: 500; margin-bottom: 6px;
  color: var(--text-secondary); font-size: 0.9rem;
}

.checkbox-label {
  display: flex; align-items: center; gap: 10px;
  cursor: pointer; padding: 6px; border-radius: var(--radius-sm);
  transition: var(--transition);
}
.checkbox-label:hover { background: rgba(59,130,246,0.05); }
.checkbox-label input[type="checkbox"] { width: 18px; height: 18px; cursor: pointer; accent-color: var(--primary); }
.checkbox-label span { color: var(--text-secondary); font-size: 0.9rem; }

.control-group input[type="range"] {
  width: 100%; height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--primary), #818cf8);
  outline: none; -webkit-appearance: none; cursor: pointer; touch-action: pan-x;
}
.control-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: var(--text-primary); cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3); border: 2px solid var(--primary);
  transition: var(--transition);
}
.control-group input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.1); box-shadow: 0 0 0 4px rgba(59,130,246,0.2); }
.control-group input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--text-primary); cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3); border: 2px solid var(--primary);
}

/* Buttons */
.btn {
  padding: 10px 20px; border: 1px solid transparent; border-radius: var(--radius-sm);
  font-size: 0.95rem; font-weight: 600; cursor: pointer;
  transition: var(--transition); margin-right: 8px; margin-bottom: 8px;
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  min-height: 40px; user-select: none;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.btn-primary { background: var(--primary); color: white; box-shadow: var(--shadow-glow); }
.btn-primary:hover { background: var(--primary-hover); box-shadow: 0 0 25px rgba(37,99,235,0.4); }
.btn-secondary { background: rgba(239,68,68,0.2); color: var(--danger); border-color: rgba(239,68,68,0.3); }
.btn-secondary:hover { background: rgba(239,68,68,0.3); border-color: var(--danger); color: #fecaca; }
.btn-accent { background: rgba(16,185,129,0.2); color: var(--success); border-color: rgba(16,185,129,0.3); }
.btn-accent:hover { background: rgba(16,185,129,0.3); border-color: var(--success); color: #6ee7b7; }

/* Stats */
.stats-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat-item {
  background: rgba(59,130,246,0.05); padding: 10px; border-radius: var(--radius-md);
  display: flex; flex-direction: column; border: 1px solid var(--border-color);
  transition: var(--transition);
}
.stat-item:hover { transform: translateY(-2px); border-color: rgba(59,130,246,0.3); box-shadow: var(--shadow-glow); }
.stat-label {
  font-size: 0.75rem; color: var(--text-secondary); margin-bottom: 2px;
  font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em;
}
.stat-value {
  font-size: 1.25rem; font-weight: 700;
  background: linear-gradient(to right, #f8fafc, #cbd5e1);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}

.info-text { font-size: 0.85rem; line-height: 1.6; color: var(--text-secondary); }

/* Scrollbar */
.control-panel::-webkit-scrollbar { width: 6px; }
.control-panel::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); border-radius: var(--radius-sm); }
.control-panel::-webkit-scrollbar-thumb { background: linear-gradient(135deg, var(--primary), #818cf8); border-radius: var(--radius-sm); }

/* Generation History */
.generation-history {
  margin-top: 30px; padding: 22px;
  background: var(--bg-card); backdrop-filter: var(--blur-md);
  border-radius: var(--radius-lg); border: 1px solid var(--border-color);
  transition: var(--transition);
}
.generation-history:hover { border-color: var(--border-color-hover); }
.generation-history h2 {
  font-size: 1.3rem; margin-bottom: 16px;
  color: var(--text-primary); font-weight: 600; letter-spacing: -0.025em;
}

.table-container {
  overflow-x: auto; max-height: 400px; overflow-y: auto;
  border-radius: var(--radius-md); border: 1px solid var(--border-color);
  -webkit-overflow-scrolling: touch;
}

#generationTable { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
#generationTable thead {
  background: rgba(59,130,246,0.1); position: sticky; top: 0; z-index: 10;
}
#generationTable th {
  padding: 10px 14px; text-align: left; font-weight: 600;
  color: var(--text-secondary); text-transform: uppercase;
  font-size: 0.7rem; letter-spacing: 0.05em; border-bottom: 1px solid var(--border-color);
  white-space: nowrap;
}
#generationTable tbody tr { transition: var(--transition); animation: fadeIn 0.3s ease-out; }
#generationTable tbody tr:hover { background: rgba(59,130,246,0.05); }
#generationTable td {
  padding: 10px 14px; color: var(--text-secondary);
  border-bottom: 1px solid var(--border-color); white-space: nowrap;
}
#generationTable tbody tr:last-child td { border-bottom: none; }
#generationTable tbody tr:first-child { background: rgba(59,130,246,0.08); font-weight: 500; }
#generationTable tbody tr:first-child td { color: var(--text-primary); }

.table-empty { padding: 30px; text-align: center; color: var(--text-muted); font-size: 0.85rem; }

/* Loading */
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }

.loading-overlay {
  position: fixed; inset: 0; background: var(--bg-body);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  z-index: 9999; opacity: 1; transition: opacity 0.5s ease-out;
}
.loading-overlay.hidden { opacity: 0; pointer-events: none; }
.loading-spinner {
  width: 50px; height: 50px;
  border: 4px solid rgba(59,130,246,0.2); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 1s linear infinite; margin-bottom: 20px;
}
.loading-overlay p { color: var(--text-secondary); font-size: 1rem; animation: pulse 2s ease-in-out infinite; }

/* Hint tooltips */
.hint {
  display: inline-flex; align-items: center; justify-content: center;
  width: 15px; height: 15px; border-radius: 50%;
  background: rgba(148,163,184,0.15); border: 1px solid rgba(148,163,184,0.25);
  color: var(--text-muted); font-size: 9px; font-weight: 700;
  cursor: help; position: relative; margin-left: 4px;
  line-height: 1; transition: var(--transition); font-style: normal;
}
.hint:hover { background: rgba(59,130,246,0.2); border-color: rgba(59,130,246,0.4); color: var(--primary); }
.hint::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  background: #1e293b; color: var(--text-primary);
  padding: 8px 12px; border-radius: var(--radius-sm);
  font-size: 0.78rem; font-weight: 400; line-height: 1.4;
  white-space: normal; width: max-content; max-width: 220px;
  pointer-events: none; opacity: 0; visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s; z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4); border: 1px solid var(--border-color-hover);
  text-transform: none; letter-spacing: normal;
}
.hint::before {
  content: ''; position: absolute; bottom: calc(100% + 2px); left: 50%; transform: translateX(-50%);
  border: 5px solid transparent; border-top-color: #1e293b;
  pointer-events: none; opacity: 0; visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s; z-index: 1001;
}
.hint:hover::after, .hint:hover::before { opacity: 1; visibility: visible; }

/* Responsive */
@media (max-width: 1200px) {
  .main-content { grid-template-columns: 1fr; }
  .control-panel { max-height: none; }
}
@media (max-width: 768px) {
  body { padding: 10px; }
  .header h1 { font-size: 1.6rem; }
  #simulationCanvas { height: auto; aspect-ratio: 1 / 1; max-height: 60vh; }
  .canvas-container { padding: 10px; }
  .charts-row { grid-template-columns: 1fr; }
  .chart-card canvas { height: 150px; }
  .chart-card h3 { font-size: 0.75rem; margin-bottom: 8px; }
  .chart-card { padding: 12px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-label { font-size: 0.65rem; }
  .stat-value { font-size: 1.05rem; }
  .btn { padding: 9px 16px; font-size: 0.85rem; }
  .control-panel { padding: 16px; }
  .generation-history { padding: 14px; margin-top: 20px; }
  .progress-bar-container { height: 20px; }
  .progress-label { font-size: 0.65rem; }
  .orb-1 { width: 300px; height: 300px; }
  .orb-2 { width: 250px; height: 250px; }
}
@media (max-width: 480px) {
  body { padding: 6px; }
  .header h1 { font-size: 1.3rem; }
  .header p { font-size: 0.85rem; }
  #simulationCanvas { height: auto; aspect-ratio: 1 / 1; max-height: 55vh; }
  .canvas-container { padding: 8px; }
  .chart-card canvas { height: 130px; }
  .stats-grid { grid-template-columns: 1fr; }
  .stat-item { flex-direction: row; justify-content: space-between; align-items: center; padding: 8px; }
  .stat-label { margin-bottom: 0; font-size: 0.6rem; }
  .stat-value { font-size: 1rem; }
  .panel-section h2 { font-size: 1rem; }
  .control-group label { font-size: 0.82rem; }
  .btn { padding: 8px 14px; font-size: 0.8rem; margin-right: 6px; }
  .progress-bar-container { height: 18px; }
  .progress-label { font-size: 0.6rem; }
  .orb-1, .orb-2 { width: 200px; height: 200px; }
  .generation-history { padding: 10px; margin-top: 14px; }
  .generation-history h2 { font-size: 1.1rem; margin-bottom: 10px; }
  #generationTable th, #generationTable td { padding: 6px 5px; font-size: 0.65rem; }
  #generationTable th { font-size: 0.6rem; }
}

@media print {
  .orb, .loading-overlay, .control-panel, .btn { display: none !important; }
  .main-content { grid-template-columns: 1fr; }
}
