/* ARM64 Address & Page Table Calculator
 * Standalone: no framework, no webfonts, no network dependencies.
 * Works over file://.
 */

/* ---------------------------------------------------------------- tokens */

:root {
  --ui-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
             "Helvetica Neue", Arial, sans-serif;
  --mono-font: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
               "Liberation Mono", monospace;

  --bg: #f7f8fa;
  --surface: #ffffff;
  --ink: #1f2937;
  --ink-soft: #5b6472;
  --ink-faint: #8a93a0;
  --line: #dde1e7;
  --line-strong: #b9c0ca;
  --accent: #2b6cb0;
  --warn-bg: #fff6e5;
  --warn-ink: #8a5a00;
  --err: #b42318;

  /* field palette: --fc solid (strip boxes), --ft tint (bit cells) */
  --violet-c: #b69cf7;  --violet-t: #efe9fe;
  --blue-c:   #7fb2f5;  --blue-t:   #e0ecfd;
  --green-c:  #5fd3a7;  --green-t:  #d8f6ea;
  --amber-c:  #f6c75a;  --amber-t:  #fdf0cf;
  --rose-c:   #f99099;  --rose-t:   #fde3e5;
  --teal-c:   #5cc8d6;  --teal-t:   #d6f2f6;
  --indigo-c: #96a6f0;  --indigo-t: #e4e8fd;
  --gray-c:   #ccd3dc;  --gray-t:   #eef1f5;
  --on-field: #1b2330;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181d;
    --surface: #1d2127;
    --ink: #e6e9ee;
    --ink-soft: #a8b0bb;
    --ink-faint: #7b8492;
    --line: #333a44;
    --line-strong: #4a535f;
    --accent: #7fb2f5;
    --warn-bg: #3a2f14;
    --warn-ink: #f0ce8a;
    --err: #f28b82;

    --violet-c: #8f73d8; --violet-t: #332a4d;
    --blue-c:   #5590d8; --blue-t:   #1e2f45;
    --green-c:  #3fae89; --green-t:  #16332a;
    --amber-c:  #d2a337; --amber-t:  #3b2f14;
    --rose-c:   #d4727c; --rose-t:   #3d2125;
    --teal-c:   #3f9aa8; --teal-t:   #143037;
    --indigo-c: #6f7fce; --indigo-t: #232946;
    --gray-c:   #5d6774; --gray-t:   #272c33;
    --on-field: #10141a;
  }
}

/* field color classes, applied to strip boxes / bit groups / cards */
.f-violet { --fc: var(--violet-c); --ft: var(--violet-t); }
.f-blue   { --fc: var(--blue-c);   --ft: var(--blue-t);   }
.f-green  { --fc: var(--green-c);  --ft: var(--green-t);  }
.f-amber  { --fc: var(--amber-c);  --ft: var(--amber-t);  }
.f-rose   { --fc: var(--rose-c);   --ft: var(--rose-t);   }
.f-teal   { --fc: var(--teal-c);   --ft: var(--teal-t);   }
.f-indigo { --fc: var(--indigo-c); --ft: var(--indigo-t); }
.f-gray   { --fc: var(--gray-c);   --ft: var(--gray-t);   }

/* ----------------------------------------------------------------- page */

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 1.5rem 1rem 3rem;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--ui-font);
  font-size: 15px;
  line-height: 1.5;
}

.page-head,
.tabs,
.panel,
.page-foot { max-width: 1100px; margin-left: auto; margin-right: auto; }

.page-head { text-align: center; margin-bottom: 1.25rem; }

h1 {
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.015em;
  margin: 0 0 0.35rem;
}

.assumptions { margin: 0; color: var(--ink-soft); font-size: 0.95rem; }
.assumptions b { color: var(--ink); font-weight: 600; }

.noscript {
  max-width: 1100px; margin: 0 auto 1rem;
  padding: 0.75rem 1rem;
  background: var(--warn-bg); color: var(--warn-ink);
  border-radius: 6px;
}

.page-foot {
  margin-top: 2rem;
  text-align: center;
  color: var(--ink-faint);
  font-size: 0.85rem;
}
.page-foot p { margin: 0; }

/* ----------------------------------------------------------------- tabs */

.tabs {
  display: flex;
  gap: 0.4rem;
  margin-bottom: -1px;
  padding-left: 0.5rem;
}

.tab {
  font: inherit;
  font-weight: 600;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  padding: 0.5rem 1rem;
  cursor: pointer;
}

.tab:hover { color: var(--ink); }

.tab[aria-selected="true"] {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- panel */

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1.5rem;
}

.placeholder { color: var(--ink-faint); text-align: center; margin: 2rem 0; }

/* -------------------------------------------------------- panel toolbar */

.panel-toolbar { display: flex; justify-content: flex-end; margin-bottom: 0.5rem; }

.basesel {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.basesel legend {
  float: left;              /* keep the legend inline with the radios */
  padding: 0 0.5rem 0 0;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-faint);
}

.basesel label {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.basesel input { accent-color: var(--accent); cursor: pointer; }

/* ---------------------------------------------------------------- input */

.input-area { text-align: center; margin-bottom: 1.5rem; }

.input-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
  margin-bottom: 0.4rem;
}

.value-input {
  width: 100%;
  max-width: 460px;
  font-family: var(--mono-font);
  font-size: 1.6rem;
  text-align: center;
  padding: 0.5rem 0.75rem;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
}

.value-input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.hint { margin: 0.45rem 0 0; font-size: 0.82rem; color: var(--ink-faint); }
.hint code { font-family: var(--mono-font); }

.error { margin: 0.45rem 0 0; font-size: 0.85rem; color: var(--err); font-weight: 600; }

/* --------------------------------------------------------- field strip */

.strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: stretch;
  gap: 0.6rem;
  margin-bottom: 1.5rem;
}

.strip-box {
  background: var(--fc);
  color: var(--on-field);
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 10px;
  padding: 0.6rem 0.9rem;
  min-width: 7rem;
  text-align: center;
}

/* The value is editable: type a number here and the address updates. */
.strip-input {
  display: block;
  width: 100%;
  font-family: var(--mono-font);
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  color: inherit;
  background: transparent;
  border: none;
  border-bottom: 1px dashed rgba(0, 0, 0, 0.28);
  border-radius: 0;
  padding: 0 0 0.1rem;
  margin-bottom: 0.25rem;
}

.strip-input:hover { border-bottom-color: rgba(0, 0, 0, 0.5); }

.strip-input:focus {
  outline: none;
  border-bottom: 1px solid rgba(0, 0, 0, 0.7);
  background: rgba(255, 255, 255, 0.35);
}

.strip-input.is-invalid {
  background: rgba(220, 38, 38, 0.22);
  border-bottom-color: #8b1a14;
}

.strip-name {
  display: block;
  font-size: 0.88rem;
  font-weight: 700;
  white-space: nowrap;
  line-height: 1.25;
}

.strip-bits {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  white-space: nowrap;
  opacity: 0.68;
}

.error-centered { text-align: center; }

/* ------------------------------------------------------------ bit rows */

.bits-wrap { margin-bottom: 1.5rem; }

/* Whole field groups wrap; a group never breaks internally. */
.bitrow {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
}

.bitgroup { display: flex; flex-direction: column; align-items: center; }

.bits { display: flex; }

.bitcell { display: flex; flex-direction: column; align-items: center; }

.bitidx {
  font-family: var(--mono-font);
  font-size: 0.62rem;
  color: var(--ink-faint);
  line-height: 1.2;
  height: 1.2em;
  user-select: none;
}

.bit {
  font-family: var(--mono-font);
  font-size: 0.85rem;
  line-height: 1;
  width: 1.4rem;
  height: 1.6rem;
  padding: 0;
  cursor: pointer;
  color: var(--ink);
  background: var(--ft);
  border: 1px solid var(--line);
  border-right-width: 0;
}

.bitcell:first-child .bit { border-radius: 4px 0 0 4px; }
.bitcell:last-child  .bit { border-radius: 0 4px 4px 0; border-right-width: 1px; }

.bit[aria-checked="true"] { font-weight: 700; }
.bit[aria-checked="false"] { color: var(--ink-faint); }
.bit:hover { filter: brightness(0.94); }
.bit:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; position: relative; }

.grouplabel {
  margin-top: 0.3rem;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--ink-soft);
  text-align: center;
}

.bits-hint { margin: 0.8rem 0 0; text-align: center; font-size: 0.82rem; color: var(--ink-faint); }

/* Small all-caps heading over a block, as used above the walk. */
.section-head {
  margin: 0 0 0.4rem;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
}

/* ------------------------------------------------- descriptor (tab 2) */

/* Two bit rows stack; give the second a little breathing room. */
#pte-bits-lo { margin-top: 0.9rem; }

/* The fields the lecture bolds get a heavier outline, as in the l15 figure. */
.bitgroup.is-important .bit {
  border-top-width: 2px;
  border-bottom-width: 2px;
  border-color: var(--line-strong);
}
.bitgroup.is-important .bitcell:first-child .bit { border-left-width: 2px; }
.bitgroup.is-important .bitcell:last-child .bit { border-right-width: 2px; }
.bitgroup.is-important .grouplabel { color: var(--ink); }

.grouplabel .gl-cont { font-weight: 400; color: var(--ink-faint); }

/* One row per exception level, with the labels and values in aligned
 * columns so the two are easy to compare at a glance. */
.perm {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.3rem 1.25rem;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.perm-group { display: contents; }

.perm-who,
.perm-what {
  font-family: var(--mono-font);
  font-size: 0.95rem;
}

.perm-who { font-weight: 700; }
.perm-no { color: var(--ink-faint); }

.warnings { list-style: none; margin: 0 0 1rem; padding: 0; }

.warnings li {
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.4rem;
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-radius: 6px;
  font-size: 0.88rem;
}

.warnings li code { font-family: var(--mono-font); }

.pte-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.pte-table th {
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-faint);
  border-bottom: 1px solid var(--line-strong);
  padding: 0.3rem 0.5rem;
}

.pte-table td {
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid var(--line);
  vertical-align: baseline;
}

.pte-table .c-bits,
.pte-table .c-value { font-family: var(--mono-font); white-space: nowrap; }

.pte-table .c-bits { color: var(--ink-faint); }
.pte-table .c-name { font-weight: 600; }

/* Fields the lecture doesn't emphasize stay present but quiet. */
.pte-table tr.is-minor td { color: var(--ink-faint); }
.pte-table tr.is-minor .c-name { font-weight: 400; }

/* A color chip tying each row to its group in the diagram above. */
.pte-table .c-name::before {
  content: "";
  display: inline-block;
  width: 0.65rem;
  height: 0.65rem;
  margin-right: 0.45rem;
  border-radius: 2px;
  background: var(--fc);
  border: 1px solid rgba(0, 0, 0, 0.15);
}

.pte-table tr.is-dim td { opacity: 0.45; }

/* ------------------------------------------------------------- derived */

.derived {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 1.25rem;
}

.derived-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-faint);
}

.derived-value { font-family: var(--mono-font); font-size: 1rem; }

/* ---------------------------------------------------------------- walk */

.walk {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.85rem 1rem;
}

.walk h2 {
  margin: 0 0 0.4rem;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
}

.walk-text { margin: 0; line-height: 1.7; }

.walk-text .w-num {
  font-family: var(--mono-font);
  font-weight: 700;
  padding: 0.05em 0.3em;
  border-radius: 4px;
  background: var(--ft);
  border: 1px solid var(--fc);
}

.walk-text .w-byte { font-family: var(--mono-font); color: var(--ink-soft); }

/* ------------------------------------------------------------- actions */

.actions { display: flex; align-items: center; gap: 0.5rem; margin-top: 1.25rem; }

.btn {
  font: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 0.4rem 0.85rem;
}

.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.copied { font-size: 0.85rem; color: var(--ink-faint); }

/* ----------------------------------------------------------- print view */

@media print {
  body { background: #fff; padding: 0; }
  .tabs, .actions, .bits-hint, .hint, .noscript,
  .panel-toolbar { display: none !important; }
  .panel { border: none; padding: 0; }
  .value-input { border: none; background: transparent; font-size: 1.3rem; }
  .bit { border-color: #999; }
  .strip-box, .walk, .derived { break-inside: avoid; }
  .strip-input { border-bottom: none; }
}
