:root {
  --bg-primary:    #1e1e2e;
  --bg-secondary:  #181825;
  --bg-panel:      #313244;
  --bg-input:      #1e1e2e;
  --border:        #45475a;
  --text-primary:  #cdd6f4;
  --text-muted:    #6c7086;
  --text-dim:      #585b70;
  --accent-blue:   #89b4fa;
  --accent-green:  #a6e3a1;
  --accent-yellow: #f9e2af;
  --accent-red:    #f38ba8;
  --accent-teal:   #94e2d5;
  --accent-mauve:  #cba6f7;
  --accent-peach:  #fab387;
  --highlight-pc:  #2a3045;
  --changed-reg:   #2a2a1a;
  --font-mono: 'Courier New', Courier, monospace;
}

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

html {
  height: 100%;
  height: -webkit-fill-available;
}

body {
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 13px;
  /* Pin the body to the visible viewport so iOS Safari can't pull the
     top of the page under the tab/address bar (rubber-band scroll). */
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  /* Fallback chain: vh → iOS fill-available → modern dynamic viewport unit */
  height: 100vh;
  height: -webkit-fill-available;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

#app {
  display: grid;
  /* Toolbar height is auto so wrapped controls never clip the workspace */
  grid-template-rows: auto 1fr 160px;
  height: 100%;
  /* Respect the safe areas (notch / status bar / home indicator) */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

/* ── Toolbar ── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  min-height: 44px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.title {
  font-size: 14px;
  font-weight: bold;
  color: var(--accent-blue);
  margin-right: 8px;
  white-space: nowrap;
}
.controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

button {
  background: var(--bg-panel);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 12px;
  transition: background 0.15s;
}
button:hover:not(:disabled) { background: #414459; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button:active:not(:disabled) { background: #505375; }

#btn-assemble { color: var(--accent-green); border-color: var(--accent-green); }
#btn-step     { color: var(--accent-teal);  border-color: var(--accent-teal);  }
#btn-run      { color: var(--accent-blue);  border-color: var(--accent-blue);  }
#btn-reset    { color: var(--accent-peach); border-color: var(--accent-peach); }

kbd {
  font-size: 10px;
  color: var(--text-muted);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 3px;
}

.toggle-label, .speed-label {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
#run-speed {
  width: 54px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 2px 4px;
  font-family: var(--font-mono);
  font-size: 12px;
}

/* ── Workspace ── */
#workspace {
  display: grid;
  grid-template-columns: 1fr 320px;
  overflow: hidden;
  min-height: 0; /* allow inner scroll regions to size correctly on iOS */
}

#left-panel {
  display: grid;
  grid-template-rows: 1fr 1fr;
  overflow: hidden;
  min-height: 0;
  border-right: 1px solid var(--border);
}

/* ── Panel header ── */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}
.hint { color: var(--text-dim); font-style: italic; }

/* ── Editor ── */
#editor-section {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}
#asm-editor {
  flex: 1;
  background: var(--bg-input);
  color: var(--text-primary);
  border: none;
  outline: none;
  resize: none;
  padding: 8px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  tab-size: 4;
  overflow-y: auto;
}

/* ── Bytecode ── */
#bytecode-section {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#error-banner {
  background: #2d1b1b;
  border-bottom: 1px solid var(--accent-red);
  padding: 4px 8px;
  color: var(--accent-red);
  font-size: 12px;
  display: none;
  max-height: 80px;
  overflow-y: auto;
}
#error-banner.visible { display: block; }
.error-line { cursor: pointer; padding: 1px 0; }
.error-line:hover { text-decoration: underline; }

#bytecode-view {
  flex: 1;
  overflow-y: auto;
  font-size: 12px;
}
#instr-breakdown {
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  font-size: 12px;
  padding: 0;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.2s ease;
}
#instr-breakdown.visible { max-height: 160px; overflow-y: auto; padding: 8px; }

.bc-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 8px;
  border-bottom: 1px solid transparent;
  cursor: pointer;
  line-height: 1.5;
}
.bc-row:hover { background: var(--bg-panel); }
.bc-row.active-pc {
  background: var(--highlight-pc);
  border-left: 3px solid var(--accent-blue);
  padding-left: 5px;
}
.bc-row.selected { background: #252538; }
.bc-addr { color: var(--text-muted); min-width: 76px; }
.bc-hex  { color: var(--accent-yellow); min-width: 74px; }
.bc-bits { display: flex; gap: 2px; }
.bc-disasm { color: var(--text-primary); margin-left: 4px; }

/* Instruction field colors */
.bf-op     { color: var(--accent-red);    background: #2d1b1b; border-radius: 2px; padding: 0 1px; }
.bf-rs     { color: var(--accent-blue);   background: #1b2340; border-radius: 2px; padding: 0 1px; }
.bf-rt     { color: var(--accent-teal);   background: #1b2d2d; border-radius: 2px; padding: 0 1px; }
.bf-rd     { color: var(--accent-green);  background: #1b2d1b; border-radius: 2px; padding: 0 1px; }
.bf-sh     { color: var(--accent-mauve);  background: #2d1b2d; border-radius: 2px; padding: 0 1px; }
.bf-fn     { color: var(--accent-peach);  background: #2d2010; border-radius: 2px; padding: 0 1px; }
.bf-imm    { color: var(--accent-yellow); background: #2d2b1b; border-radius: 2px; padding: 0 1px; }
.bf-tgt    { color: var(--accent-mauve);  background: #2d1b2d; border-radius: 2px; padding: 0 1px; }

/* Breakdown panel */
.bd-title { color: var(--accent-blue); font-weight: bold; margin-bottom: 6px; }
.bd-fields {
  display: flex;
  gap: 0;
  margin-bottom: 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.bd-field {
  flex: 1;
  text-align: center;
  padding: 3px 2px;
  border-right: 1px solid var(--border);
}
.bd-field:last-child { border-right: none; }
.bd-field .bd-bits  { font-size: 10px; color: var(--text-muted); }
.bd-field .bd-name  { font-size: 10px; color: var(--text-muted); }
.bd-field .bd-val   { font-size: 12px; font-weight: bold; }
.bd-info { color: var(--text-muted); font-size: 11px; margin-top: 4px; }

/* ── Right panel ── */
#right-panel {
  display: grid;
  grid-template-rows: 1fr 200px;
  overflow: hidden;
}

/* ── Register table ── */
#register-section {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#register-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
#register-table thead th {
  background: var(--bg-primary);
  color: var(--text-muted);
  padding: 3px 6px;
  text-align: left;
  position: sticky;
  top: 0;
  border-bottom: 1px solid var(--border);
  font-weight: normal;
  font-size: 11px;
}
.reg-tbody-wrap {
  flex: 1;
  overflow-y: auto;
}
.reg-tbody-wrap table { width: 100%; border-collapse: collapse; font-size: 12px; }
.reg-row td {
  padding: 1px 6px;
  border-bottom: 1px solid transparent;
  line-height: 1.6;
}
.reg-row:hover td { background: var(--bg-panel); }
.reg-row.changed td { background: var(--changed-reg); }
.reg-row.zero-reg td { color: var(--text-dim); }
.reg-num  { color: var(--text-dim); width: 24px; }
.reg-name { color: var(--accent-teal); width: 50px; }
.reg-hex  { color: var(--accent-yellow); width: 80px; font-variant-numeric: tabular-nums; }
.reg-dec  { color: var(--text-muted); font-variant-numeric: tabular-nums; }

#special-regs {
  border-top: 1px solid var(--border);
  padding: 4px 6px;
  background: var(--bg-primary);
  display: flex;
  gap: 12px;
}
.special-reg-row { display: flex; gap: 6px; align-items: center; }
.sreg-name { color: var(--text-muted); font-size: 11px; }
.sreg-val  { color: var(--accent-blue); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ── Memory ── */
#memory-section {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-top: 1px solid var(--border);
}
.mem-controls { display: flex; gap: 4px; }
.mem-seg-btn {
  padding: 2px 6px;
  font-size: 11px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.mem-seg-btn.active {
  background: var(--bg-panel);
  color: var(--accent-blue);
  border-color: var(--accent-blue);
}
#memory-view {
  flex: 1;
  overflow-y: auto;
  font-size: 11px;
  padding: 4px 0;
}
.mem-row { display: flex; gap: 6px; padding: 1px 8px; }
.mem-row:hover { background: var(--bg-panel); }
.mem-addr { color: var(--text-muted); width: 76px; flex-shrink: 0; }
.mem-bytes { color: var(--accent-yellow); flex: 1; letter-spacing: 1px; }
.mem-ascii { color: var(--accent-green); width: 80px; }

/* ── Console ── */
#console-section {
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--border);
  overflow: hidden;
}
#console-output {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  font-size: 12px;
  line-height: 1.6;
}
.console-out   { color: var(--text-primary); }
.console-err   { color: var(--accent-red); }
.console-info  { color: var(--text-muted); font-style: italic; }

.small-btn {
  padding: 2px 6px;
  font-size: 11px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* Scrollbars */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-secondary); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

#error-count { color: var(--accent-red); }

/* ── Responsive: tablets / narrow viewports (e.g. iPad portrait) ── */
@media (max-width: 820px) {
  #workspace { grid-template-columns: 1fr 250px; }
  #right-panel { grid-template-rows: 1fr 160px; }
  .title { font-size: 13px; }
  .controls { gap: 4px; }
  button { padding: 6px 10px; } /* larger touch targets */
}

/* ── Short viewports: give the workspace more room ── */
@media (max-height: 600px) {
  #app { grid-template-rows: auto 1fr 110px; }
}
