/* nanoLADA Simulator — style.css */
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;600&family=Source+Code+Pro:wght@400;600&display=swap');

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

:root {
  --crimson:  #9B1C1C;
  --gold:     #C8A44A;
  --dark:     #1A1A2E;
  --mid:      #2D3561;
  --bg:       #F0EDE8;
  --panel:    #FFFFFF;
  --border:   #D8D4CC;
  --text:     #2C2C2C;
  --muted:    #7A7570;
  --changed:  #FFF3CD;
  --error-bg: #FEE2E2;
  --error:    #991B1B;
  --mono:     'Source Code Pro', 'Courier New', monospace;
  --radius:   8px;
}

html, body { height: 100%; }

body {
  font-family: 'Sarabun', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ── Top bar ─────────────────────────────────────────────────────────── */
.topbar {
  background: var(--dark);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .5rem 1rem;
  flex-shrink: 0;
  gap: 1rem;
  flex-wrap: wrap;
}
.topbar-left { display: flex; align-items: baseline; gap: .5rem; }
.logo        { font-family: var(--mono); font-size: 1.1rem; font-weight: 600; color: var(--gold); }
.logo-sub    { font-size: .75rem; opacity: .65; }
.topbar-right { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.book-ref    { font-size: .72rem; opacity: .6; }
.back-link   { font-size: .75rem; color: var(--gold); text-decoration: none; white-space: nowrap; }
.back-link:hover { text-decoration: underline; }

/* ── Workspace layout ────────────────────────────────────────────────── */
.workspace {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: .75rem;
  padding: .75rem;
  flex: 1;
  min-height: 0;
  align-items: start;
}

/* ── Panel ───────────────────────────────────────────────────────────── */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.panel-header {
  background: var(--dark);
  color: rgba(255,255,255,.9);
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  padding: .35rem .75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.panel-header select {
  font-size: .7rem;
  padding: .15rem .3rem;
  border-radius: 4px;
  border: none;
  background: rgba(255,255,255,.12);
  color: #fff;
  cursor: pointer;
}
.panel-header select option { background: var(--dark); }

/* ── Editor ──────────────────────────────────────────────────────────── */
.editor-wrap {
  display: flex;
  flex: 1;
  min-height: 300px;
  overflow: hidden;
  position: relative;
}
.line-nums {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  padding: .5rem .4rem .5rem .5rem;
  background: #F5F2ED;
  color: var(--muted);
  text-align: right;
  user-select: none;
  min-width: 2.4rem;
  overflow: hidden;
  white-space: pre;
  border-right: 1px solid var(--border);
  position: relative;
  z-index: 3;        /* sits above the highlight bar */
}
/* ── Current-instruction highlight bar ──────────────────────────────── */
.line-highlight {
  position: absolute;
  /* left is set dynamically by JS (= editor.offsetLeft) */
  right: 0;
  height: 19.2px;   /* 12px font × 1.6 line-height — must match .editor */
  background: rgba(251, 191, 36, .18);
  border-left: 3px solid rgba(245, 158, 11, .85);
  pointer-events: none;
  z-index: 1;
  display: none;
  transition: top .08s ease;
}
.editor {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  padding: .5rem .6rem;
  border: none;
  outline: none;
  resize: none;
  flex: 1;
  background: transparent;  /* lets .line-highlight show through */
  color: var(--text);
  tab-size: 4;
  white-space: pre;
  overflow: auto;
  position: relative;
  z-index: 2;
}
/* editor-wrap provides the background that was on .editor */
.editor-wrap { background: #FAFAF8; }

/* ── Error bar ───────────────────────────────────────────────────────── */
.error-bar {
  background: var(--error-bg);
  color: var(--error);
  font-size: .72rem;
  padding: .35rem .75rem;
  border-top: 1px solid #FCA5A5;
}
.hidden { display: none !important; }

/* ── Controls ────────────────────────────────────────────────────────── */
.controls {
  display: flex;
  gap: .4rem;
  padding: .5rem .6rem;
  border-top: 1px solid var(--border);
  align-items: center;
  flex-wrap: wrap;
}
.btn {
  font-family: 'Sarabun', sans-serif;
  font-size: .78rem;
  font-weight: 600;
  padding: .3rem .8rem;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
  white-space: nowrap;
}
.btn:disabled { opacity: .38; cursor: not-allowed; }
.btn:not(:disabled):hover { background: #EEE; }
.btn-primary {
  background: var(--crimson);
  color: #fff;
  border-color: var(--crimson);
}
.btn-primary:not(:disabled):hover { background: #7a1515; border-color: #7a1515; }
.btn-danger { border-color: #ECA; color: #8B4; }
.btn-danger:not(:disabled):hover { background: #FFF9ED; }
.btn-link {
  background: none;
  border: none;
  color: var(--crimson);
  font-size: .7rem;
  cursor: pointer;
  padding: 0;
}
.btn-link:hover { text-decoration: underline; }

.cycle-badge {
  margin-left: auto;
  font-size: .72rem;
  color: var(--muted);
  background: #F0EDE8;
  padding: .2rem .6rem;
  border-radius: 12px;
  border: 1px solid var(--border);
}

/* ── Export ──────────────────────────────────────────────────────────── */
.export-section {
  border-top: 1px solid var(--border);
  font-size: .78rem;
}
.export-section summary {
  padding: .45rem .75rem;
  cursor: pointer;
  user-select: none;
  background: #F5F2ED;
  color: var(--mid);
  font-weight: 600;
}
.export-section summary:hover { background: #EDE9E0; }
.export-body { padding: .6rem .75rem .75rem; }
.export-hint { font-size: .72rem; color: var(--muted); margin-bottom: .5rem; }
.export-hint code { font-family: var(--mono); background: #EEE; padding: 0 .25rem; border-radius: 3px; }
.export-files { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.export-files label {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .72rem; font-weight: 600; margin-bottom: .2rem; color: var(--mid);
}
.export-ta {
  font-family: var(--mono);
  font-size: .65rem;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .3rem .4rem;
  background: #F8F8F6;
  resize: none;
  color: var(--text);
}
.btn-copy {
  font-size: .65rem;
  padding: .1rem .4rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #EEE;
  cursor: pointer;
}
.btn-copy:hover { background: var(--crimson); color: #fff; border-color: var(--crimson); }
.export-cmd {
  margin-top: .6rem;
  background: var(--dark);
  color: #A8E6A8;
  font-family: var(--mono);
  font-size: .62rem;
  padding: .4rem .6rem;
  border-radius: 4px;
  overflow-x: auto;
  white-space: nowrap;
}

/* ── Register grid ───────────────────────────────────────────────────── */
.reg-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: .4rem;
  background: #F5F2ED;
}
.reg-cell {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .2rem .3rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background .2s;
  min-width: 0;
}
.reg-cell.changed { background: var(--changed); border-color: #D4A017; }
.reg-name {
  font-family: var(--mono);
  font-size: .6rem;
  color: var(--muted);
  font-weight: 600;
}
.reg-val {
  font-family: var(--mono);
  font-size: .65rem;
  color: var(--text);
  font-weight: 600;
  word-break: break-all;
}

.hex-toggle {
  display: flex;
  align-items: center;
  gap: .3rem;
  font-size: .68rem;
  cursor: pointer;
  color: rgba(255,255,255,.75);
}

/* ── Memory view ─────────────────────────────────────────────────────── */
.mem-hint { font-size: .62rem; opacity: .6; }
.mem-view {
  padding: .4rem;
  max-height: 120px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: .65rem;
  background: #FAFAF8;
}
.mem-row { display: flex; gap: .5rem; }
.mem-addr { color: var(--mid); min-width: 3.5rem; }
.mem-val  { color: var(--text); }

/* ── Execution log ───────────────────────────────────────────────────── */
.exec-log {
  padding: .4rem;
  max-height: 260px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: .65rem;
  background: #FAFAF8;
}
.log-row {
  display: grid;
  grid-template-columns: 2.2rem 3.5rem 1fr 1fr;
  gap: .3rem;
  padding: .15rem .2rem;
  border-bottom: 1px solid #EDEBE6;
  align-items: start;
}
.log-row:hover { background: #F5F2ED; }
.log-row.log-halted { background: #FEF3C7; }
.log-cycle  { color: var(--muted); text-align: right; }
.log-pc     { color: var(--mid); }
.log-asm    { color: var(--crimson); font-weight: 600; }
.log-effect { color: #2D6A4F; }

.empty-msg { color: var(--muted); font-style: italic; font-size: .72rem; padding: .25rem; display: block; }

/* ── Mode tabs ───────────────────────────────────────────────────────── */
.mode-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  background: #F5F2ED;
  flex-shrink: 0;
}
.mode-tab {
  font-family: 'Sarabun', sans-serif;
  font-size: .76rem;
  font-weight: 600;
  padding: .4rem .9rem;
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .12s, border-color .12s;
  white-space: nowrap;
}
.mode-tab:hover { color: var(--text); }
.mode-tab.active {
  color: var(--crimson);
  border-bottom-color: var(--crimson);
  background: var(--panel);
}

/* ── Stage badge (multi-cycle) ───────────────────────────────────────── */
.stage-badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: .72rem;
  font-weight: 700;
  padding: .15rem .5rem;
  border-radius: 10px;
  letter-spacing: .04em;
}
.stage-IF  { background:#DBEAFE; color:#1D4ED8; }
.stage-ID  { background:#FEF9C3; color:#92400E; }
.stage-EX  { background:#DCFCE7; color:#166534; }
.stage-MEM { background:#FCE7F3; color:#9D174D; }
.stage-WB  { background:#EDE9FE; color:#5B21B6; }
.stage-bubble { background:#F3F4F6; color:#9CA3AF; font-style:italic; }

/* ── Multi-cycle log columns ─────────────────────────────────────────── */
.log-row-mc {
  display: grid;
  grid-template-columns: 2.2rem 3.5rem 1.8rem 1fr 1fr;
  gap: .3rem;
  padding: .15rem .2rem;
  border-bottom: 1px solid #EDEBE6;
  align-items: center;
}
.log-row-mc:hover { background: #F5F2ED; }

/* ── Pipeline stage strip ────────────────────────────────────────────── */
.pipeline-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 3px;
  padding: .4rem;
  background: #F5F2ED;
  flex-shrink: 0;
}
.pipeline-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .25rem .2rem;
  min-width: 0;
}
.pipeline-cell-label {
  font-size: .6rem;
  font-weight: 700;
  font-family: var(--mono);
  color: var(--muted);
  margin-bottom: .15rem;
}
.pipeline-cell-val {
  font-size: .6rem;
  font-family: var(--mono);
  color: var(--text);
  text-align: center;
  word-break: break-all;
  white-space: normal;
}
.pipeline-cell.active-IF  { background:#DBEAFE; border-color:#93C5FD; }
.pipeline-cell.active-ID  { background:#FEF9C3; border-color:#FDE68A; }
.pipeline-cell.active-EX  { background:#DCFCE7; border-color:#86EFAC; }
.pipeline-cell.active-MEM { background:#FCE7F3; border-color:#F9A8D4; }
.pipeline-cell.active-WB  { background:#EDE9FE; border-color:#C4B5FD; }

/* ── Pipeline Gantt diagram ──────────────────────────────────────────── */
.pipeline-diagram-wrap {
  padding: .4rem;
  overflow-x: auto;
  overflow-y: auto;
  max-height: 240px;
  background: #FAFAF8;
}
.pipeline-diagram {
  font-family: var(--mono);
  font-size: .58rem;
  border-collapse: collapse;
  white-space: nowrap;
}
.pipeline-diagram th {
  position: sticky;
  top: 0;
  background: #F5F2ED;
  color: var(--muted);
  padding: .15rem .35rem;
  border: 1px solid var(--border);
  text-align: center;
  font-weight: 600;
  z-index: 2;
}
.pipeline-diagram td {
  border: 1px solid #E5E3DE;
  padding: .12rem .25rem;
  text-align: center;
  min-width: 1.8rem;
}
.pipeline-diagram .instr-label {
  position: sticky;
  left: 0;
  background: #F5F2ED;
  color: var(--mid);
  font-weight: 600;
  text-align: left;
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 1;
  padding-right: .4rem;
}
.pd-IF  { background:#DBEAFE; color:#1D4ED8; }
.pd-ID  { background:#FEF9C3; color:#92400E; }
.pd-EX  { background:#DCFCE7; color:#166534; }
.pd-MEM { background:#FCE7F3; color:#9D174D; }
.pd-WB  { background:#EDE9FE; color:#5B21B6; }
.pd-stall { background:#FEF3C7; color:#92400E; font-style:italic; }

/* ── Stall indicator ─────────────────────────────────────────────────── */
.stall-badge {
  display: inline-block;
  font-size: .65rem;
  background: #FEF3C7;
  color: #92400E;
  border: 1px solid #FDE68A;
  border-radius: 8px;
  padding: .1rem .45rem;
  margin-left: .4rem;
}

/* ── Responsive ──────────────────────────────────────────────────────── */
@media (max-width: 780px) {
  .workspace { grid-template-columns: 1fr; }
  .export-files { grid-template-columns: 1fr; }
  .reg-grid { grid-template-columns: repeat(4, 1fr); }
}
