:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #16213e;
  --surface3: #0d1b2a;
  --accent: #7c3aed;
  --accent2: #a855f7;
  --accent-glow: rgba(124, 58, 237, 0.3);
  --green: #10b981;
  --blue: #3b82f6;
  --orange: #f59e0b;
  --red: #ef4444;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: rgba(255,255,255,0.08);
  --radius: 12px;
  --radius-sm: 8px;
}

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

html { scroll-behavior: smooth; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* HEADER */
header {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  border-bottom: 1px solid var(--border);
  padding: 2.5rem 1.5rem;
  text-align: center;
  position: relative;
  overflow: hidden;
}
header::before {
  content: '';
  position: absolute;
  top: -50%;
  left: 50%;
  transform: translateX(-50%);
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(124,58,237,0.15) 0%, transparent 70%);
  pointer-events: none;
}
header h1 {
  font-size: clamp(2rem, 5vw, 3rem);
  font-weight: 800;
  background: linear-gradient(135deg, #a855f7, #3b82f6, #10b981);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.5rem;
}
.subtitle {
  color: var(--text-muted);
  font-size: 1.1rem;
}

/* MAIN LAYOUT */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

/* THEORY CARD */
.theory-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
}
.theory-card h2 {
  color: var(--accent2);
  font-size: 1.4rem;
  margin-bottom: 1.5rem;
}
.theory-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.25rem;
  margin-bottom: 1.5rem;
}
.theory-item {
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1.25rem;
  transition: transform 0.2s, border-color 0.2s;
}
.theory-item:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}
.theory-icon {
  font-size: 2rem;
  display: block;
  margin-bottom: 0.5rem;
}
.theory-item h3 {
  font-size: 0.95rem;
  color: var(--accent2);
  margin-bottom: 0.5rem;
}
.theory-item p {
  font-size: 0.88rem;
  color: var(--text-muted);
}

.formula-box {
  background: var(--surface3);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.formula-label {
  font-size: 0.85rem;
  color: var(--text-muted);
}
.formula {
  font-family: 'Courier New', monospace;
  color: var(--accent2);
  font-size: 0.95rem;
}

/* UPLOAD */
.upload-area {
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.upload-area:hover, .upload-area.drag-over {
  border-color: var(--accent2);
  background: rgba(124,58,237,0.05);
}
.upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 3rem 2rem;
  text-align: center;
}
.upload-icon { font-size: 3rem; }
.upload-hint { font-size: 0.8rem; color: var(--text-muted); }

/* TOOL SECTION */
.tool-section { animation: fadeIn 0.4s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.tool-grid {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 1.5rem;
  align-items: start;
}

/* CONTROLS PANEL */
.controls-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  position: sticky;
  top: 1rem;
}
.controls-panel h2 {
  font-size: 1.1rem;
  color: var(--accent2);
}
.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.control-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.info-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  font-size: 0.65rem;
  cursor: pointer;
  color: white;
  user-select: none;
}

/* Tip modal */
.tip-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tip-modal-content {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.4rem 1.8rem;
  max-width: 340px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  text-align: center;
}
.tip-modal-content p {
  margin: 0 0 1.1rem;
  color: var(--text);
  font-size: 0.95rem;
  line-height: 1.5;
}

/* PRESETS GRID */
.presets-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
}
.preset-btn {
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.5rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}
.preset-btn:hover {
  border-color: var(--accent);
  background: rgba(124,58,237,0.1);
  transform: scale(1.02);
}
.preset-btn.active {
  border-color: var(--accent2);
  background: rgba(168,85,247,0.15);
  color: var(--accent2);
}

/* SIZE BUTTONS */
.size-select {
  display: flex;
  gap: 0.5rem;
}
.size-btn {
  flex: 1;
  padding: 0.5rem;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.15s;
}
.size-btn.active {
  border-color: var(--accent2);
  background: rgba(168,85,247,0.15);
  color: var(--accent2);
}

/* MATRIX */
.matrix-container {
  display: flex;
  justify-content: center;
  padding: 0.5rem;
}
.matrix-grid {
  display: grid;
  gap: 4px;
}
.matrix-grid input {
  width: 52px;
  height: 52px;
  text-align: center;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: 'Courier New', monospace;
  font-weight: 600;
  transition: border-color 0.15s, background 0.15s;
  -moz-appearance: textfield;
}
.matrix-grid input::-webkit-outer-spin-button,
.matrix-grid input::-webkit-inner-spin-button { -webkit-appearance: none; }
.matrix-grid input:focus {
  outline: none;
  border-color: var(--accent2);
  background: rgba(168,85,247,0.1);
}
.matrix-grid input.highlight {
  background: rgba(168,85,247,0.2);
  border-color: var(--accent2);
}

.matrix-actions {
  display: flex;
  gap: 0.5rem;
}

/* DIVISOR */
.divisor-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.divisor-row input {
  flex: 1;
  padding: 0.5rem 0.75rem;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  -moz-appearance: textfield;
}
.divisor-row input:focus { outline: none; border-color: var(--accent2); }

/* BUTTONS */
.btn {
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent2); }
.btn-apply {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: white;
  font-size: 1rem;
  padding: 0.85rem;
  justify-content: center;
  box-shadow: 0 4px 15px var(--accent-glow);
}
.btn-apply:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px var(--accent-glow);
}
.btn-apply:active { transform: translateY(0); }
.btn-secondary {
  background: var(--surface3);
  border: 1px solid var(--border);
  color: var(--text-muted);
  justify-content: center;
}
.btn-secondary:hover { border-color: var(--text-muted); color: var(--text); }
.btn-small {
  padding: 0.35rem 0.75rem;
  font-size: 0.78rem;
  background: var(--surface3);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.btn-small:hover { border-color: var(--accent); color: var(--text); }

/* CANVAS PANEL */
.canvas-panel {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}
.canvas-wrapper {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  flex-wrap: wrap;
}
.canvas-box {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  align-items: center;
}
.canvas-label {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
canvas {
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  max-width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
}
.arrow-between {
  font-size: 2rem;
  color: var(--accent2);
  padding-top: 2rem;
  flex-shrink: 0;
}

/* EXPLANATION */
.explanation-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent2);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
}
.explanation-box h3 {
  font-size: 0.95rem;
  color: var(--accent2);
  margin-bottom: 0.5rem;
}
.explanation-box p {
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.6;
}

/* MATH DEMO */
.math-demo {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.math-demo h3 { color: var(--green); margin-bottom: 0.5rem; font-size: 1rem; }
.math-intro { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem; }
.math-grid-demo {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.math-mini-grid {
  display: grid;
  gap: 2px;
}
.math-mini-cell {
  width: 44px;
  height: 44px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-family: monospace;
  font-weight: 600;
}
.math-mini-cell.pixel { background: var(--surface3); border: 1px solid var(--border); }
.math-mini-cell.kernel { background: rgba(124,58,237,0.2); border: 1px solid var(--accent); color: var(--accent2); }
.math-mini-cell.center { background: rgba(16,185,129,0.2); border: 1px solid var(--green); color: var(--green); }
.math-label { font-size: 0.75rem; color: var(--text-muted); text-align: center; margin-top: 0.4rem; }
.math-op { font-size: 1.5rem; color: var(--text-muted); }
.math-result {
  background: var(--surface3);
  border: 1px solid var(--green);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  font-family: monospace;
  font-size: 0.85rem;
  color: var(--green);
  white-space: pre-wrap;
  word-break: break-word;
}

/* PROCESSING OVERLAY */
.processing-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}
.processing-box {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 2rem 3rem;
  text-align: center;
}
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent2);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* FOOTER */
footer {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}

/* COLOR FILTERS */
.presets-section-divider {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.25rem 0;
}
.presets-section-divider::before,
.presets-section-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}
.presets-section-divider span {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--orange);
  white-space: nowrap;
}

.color-preset-btn {
  background: var(--surface3);
  border: 1px solid rgba(245,158,11,0.25);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.5rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}
.color-preset-btn:hover {
  border-color: var(--orange);
  background: rgba(245,158,11,0.08);
  transform: scale(1.02);
}
.color-preset-btn.active {
  border-color: var(--orange);
  background: rgba(245,158,11,0.15);
  color: var(--orange);
}

.bias-row {
  display: flex;
  gap: 0.5rem;
}
.bias-input {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  align-items: center;
}
.bias-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}
.bias-label.r { color: #f87171; }
.bias-label.g { color: #4ade80; }
.bias-label.b { color: #60a5fa; }
.bias-input input {
  width: 100%;
  padding: 0.4rem 0.3rem;
  text-align: center;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.85rem;
  -moz-appearance: textfield;
}
.bias-input input:focus { outline: none; border-color: var(--orange); }

/* RESPONSIVE */
@media (max-width: 900px) {
  .tool-grid {
    grid-template-columns: 1fr;
  }
  .controls-panel {
    position: static;
  }
}
@media (max-width: 600px) {
  .canvas-wrapper {
    flex-direction: column;
    align-items: center;
  }
  .arrow-between {
    transform: rotate(90deg);
    padding: 0;
  }
  .presets-grid {
    grid-template-columns: 1fr 1fr;
  }
}
