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

:root {
  --bg:             #f1f5f9;
  --card:           #ffffff;
  --border:         #e2e8f0;
  --text:           #1e293b;
  --text-muted:     #64748b;
  --primary:        #4f46e5;
  --primary-light:  #eef2ff;

  --col-entering:       #dbeafe;
  --col-entering-head:  #3b82f6;
  --col-leaving:        #fef3c7;
  --col-leaving-head:   #d97706;
  --col-pivot:          #ef4444;
  --col-min-ratio:      #dcfce7;
  --col-min-ratio-txt:  #16a34a;
  --col-optimal:        #d1fae5;

  --radius:    12px;
  --radius-sm: 6px;
  --shadow:    0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.10);
}

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

/* ===== HEADER ===== */
.site-header {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  padding: 2.5rem 1rem 2rem;
  text-align: center;
}

.site-header h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.site-header p {
  opacity: 0.82;
  margin-top: 0.3rem;
  font-size: 1rem;
}

/* ===== CONTAINER ===== */
.container {
  max-width: 920px;
  margin: 0 auto;
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ===== CARD ===== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.card-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

/* ===== BUTTONS ===== */
.btn-primary, .btn-secondary, .btn-solve, .nav-btn {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  border-radius: var(--radius-sm);
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.btn-primary {
  background: var(--primary);
  color: white;
  padding: 0.55rem 1.2rem;
  font-size: 0.9rem;
}
.btn-primary:hover { background: #4338ca; }

.btn-secondary {
  background: var(--primary-light);
  color: var(--primary);
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
}
.btn-secondary:hover { background: #e0e7ff; }

.btn-solve {
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: white;
  padding: 0.9rem 2.8rem;
  font-size: 1.05rem;
  border-radius: 8px;
  box-shadow: 0 4px 18px rgba(79,70,229,.35);
  letter-spacing: 0.3px;
}
.btn-solve:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(79,70,229,.45);
}
.btn-solve:active { transform: translateY(0); }

.nav-btn {
  background: #f1f5f9;
  color: var(--text);
  padding: 0.55rem 1.2rem;
  font-size: 0.9rem;
  border: 1px solid var(--border);
}
.nav-btn:hover:not(:disabled) { background: #e2e8f0; }
.nav-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ===== PROBLEM DISPLAY ===== */
.problem-display {
  padding: 1.2rem 1.5rem;
}

.problem-obj {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 0.65rem;
}

.problem-sa {
  color: var(--text-muted);
  font-size: 0.88rem;
  margin-bottom: 0.3rem;
}

.problem-constraint {
  font-family: 'Courier New', monospace;
  font-size: 0.92rem;
  padding: 0.2rem 0;
}

.problem-constraint.nonneg {
  margin-top: 0.4rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* ===== EDITOR ===== */
.problem-editor {
  padding: 1.25rem 1.5rem;
  border-top: 1px solid var(--border);
  background: #fafbfd;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}

.problem-editor.hidden { display: none; }

.editor-row {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.editor-row label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.88rem;
  font-weight: 500;
}

.editor-section strong {
  display: block;
  font-size: 0.88rem;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
}

.small-input {
  width: 60px;
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.88rem;
  text-align: center;
}

.coeff-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.coeff-group {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.88rem;
}

.coeff-group input {
  width: 64px;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.88rem;
  text-align: center;
  background: white;
}
.coeff-group input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.coeff-group .var-label {
  font-family: 'Courier New', monospace;
  color: var(--text-muted);
  font-size: 0.85rem;
  min-width: 22px;
}

.coeff-plus {
  color: var(--text-muted);
  font-size: 0.88rem;
  margin: 0 2px;
}

.constraint-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
  flex-wrap: wrap;
}

.constraint-row .row-label {
  color: var(--text-muted);
  font-size: 0.8rem;
  width: 28px;
  text-align: right;
  flex-shrink: 0;
}

.leq-sign {
  color: var(--text-muted);
  font-size: 0.88rem;
  margin: 0 4px;
}

/* ===== SOLVE BAR ===== */
.solve-bar {
  display: flex;
  justify-content: center;
}

/* ===== STEP NAVIGATION ===== */
.step-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: #fafbfd;
}

.step-indicator {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#step-label {
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--primary);
}

.step-dots {
  display: flex;
  gap: 5px;
  justify-content: center;
  flex-wrap: wrap;
  max-width: 300px;
}

.step-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--border);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}
.step-dot:hover { transform: scale(1.3); }
.step-dot.active  { background: var(--primary); transform: scale(1.2); }
.step-dot.visited { background: #a5b4fc; }

/* ===== LEGEND ===== */
.legend {
  display: flex;
  gap: 0.75rem;
  padding: 0.65rem 1.5rem;
  flex-wrap: wrap;
  align-items: center;
  border-bottom: 1px solid var(--border);
  background: #fafbfd;
}

.legend-item {
  font-size: 0.75rem;
  font-weight: 500;
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
}
.legend-item.entering  { background: var(--col-entering); color: #1d4ed8; }
.legend-item.leaving   { background: var(--col-leaving);  color: #92400e; }
.legend-item.pivot     { background: var(--col-pivot); color: white; border-radius: 4px; }
.legend-item.min-ratio { background: var(--col-min-ratio); color: var(--col-min-ratio-txt); }

.legend-hint {
  margin-left: auto;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* ===== EXPLANATION BOX ===== */
#explanation-box {
  margin: 1.25rem 1.5rem 0.5rem;
}

.explanation-card {
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid transparent;
}

.explanation-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.85rem 1.2rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.phase-icon { font-size: 1.2rem; }

.explanation-body {
  padding: 0.8rem 1.2rem 1rem;
}

.explanation-body ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.explanation-body li {
  font-size: 0.88rem;
  padding-left: 1.4rem;
  position: relative;
  line-height: 1.5;
}

.explanation-body li::before {
  content: '\2192';
  position: absolute;
  left: 0;
  opacity: 0.5;
}

/* Phase color schemes */
.phase-initial .explanation-card  { border-color: #c7d2fe; }
.phase-initial .explanation-head  { background: #eef2ff; color: #4338ca; }
.phase-initial .explanation-body  { background: #f8f9ff; color: #3730a3; }

.phase-entering .explanation-card { border-color: #bfdbfe; }
.phase-entering .explanation-head { background: #dbeafe; color: #1d4ed8; }
.phase-entering .explanation-body { background: #f0f7ff; color: #1e3a5f; }

.phase-ratios .explanation-card   { border-color: #fde68a; }
.phase-ratios .explanation-head   { background: #fef3c7; color: #92400e; }
.phase-ratios .explanation-body   { background: #fffdf0; color: #78350f; }

.phase-pivot_result .explanation-card { border-color: #a7f3d0; }
.phase-pivot_result .explanation-head { background: #d1fae5; color: #065f46; }
.phase-pivot_result .explanation-body { background: #f0fdf9; color: #064e3b; }

.phase-optimal .explanation-card  { border-color: #6ee7b7; }
.phase-optimal .explanation-head  { background: #d1fae5; color: #065f46; }
.phase-optimal .explanation-body  { background: #f0fdf9; color: #064e3b; }

.phase-unbounded .explanation-card { border-color: #fca5a5; }
.phase-unbounded .explanation-head { background: #fee2e2; color: #991b1b; }
.phase-unbounded .explanation-body { background: #fff5f5; color: #7f1d1d; }

/* Solution summary inside optimal explanation */
.solution-vars {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.85rem;
  padding-top: 0.75rem;
  border-top: 1px solid #a7f3d0;
}

.solution-var {
  background: white;
  border: 1px solid #a7f3d0;
  border-radius: 6px;
  padding: 0.35rem 0.85rem;
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
  color: #065f46;
  font-weight: 600;
}

.solution-z {
  background: #065f46;
  color: white;
  border-color: #065f46;
  font-size: 0.95rem;
  padding: 0.4rem 1rem;
}

/* ===== TABLEAU ===== */
.tableau-wrapper {
  padding: 0.5rem 1.5rem 1.5rem;
  overflow-x: auto;
}

.simplex-table {
  border-collapse: collapse;
  font-family: 'Courier New', monospace;
  font-size: 0.88rem;
  width: 100%;
  min-width: 360px;
}

.simplex-table th,
.simplex-table td {
  padding: 0.48rem 0.75rem;
  text-align: center;
  border: 1px solid var(--border);
  white-space: nowrap;
}

/* Header row */
.simplex-table thead th {
  background: #f8fafc;
  font-weight: 600;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.simplex-table thead th.entering-header {
  background: var(--col-entering-head);
  color: white;
}

.simplex-table thead th.ratio-header {
  background: #f0fdf4;
  color: var(--col-min-ratio-txt);
}

/* Base column */
.simplex-table .base-cell {
  background: #f8fafc;
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

/* Z row */
.simplex-table .z-row td {
  background: #f8f7ff;
}
.simplex-table .z-row .base-cell {
  color: var(--primary);
  font-weight: 700;
  background: #eef2ff;
}

/* Entering column (non-pivot) */
.simplex-table td.entering-cell {
  background: var(--col-entering);
  color: #1d4ed8;
  font-weight: 600;
}

/* Leaving row */
.simplex-table tr.leaving-row td {
  background: var(--col-leaving);
}
.simplex-table tr.leaving-row .base-cell {
  background: #fde68a;
  color: #92400e;
  font-weight: 700;
}

/* Pivot cell (intersection) */
.simplex-table td.pivot-cell {
  background: var(--col-pivot) !important;
  color: white !important;
  font-weight: 700;
  position: relative;
}
.simplex-table td.pivot-cell::after {
  content: '';
  position: absolute;
  inset: 1px;
  border: 2px solid rgba(255,255,255,.45);
  border-radius: 2px;
  pointer-events: none;
}

/* After-pivot: show where pivot was (softer) */
.simplex-table td.pivoted-col { background: #eff6ff; }
.simplex-table tr.pivoted-row td { background: #f0fdf4; }
.simplex-table tr.pivoted-row .base-cell { background: #bbf7d0; color: #065f46; font-weight: 700; }

/* Ratio cells */
.simplex-table td.ratio-cell {
  font-size: 0.78rem;
  color: var(--text-muted);
}
.simplex-table td.min-ratio {
  background: var(--col-min-ratio);
  color: var(--col-min-ratio-txt);
  font-weight: 700;
}
.simplex-table td.no-ratio {
  color: #cbd5e1;
}

/* Optimal table */
.simplex-table.optimal-table .base-cell { background: #d1fae5; color: #065f46; }
.simplex-table.optimal-table .z-row .base-cell { background: #065f46; color: white; }
.simplex-table.optimal-table .z-row td { background: #f0fdf9; }
.simplex-table.optimal-table td.rhs-optimal { background: var(--col-optimal); color: #065f46; font-weight: 700; }

/* Step fade-in animation */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.step-animate {
  animation: fadeUp 0.22s ease forwards;
}

/* ===== FORMULA BOX ===== */
.formula-box {
  background: rgba(255,255,255,.55);
  border: 1px solid currentColor;
  border-radius: 5px;
  padding: 0.45rem 0.8rem;
  margin-top: 0.65rem;
  font-family: 'Courier New', monospace;
  font-size: 0.82rem;
  opacity: 0.85;
  display: inline-block;
}

/* ===== HELP BUTTON ===== */
.site-header {
  position: relative;
}

.help-btn {
  position: absolute;
  top: 50%;
  right: 1.5rem;
  transform: translateY(-50%);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.55);
  background: rgba(255,255,255,.15);
  color: white;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.help-btn:hover {
  background: rgba(255,255,255,.30);
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(3px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  animation: fadeOverlay 0.18s ease;
}

@keyframes fadeOverlay {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal-box {
  background: white;
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: slideModal 0.2s ease;
}

@keyframes slideModal {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)   scale(1);    }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.4rem;
  border-bottom: 1px solid var(--border);
  background: var(--primary-light);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary);
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.4rem;
  color: var(--text-muted);
  line-height: 1;
  padding: 0 0.2rem;
  font-family: inherit;
  transition: color 0.1s;
}
.modal-close:hover { color: var(--text); }

.modal-body {
  overflow-y: auto;
  padding: 1.2rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

.modal-section h3 {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.modal-section p {
  font-size: 0.88rem;
  color: var(--text);
  line-height: 1.55;
}

.modal-section ol {
  padding-left: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.modal-section ol li {
  font-size: 0.88rem;
  color: var(--text);
  line-height: 1.5;
}

.modal-note {
  margin-top: 0.5rem;
  font-size: 0.8rem !important;
  color: var(--text-muted) !important;
  font-style: italic;
}

/* Problem block inside modal */
.modal-problem {
  background: var(--primary-light);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  margin: 0.6rem 0;
  font-family: 'Courier New', monospace;
  font-size: 0.88rem;
}

.modal-problem-obj {
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 0.3rem;
}

.modal-problem-sa {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
}

.modal-problem-constraints {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  color: var(--text);
}

/* Table inside modal */
.modal-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.83rem;
  margin-top: 0.5rem;
}

.modal-table th,
.modal-table td {
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  text-align: left;
}

.modal-table thead th {
  background: #f8fafc;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

.modal-table code {
  background: #eef2ff;
  color: var(--primary);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
}

.modal-result {
  margin-top: 0.6rem;
  font-size: 0.85rem !important;
  color: #065f46 !important;
  background: var(--col-optimal);
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
}

/* Keyboard shortcuts */
.modal-keys {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.modal-key-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--text);
}

kbd {
  display: inline-block;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0.15rem 0.5rem;
  font-family: inherit;
  font-size: 0.82rem;
  color: var(--text);
}

.key-btn {
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  padding: 0.15rem 0.6rem;
  font-size: 0.82rem;
}

.key-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
  flex-shrink: 0;
}

/* Colors legend inside modal */
.modal-colors {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.modal-color {
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.25rem 0.7rem;
  border-radius: 4px;
}
.modal-color.entering  { background: var(--col-entering); color: #1d4ed8; }
.modal-color.leaving   { background: var(--col-leaving);  color: #92400e; }
.modal-color.pivot     { background: var(--col-pivot);    color: white;   }
.modal-color.min-ratio { background: var(--col-min-ratio); color: var(--col-min-ratio-txt); }

/* ===== RESPONSIVE ===== */
@media (max-width: 600px) {
  .site-header h1 { font-size: 1.5rem; }
  .container { padding: 1rem 0.5rem; }
  .card-header { padding: 0.9rem 1rem; }
  .step-nav { padding: 0.75rem 0.9rem; }
  .legend { padding: 0.5rem 0.9rem; gap: 0.4rem; }
  .legend-hint { display: none; }
  .tableau-wrapper { padding: 0.5rem 0.75rem 1rem; }
  #explanation-box { margin: 1rem 0.75rem 0.5rem; }
  .problem-display { padding: 1rem; }
}
