:root {
  --key-size: 56px;
  --key-gap: 6px;
  --bg: #0e0f12;
  --fg: #e8e6df;
  --fg-muted: #a8a6a0;
  --kb-bg: #2a2c34;
  --kb-border: #3a3c44;
  --key-bg: #f4f1ea;
  --key-fg: #1a1a1a;
  --key-border: #d8d3c5;
  --key-dim-bg: #4a4d55;
  --key-dim-fg: #c8c8c8;
  --key-dim-border: #5a5d65;
  --accent: #ffcc55;
  --accent-fg: #1a1a1a;
  --modal-backdrop: rgba(0, 0, 0, .7);
  --card-bg: #16171b;
  --card-border: #2a2b30;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --serif: Georgia, "Times New Roman", serif;
}

[data-theme="light"] {
  --bg: #f7f5f0;
  --fg: #1a1a1a;
  --fg-muted: #5a5a5a;
  --kb-bg: #6a6e75;
  --kb-border: #8a8e95;
  --key-bg: #ffffff;
  --key-fg: #1a1a1a;
  --key-border: #b8b3a5;
  --key-dim-bg: #c8ccd0;
  --key-dim-fg: #2a2b30;
  --key-dim-border: #a0a4aa;
  --accent: #d4900a;
  --accent-fg: #ffffff;
  --modal-backdrop: rgba(20, 20, 20, .5);
  --card-bg: #ffffff;
  --card-border: #d8d3c5;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--fg); font-family: var(--serif); }
.page-header {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1rem 1rem;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
}
.page-header-content {
  grid-column: 2;
  text-align: center;
}
.page-header h1 { margin: 0; }
.page-header .tagline { margin: .5rem 0 0; }
.page-header .kb-controls {
  grid-column: 3;
  justify-self: end;
  flex-wrap: wrap;
}
@media (max-width: 700px) {
  .page-header {
    grid-template-columns: 1fr;
  }
  .page-header-content,
  .page-header .kb-controls {
    grid-column: 1;
    justify-self: center;
  }
}
main { max-width: 1200px; margin: 0 auto; padding: 0 1rem 2rem; }
h1 { font-family: var(--mono); font-size: 1.75rem; letter-spacing: .08em; font-weight: 700; margin: 0; }
.tagline { font-size: 1rem; opacity: .75; margin: .35rem 0 0; }
.tagline { opacity: .7; }
footer { max-width: 1200px; margin: 0 auto; padding: 2rem 1rem; display: flex; gap: 2rem; justify-content: center; flex-wrap: wrap; color: var(--fg-muted); }
footer a { color: inherit; text-decoration: none; }
footer a:hover { text-decoration: underline; }

#keyboard {
  display: inline-flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 1.25rem;
  padding: 16px;
  background: var(--kb-bg);
  border: 1px solid var(--kb-border);
  border-radius: 12px;
  margin: 0 auto;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .25);
}

.kb-section {
  display: flex;
  flex-direction: column;
  gap: var(--key-gap);
}

#keyboard-wrap {
  width: 100%;
  padding: 1rem 0;
  overflow-x: auto;
  text-align: center;
  position: relative;
  touch-action: pan-x pan-y;
  min-height: 380px;
}
.zoom-controls {
  display: inline-flex;
  gap: 2px;
  padding: 4px;
  background: transparent;
  border: 1px solid var(--card-border);
  border-radius: 8px;
}
.zoom-controls button {
  background: transparent;
  color: var(--fg);
  border: 0;
  width: 32px;
  height: 32px;
  font-family: var(--mono);
  font-size: 1.1rem;
  cursor: pointer;
  border-radius: 4px;
  line-height: 1;
}
.zoom-controls button:hover { background: var(--kb-bg); }

.kb-row {
  display: flex;
  gap: var(--key-gap);
  height: var(--key-size);
}

.kb-key {
  width: calc(var(--key-size) * var(--w, 1) + (var(--w, 1) - 1) * var(--key-gap));
  height: var(--key-size);
  flex: none;
  background: var(--key-bg);
  background: linear-gradient(180deg, var(--key-bg) 0%, color-mix(in srgb, var(--key-bg) 92%, black) 100%);
  color: var(--key-fg);
  border: 1px solid var(--key-border);
  border-bottom-width: 3px;
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .08s ease, box-shadow .08s ease, filter .15s;
  position: relative;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .05), inset 0 1px 0 rgba(255, 255, 255, .6);
  padding: 0;
}
.kb-key:hover { filter: brightness(1.05); }
.kb-key.dim {
  background: var(--key-dim-bg);
  color: var(--key-dim-fg);
  border-color: var(--key-dim-border);
  cursor: default;
  box-shadow: none;
}
.kb-key.dim:hover { filter: none; }
.kb-key.pressed { transform: translateY(2px); border-bottom-width: 1px; }
.kb-key.lit {
  box-shadow:
    0 0 0 1px var(--accent),
    0 0 12px color-mix(in srgb, var(--accent) 30%, transparent),
    inset 0 -2px 0 rgba(0, 0, 0, .05),
    inset 0 1px 0 rgba(255, 255, 255, .6);
}

.kb-spacer {
  width: calc(var(--key-size) * var(--w, 1) + (var(--w, 1) - 1) * var(--key-gap));
  height: var(--key-size);
  flex: none;
  pointer-events: none;
}

/* Tall numpad keys (num-add, num-enter) spanning two rows */
.kb-key[data-h="2"][data-extend="down"] {
  height: calc(2 * var(--key-size) + var(--key-gap));
  align-self: flex-start;
}
.kb-key[data-h="2"][data-extend="up"] {
  height: calc(2 * var(--key-size) + var(--key-gap));
  align-self: flex-end;
}

/* L-shaped ISO Enter: single element, clip-path defines the L */
.kb-key[data-iso="true"] {
  clip-path: polygon(
    0 0,
    100% 0,
    100% 100%,
    33.333% 100%,
    33.333% calc(var(--key-size) + var(--key-gap)),
    0% calc(var(--key-size) + var(--key-gap))
  );
  /* clip-path clips outer shadows + borders; use inset box-shadow for the visible edge */
  border: 0;
  background: linear-gradient(180deg, var(--key-bg) 0%, color-mix(in srgb, var(--key-bg) 92%, black) 100%);
  box-shadow: inset 0 0 0 1px var(--key-border);
  filter: none;
  display: grid;
  grid-template-rows: calc(var(--key-size) + var(--key-gap)) 1fr;
  place-items: center;
  line-height: 1;
  padding: 0;
}
.kb-key[data-iso="true"].lit {
  box-shadow: inset 0 0 0 2px var(--accent);
  filter: drop-shadow(0 0 10px color-mix(in srgb, var(--accent) 60%, transparent));
}
.kb-key[data-iso="true"].pressed {
  transform: translateY(2px);
}

.card { /* layout-only inside modal */ }
.card-era { display: inline-block; font-family: var(--mono); font-size: .75rem; letter-spacing: .1em; padding: .25rem .5rem; background: var(--accent); color: var(--accent-fg); border-radius: 4px; }
.card-title { font-family: var(--mono); font-size: 2rem; margin: .5rem 0 1rem; }
.card p { line-height: 1.6; }

/* visited filter mode */
body.show-visited .kb-key:not(.visited):not(.dim),
body.show-unvisited .kb-key.visited {
  opacity: .25;
  transition: opacity .15s ease;
}

body.shift-on .kb-key[data-id="shift-l"],
body.shift-on .kb-key[data-id="shift-r"],
body.altgr-on .kb-key[data-id="alt-r"] {
  background: var(--accent);
  color: var(--accent-fg);
}

#theme-toggle {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  padding: .5rem .75rem;
  cursor: pointer;
  font-size: 1rem;
  font-family: var(--mono);
  line-height: 1;
}
#theme-toggle:hover { background: var(--card-bg); }

.kb-controls {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: .35rem;
}
.kb-controls-row {
  display: flex;
  gap: .5rem;
}
[data-theme="dark"] .theme-icon-light,
:root:not([data-theme]) .theme-icon-light { display: none; }
[data-theme="light"] .theme-icon-dark { display: none; }

.modal[hidden] { display: none; }
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--modal-backdrop);
  backdrop-filter: blur(4px);
}
.modal-card {
  position: relative;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  max-width: 600px;
  width: calc(100% - 2rem);
  max-height: calc(100vh - 4rem);
  z-index: 1;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .4);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-card-header {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  padding: .5rem .5rem 0;
}
.modal-content {
  overflow-y: auto;
  padding: 0 2rem 2rem;
  flex: 1 1 auto;
}
.modal-close {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 50%;
  color: var(--fg);
  font-size: 1.4rem;
  cursor: pointer;
  width: 40px;
  height: 40px;
  padding: 0;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
}
.modal-close:hover { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
body.modal-open { overflow: hidden; }

#shortcuts-button {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  padding: .5rem .75rem;
  cursor: pointer;
  font-family: var(--mono);
  font-size: .9rem;
  line-height: 1;
}
#shortcuts-button:hover { background: var(--card-bg); }

.shortcuts-list .shortcuts-title {
  font-family: var(--mono);
  margin: 0 0 1rem;
  font-size: 1.5rem;
}
.shortcut-group h3 {
  font-family: var(--mono);
  font-size: .8rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  opacity: .7;
  margin: 1.5rem 0 .5rem;
}
.shortcut-group ul { list-style: none; padding: 0; margin: 0; }
.shortcut-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--card-border);
  padding: .75rem .25rem;
  text-align: left;
  cursor: pointer;
  color: var(--fg);
  font-family: inherit;
}
.shortcut-row:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.shortcut-row:last-child { border-bottom: 0; }
.shortcut-title { font-family: var(--mono); font-size: 1rem; }
.shortcut-era { font-size: .8rem; opacity: .6; font-family: var(--mono); }

@media (max-width: 600px) {
  #shortcuts-button { font-size: .75rem; padding: .4rem .6rem; }
}

.cheatsheet-trigger {
  display: flex;
  justify-content: center;
  margin: 1rem auto 0;
  padding: 0 1rem;
}
#cheatsheet-button {
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 0;
  font: inherit;
}
.cheatsheet-trigger-paper {
  display: inline-block;
  background: #fbf8e8;
  background-image:
    linear-gradient(transparent calc(1.4em - 1px), rgba(80, 130, 180, .35) 1.4em);
  background-size: 100% 1.4em;
  color: #1a1a2a;
  font-family: 'Caveat', 'Comic Sans MS', cursive;
  font-size: 1.4rem;
  line-height: 1.4em;
  padding: .35em 1.2em .55em;
  border-radius: 4px;
  box-shadow: 2px 3px 10px rgba(0, 0, 0, .35);
  transform: rotate(-1.2deg);
  transition: transform .15s ease, box-shadow .15s ease;
  border-top: 8px solid rgba(255, 240, 130, .7);
  position: relative;
}
#cheatsheet-button:hover .cheatsheet-trigger-paper {
  transform: rotate(-2deg) translateY(-2px);
  box-shadow: 3px 5px 14px rgba(0, 0, 0, .4);
}

.cheatsheet {
  position: relative;
  background-color: #fbf8e8;
  background-image:
    linear-gradient(transparent calc(1.6em - 1px), rgba(80, 130, 180, .35) 1.6em),
    linear-gradient(90deg, transparent 2.2rem, rgba(220, 80, 80, .55) 2.2rem, rgba(220, 80, 80, .55) calc(2.2rem + 1px), transparent calc(2.2rem + 1px));
  background-size: 100% 1.6em, 100% 100%;
  background-repeat: repeat-y, no-repeat;
  color: #1a1a2a;
  font-family: 'Caveat', 'Comic Sans MS', cursive;
  font-size: 1.35rem;
  line-height: 1.6em;
  padding: 1.4em 1.5rem 1.6em 3rem;
  border-radius: 4px;
  box-shadow: 4px 6px 18px rgba(0, 0, 0, .35), inset 0 0 0 1px rgba(0, 0, 0, .04);
  transform: rotate(-.6deg);
}
.cheatsheet::before,
.cheatsheet::after {
  content: '';
  position: absolute;
  top: -14px;
  width: 130px;
  height: 30px;
  background: rgba(255, 240, 130, .7);
  box-shadow: 0 2px 4px rgba(0, 0, 0, .25);
  pointer-events: none;
  z-index: 1;
}
.cheatsheet::before {
  left: 6%;
  transform: rotate(-4deg);
}
.cheatsheet::after {
  right: 6%;
  transform: rotate(3deg);
}
.cheatsheet-head {
  font-family: 'Caveat', cursive;
  font-size: 2.4rem;
  font-weight: 700;
  text-align: center;
  margin: 0 0 .1em;
  text-decoration: underline wavy;
  text-decoration-color: rgba(220, 80, 80, .7);
  text-underline-offset: 6px;
}
.cheatsheet-sub {
  text-align: center;
  font-size: 1rem;
  opacity: .7;
  margin: 0 0 1.2em;
  font-style: italic;
}
.cheatsheet-section { margin: 1em 0 0; }
.cheatsheet-section h3 {
  font-family: 'Caveat', cursive;
  font-size: 1.4rem;
  font-weight: 700;
  margin: .2em 0 .1em;
  color: rgba(220, 80, 80, .85);
}
.cheatsheet-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
  column-count: 2;
  column-gap: 1.5rem;
}
.cheatsheet-row {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  width: 100%;
  background: transparent;
  border: 0;
  padding: 0 0 0 .25em;
  text-align: left;
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  cursor: pointer;
  break-inside: avoid;
}
.cheatsheet-row:hover {
  background: rgba(255, 240, 130, .55);
  text-decoration: line-through wavy;
  text-decoration-color: rgba(220, 80, 80, .6);
}
.cheatsheet-title { font-weight: 700; }
.cheatsheet-era {
  opacity: .55;
  font-size: .85em;
  white-space: nowrap;
}

@media (max-width: 600px) {
  .cheatsheet-section ul { column-count: 1; }
  .cheatsheet { font-size: 1.2rem; padding: 1.4em 1rem 1.6em 2.2rem; }
}

/* Cheatsheet inside modal: override modal-card padding so paper feels right */
#modal .modal-card:has(.cheatsheet) {
  background: transparent;
  border: 0;
  box-shadow: none;
  max-width: 720px;
}
#modal .modal-card:has(.cheatsheet) .modal-content {
  padding: .5rem 0 0;
  overflow-x: hidden;
}

/* Modifier active mode: fade non-relevant keys, highlight active set */
body.shift-on .kb-key:not([data-shift-active]),
body.altgr-on .kb-key:not([data-altgr-active]),
body.ctrl-on .kb-key:not([data-ctrl-active]) {
  opacity: .25;
  transition: opacity .15s ease;
}
body.shift-on .kb-key[data-shift-active],
body.altgr-on .kb-key[data-altgr-active],
body.ctrl-on .kb-key[data-ctrl-active] {
  transition: opacity .15s ease;
}

#visited-filter {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  padding: 0 .75rem;
  cursor: pointer;
  font-family: var(--mono);
  font-size: .85rem;
  line-height: 1;
  height: 42px;
}
#visited-filter:hover { background: var(--card-bg); }
#visited-filter[data-state="visited"],
#visited-filter[data-state="unvisited"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
