/* ==========================================================================
   CAS · RL Series — Lecture 03: Bellman Equations
   bellman.css : specialized interactive components, matrix displays,
   backup trees, contraction plots, and gridworld styling.
   ========================================================================== */

html:not(.light-theme) {
  --bg0: #070d19;
  --bg1: #0e172a;
  --panel: #101b30;
  --panel-2: #16233b;
  --panel-solid: #101b30;
  --line: rgba(148, 163, 184, 0.2);
  --line-2: rgba(148, 163, 184, 0.34);
  --text: #f1f5f9;
  --text-2: #cbd5e1;
  --text-3: #94a3b8;
  --teal: #5eead4;
  --sky: #38bdf8;
  --indigo: #818cf8;
  --violet: #c4b5fd;
  --amber: #fbbf24;
  --rose: #fb7185;
  --green: #34d399;
}

html.light-theme {
  --bg0: #f8fafc;
  --bg1: #f1f5f9;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --panel-solid: #ffffff;
  --line: rgba(15, 23, 42, 0.11);
  --line-2: rgba(15, 23, 42, 0.2);
  --text: #0f172a;
  --text-2: #334155;
  --text-3: #64748b;
  --teal: #0d9488;
  --sky: #0284c7;
  --indigo: #4f46e5;
  --violet: #7c3aed;
  --amber: #d97706;
  --rose: #e11d48;
  --green: #059669;
}

html.light-theme body {
  background-color: #f8fafc;
  color: #0f172a;
  background-image:
    radial-gradient(900px 500px at 15% 4%, rgba(13, 148, 136, 0.065), transparent 65%),
    radial-gradient(950px 550px at 88% 10%, rgba(2, 132, 199, 0.065), transparent 65%);
}

html.light-theme .topnav { background: rgba(255, 255, 255, 0.94); border-bottom-color: rgba(15, 23, 42, 0.1); }
html.light-theme .rail { background: rgba(255, 255, 255, 0.98); border-right-color: rgba(15, 23, 42, 0.12); }
html.light-theme .toc-panel { background: #ffffff; color: #0f172a; }
html.light-theme .btn { background: #ffffff; border-color: rgba(15, 23, 42, 0.22); color: #0f172a; }
html.light-theme .btn:hover { background: #f0fdfa; border-color: #0d9488; color: #0f766e; }
html.light-theme .btn.btn-teal { background: rgba(13, 148, 136, 0.12); border-color: #0d9488; color: #0f766e; font-weight: 700; }
html.light-theme .btn.btn-teal:hover { background: #0d9488; color: #ffffff; }
html.light-theme .fact { background: #f8fafc; border-color: rgba(15, 23, 42, 0.14); color: #1e293b; }
html.light-theme .tok-card { box-shadow: 0 10px 28px rgba(15, 23, 42, 0.05); }
html:not(.light-theme) .tok-card { box-shadow: 0 14px 36px rgba(0, 0, 0, 0.35); }

/* Mutual comparison between V(s) and Q(s,a) */
.vq-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1.15rem;
  margin: 1.1rem 0;
}

.vq-card {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  padding: 1.25rem 1.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  transition: transform 0.16s ease, border-color 0.16s ease;
}

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

.vq-card.is-v { border-left: 4px solid var(--teal); }
.vq-card.is-q { border-left: 4px solid var(--indigo); }

.vq-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.vq-title {
  font-family: var(--font-display);
  font-size: 1.18rem;
  font-weight: 700;
  margin: 0;
}

.vq-eq {
  background: rgba(15, 23, 42, 0.04);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.65rem 0.85rem;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  color: var(--text);
  overflow-x: auto;
}

.light-theme .vq-eq {
  background: #f1f5f9;
}

/* Backup diagram visual studio */
.tree-studio {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}

.tree-card {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.tree-svg-wrap {
  background: var(--bg0);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  overflow: hidden;
}

.light-theme .tree-svg-wrap {
  background: #f8fafc;
}

/* Linear system matrix solver */
.matrix-layout {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
  margin: 1rem 0;
}

.matrix-card {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  padding: 1rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.matrix-card-title {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-2);
  margin: 0;
}

.matrix-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 0.88rem;
  margin-top: 0.35rem;
}

.matrix-table td {
  padding: 0.45rem 0.5rem;
  text-align: right;
  border-bottom: 1px solid var(--line);
}

.matrix-table tr:last-child td {
  border-bottom: 0;
}

.matrix-table td.state-name {
  text-align: left;
  color: var(--text-2);
  font-weight: 600;
  width: 32%;
}

/* Contraction mapping visualizer */
.contraction-studio {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}

.error-bar-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-top: 0.75rem;
}

.error-bar-row {
  display: grid;
  grid-template-columns: 80px minmax(0, 1fr) 75px;
  align-items: center;
  gap: 0.65rem;
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.error-bar-track {
  height: 12px;
  background: var(--panel-2);
  border-radius: 6px;
  overflow: hidden;
  position: relative;
}

.error-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--teal), var(--indigo));
  border-radius: 6px;
  transition: width 0.25s ease;
}

/* 3x3 Gridworld Value Iteration Visualizer */
.gw-studio {
  display: grid;
  grid-template-columns: minmax(0, 360px) minmax(0, 1fr);
  gap: 1.5rem;
  align-items: start;
}

.gw-board {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(3, 100px);
  gap: 8px;
  background: var(--bg0);
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  justify-content: center;
}

.light-theme .gw-board {
  background: #f1f5f9;
}

.gw-tile {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 8px;
  position: relative;
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.gw-tile.is-goal {
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.12);
}

.gw-tile.is-mud {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.12);
}

.gw-coord {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-3);
  display: flex;
  justify-content: space-between;
}

.gw-vval {
  font-family: var(--font-mono);
  font-size: 1.12rem;
  font-weight: 700;
  text-align: center;
  margin: auto 0;
  color: var(--text);
}

.gw-policy-arrow {
  font-size: 1.25rem;
  color: var(--teal);
  text-align: center;
  font-weight: 700;
}

/* Responsive breakdowns */
@media (max-width: 992px) {
  .vq-grid,
  .tree-studio,
  .matrix-layout,
  .contraction-studio,
  .gw-studio {
    grid-template-columns: 1fr;
  }
}
