:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --border: #e5e5e7;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --accent: #0071e3;
  --danger: #ff3b30;
  --radius: 20px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.04);

  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
}

/* =========================================================
   Top Bar
========================================================= */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  padding: clamp(14px, 3vw, 28px) clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--border);
  background: white;
  gap: 16px;
}

h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
}

.sub {
  margin: 6px 0 0;
  font-size: 14px;
  color: var(--muted);
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
}

.flag {
  padding: 4px 8px;
  border-radius: 4px;
  font-family: monospace;
  font-weight: bold;
  font-size: 13px;
}

.flag-on {
  background: var(--accent);
  color: white;
}

.flag-off {
  background: var(--muted);
  color: white;
  opacity: 0.6;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--muted);
}

/* =========================================================
   Layout
========================================================= */

.grid {
  padding: clamp(16px, 4vw, 40px);
  display: grid;
  gap: clamp(12px, 2vw, 28px);
}


/* Desktop: 3 columns */
@media (min-width: 1201px) {
  .grid {
    grid-template-columns: 1.2fr 1fr 0.9fr;
  }
}

/* Tablet/Small Desktop: 2 columns */
@media (max-width: 1200px) and (min-width: 851px) {
  .grid {
    grid-template-columns: 1.2fr 1fr;
  }
}

/* Mobile/Vertical Tablet: 1 column (Ensures NES is full width) */
@media (max-width: 850px) {
  .grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
}

/* =========================================================
   Cards
========================================================= */

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}

.card.wide {
  grid-column: 1 / -1;
}

@media (max-width: 420px) {
  .card {
    padding: 16px;
    border-radius: 16px;
  }
}

h2 {
  margin: 0 0 18px;
  font-size: 16px;
  font-weight: 600;
}

/* =========================================================
   Textarea
========================================================= */

textarea {
  width: 100%;
  height: 220px;
  resize: vertical;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: #fafafa;
  padding: 14px;
  font-family: var(--mono);
  font-size: 13px;
}

textarea:focus {
  border-color: var(--accent);
  outline: none;
}

/* =========================================================
   Buttons
========================================================= */

button {
  border: 1px solid var(--border);
  background: #f2f2f2;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.05s ease;
}

button:hover {
  background: #e8e8ed;
}

button:active {
  transform: scale(0.98);
}

button.primary {
  background: var(--accent);
  color: white;
  border: none;
}

.mode-switch button.active {
  background: var(--accent);
  color: white;
  border: none;
  box-shadow: 0 6px 18px rgba(0, 113, 227, 0.25);
}

/* Button rows */

.row,
.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

@media (max-width: 420px) {
  .row,
  .controls {
    grid-template-columns: 1fr;
  }

  button {
    width: 100%;
  }
}

/* =========================================================
   Register Grid
========================================================= */

.kv {
  display: grid;
  grid-template-columns: 90px 1fr;
  gap: 10px 18px;
  font-family: var(--mono);
  font-size: 13px;
}

/* Shared highlight base */
.kv .highlight-pc,
.kv .highlight-addr {
  font-weight: 600;
  transition: color 0.15s ease;
}

/* 🔵 PC */
.kv .highlight-pc {
  color: var(--accent); /* same blue family */
}

/* 🟡 Effective address */
.kv .highlight-addr {
  color: #b27c00;
}

.k { color: var(--muted); }
.v { font-weight: 500; }

/* =========================================================
   Log
========================================================= */

.log {
  margin-top: 16px;
  height: 160px;
  overflow: auto;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: #fafafa;
  padding: 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}

/* =========================================================
   RAM + Learn Panel
========================================================= */

.ram-wrap {
  display: flex;
  flex-direction: column; /* Stack by default */
  gap: 16px;
}

@media (min-width: 1100px) {
  .ram-wrap {
    flex-direction: row; /* Side-by-side only on large screens */
    align-items: start;
  }
}

.ram-scroll {
  overflow-x: auto;
  padding-bottom: 10px;
  width: 100%; /* Ensure it stays within its parent */
}

.ram {
  display: grid;
  grid-template-columns: 56px repeat(16, 28px);
  gap: 4px;
  font-family: var(--mono);
  font-size: 11px;
  width: max-content;
  min-width: 620px;
}

.ram .cell {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 4px 0;
  text-align: center;
  background: #fafafa;
}

.ram .cell.pc-highlight {
  background: rgba(0, 113, 227, 0.18);
  border-color: var(--accent);
}

.ram .cell.addr-highlight {
  background: rgba(178, 124, 0, 0.18);
  border-color: rgba(178, 124, 0);
}

/* =========================================================
   NES Screen
========================================================= */

.mode-cpu,
.mode-nes {
  display: none;
}

body[data-mode="nes"] .mode-nes { display: flex; }
body[data-mode="cpu"] .mode-cpu { display: block; }

/* 1. Force 1 column earlier to give NES room */
@media (max-width: 1000px) {
  .grid {
    grid-template-columns: 1fr !important;
    padding: 10px; /* Thinner margin on mobile */
  }
}

/* 2. Remove card styling for NES mode on mobile to maximize size */
@media (max-width: 600px) {
  .card.mode-nes {
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
  }
  
  .tv {
    border-radius: 0; /* Full-bleed look on mobile */
    padding: 5px;
  }
}

/* 3. Ensure the NES container can grow as much as the viewport allows */
.mode-nes {
  width: 100%;
  flex-direction: column;
  align-items: center;
}


.tv {
  width: 100%;
  max-width: 800px;
  padding: 18px;
  border-radius: 18px;

  /* soft light shell */
  background: linear-gradient(145deg, #f4f4f6, #e6e6ea);

  /* gentle depth */
  box-shadow:
    inset 0 2px 4px rgba(255,255,255,0.8),
    inset 0 -6px 12px rgba(0,0,0,0.08),
    0 15px 35px rgba(0,0,0,0.08);

  border: 1px solid rgba(0,0,0,0.06);
}

.power-led {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ff3b3b;
  margin-top: 14px;

  box-shadow:
    0 0 10px #ff3b3b,
    0 0 20px rgba(255,0,0,0.5);
}

#screen {
  display: block;
  width: 100%;
  aspect-ratio: 256 / 240;
  image-rendering: pixelated;

  border-radius: 8px;
  background: #000;
  box-shadow:
    inset 0 0 30px rgba(0,0,0,0.9),
    0 0 20px rgba(0,0,0,0.4);
}
/* allow grid items to shrink instead of forcing overflow */
.grid > * { min-width: 0; }
.card { min-width: 0; }