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

:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1b1e24;
  --muted: #5b6371;
  --border: #d5d9e0;
  --accent: #2b62d9;
  --accent-hover: #2252bb;
  --accent-soft: #e3ebfc;
  --on-accent: #ffffff;
  --focus: #2b62d9;
  --shadow: 0 10px 30px rgb(20 24 32 / 0.14), 0 2px 6px rgb(20 24 32 / 0.08);

  --case: #e1e4ea;
  --key-bg: #ffffff;
  --key-bg-hover: #f2f5fb;
  --key-border: #c4c9d2;
  --key-edge: #b1b7c2;
  --key-text: #1b1e24;

  --done: #1d8044;
  --done-hover: #186c39;
  --on-done: #ffffff;

  --correct: #1f9d55;
  --correct-bg: #d4f2df;
  --correct-fg: #0d5a2c;
  --wrong: #d6363c;
  --wrong-bg: #fbd9d9;
  --wrong-fg: #8f1a1f;
  --missing: #d9a800;
  --missing-bg: #fcefb4;
  --missing-fg: #634b00;
  --extra: #d9419e;
  --extra-bg: #f9d6ec;
  --extra-fg: #861a5c;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121418;
  --surface: #1b1e24;
  --surface-2: #242830;
  --text: #e6e8ec;
  --muted: #9ba3b0;
  --border: #343a45;
  --accent: #6d9bff;
  --accent-hover: #8aafff;
  --accent-soft: #22324f;
  --on-accent: #0b1220;
  --focus: #8fb2ff;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.45), 0 2px 6px rgb(0 0 0 / 0.3);

  --case: #191c22;
  --key-bg: #2b2f38;
  --key-bg-hover: #343946;
  --key-border: #3e4451;
  --key-edge: #101216;
  --key-text: #e6e8ec;

  --done: #1d8044;
  --done-hover: #23934f;
  --on-done: #ffffff;

  --correct: #3fbf73;
  --correct-bg: #183d27;
  --correct-fg: #a9ecc3;
  --wrong: #f0575c;
  --wrong-bg: #4a1c1f;
  --wrong-fg: #ffc2c4;
  --missing: #e8c33a;
  --missing-bg: #433812;
  --missing-fg: #f8e29a;
  --extra: #ee6cb9;
  --extra-bg: #4a1b37;
  --extra-fg: #ffc0e3;
}

[data-status="correct"] { --st: var(--correct); --st-bg: var(--correct-bg); --st-fg: var(--correct-fg); }
[data-status="wrong"] { --st: var(--wrong); --st-bg: var(--wrong-bg); --st-fg: var(--wrong-fg); }
[data-status="missing"] { --st: var(--missing); --st-bg: var(--missing-bg); --st-fg: var(--missing-fg); }
[data-status="extra"] { --st: var(--extra); --st-bg: var(--extra-bg); --st-fg: var(--extra-fg); }

/* --------------------------------------------------------------- base */

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

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

a {
  color: var(--accent);
}

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

.page,
.site-header,
.site-footer {
  width: 100%;
  max-width: 1320px;
  margin-inline: auto;
  padding-inline: 16px;
}

/* ------------------------------------------------------------- header */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-block: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.brand-mark {
  width: 28px;
  height: 28px;
}

.brand-mark rect {
  fill: var(--accent);
}

.brand-mark path {
  fill: none;
  stroke: var(--on-accent);
  stroke-width: 2;
  stroke-linecap: round;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.icon-button:hover {
  background: var(--surface-2);
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-sun,
[data-theme="dark"] .icon-moon {
  display: none;
}

[data-theme="dark"] .icon-sun {
  display: block;
}

/* ----------------------------------------------------------- controls */

.page {
  flex: 1;
  padding-bottom: 24px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 20px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.select,
.input {
  height: 38px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  max-width: 100%;
}

#layout {
  min-width: 220px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.segmented label {
  position: relative;
  display: block;
}

.segmented input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}

.segmented span {
  display: block;
  padding: 5px 14px;
  border-radius: 7px;
  font-weight: 550;
  color: var(--muted);
  white-space: nowrap;
}

.segmented input:checked + span {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12), 0 0 0 1px var(--border);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.button {
  height: 38px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.button:hover:not(:disabled) {
  background: var(--surface-2);
}

.button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.button-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--on-accent);
}

.button-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.button-done {
  margin-left: auto;
  min-width: 110px;
  border-color: transparent;
  background: var(--done);
  color: var(--on-done);
}

.button-done:hover:not(:disabled) {
  background: var(--done-hover);
}

.button-small {
  height: 32px;
  padding: 0 12px;
  font-size: 14px;
}

/* -------------------------------------------------------- intro, hint */

.intro {
  max-width: 640px;
  padding: 40px 4px 24px;
}

.intro h1 {
  margin: 0 0 8px;
  font-size: clamp(24px, 4vw, 32px);
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.intro p {
  margin: 0;
  color: var(--muted);
  font-size: 16px;
}

.hint {
  margin: 12px 4px;
  color: var(--muted);
  font-size: 14px;
}

/* ------------------------------------------------------------- result */

.result {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 24px;
  margin-top: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.score {
  margin: 0;
  font-size: 18px;
}

.score strong {
  font-size: 24px;
  letter-spacing: -0.01em;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
  color: var(--muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: var(--st-bg);
  box-shadow: inset 0 0 0 1.5px var(--st);
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

/* ----------------------------------------------------------- keyboard */

.stage {
  margin-top: 16px;
}

.kb-scroll {
  container-type: inline-size;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 6px;
}

.keyboard {
  --u: clamp(30px, calc(100cqw / 23), 54px);
  position: relative;
  width: calc(var(--u) * 23);
  height: calc(var(--u) * 7);
  margin-inline: auto;
  background: var(--case);
  border: 1px solid var(--border);
  border-radius: calc(var(--u) * 0.28);
}

.key {
  --cap-bg: var(--key-bg);
  --cap-border: var(--key-border);
  position: absolute;
  left: calc((var(--x) + 0.25) * var(--u));
  top: calc((var(--y) + 0.25) * var(--u));
  width: calc(var(--w) * var(--u));
  height: calc(var(--h) * var(--u));
  margin: 0;
  padding: calc(var(--u) * 0.035);
  border: 0;
  background: none;
  color: var(--key-text);
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.cap {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  background: var(--cap-bg);
  border: 1px solid var(--cap-border);
  border-radius: calc(var(--u) * 0.1);
  box-shadow: 0 calc(var(--u) * 0.05) 0 var(--key-edge);
  transition: background-color 0.12s, border-color 0.12s;
}

.keyboard[data-mode="edit"] .key:hover {
  --cap-bg: var(--key-bg-hover);
}

.keyboard[data-mode="result"] .key {
  cursor: default;
}

.key.is-editing {
  --cap-bg: var(--accent-soft);
  --cap-border: var(--accent);
}

.key:focus-visible {
  outline: none;
}

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

/* ISO Enter: one key across two rows */
.key--iso-enter {
  clip-path: polygon(0 0, 100% 0, 100% 100%, 16.667% 100%, 16.667% 50%, 0 50%);
}

.key--iso-enter .cap {
  background: none;
  border: 0;
  box-shadow: none;
}

.key--iso-enter:focus-visible .cap {
  outline: none;
}

.cap-shape {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.cap-shape .face {
  fill: var(--cap-bg);
  stroke: var(--cap-border);
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
  transition: fill 0.12s, stroke 0.12s;
}

.cap-shape .edge {
  fill: var(--key-edge);
}

.key--iso-enter:focus-visible .face {
  stroke: var(--focus);
  stroke-width: 2.5px;
}

/* ------------------------------------------------------------ legends */

.legends {
  --pad: calc(var(--u) * 0.08);
  position: absolute;
  inset: 0;
}

.key--iso-enter .legends {
  left: 17.5%;
}

.lg {
  --fs: 0.3;
  position: absolute;
  display: inline-block;
  max-width: calc(50% - var(--pad) * 1.5);
  padding: 0 0.16em;
  border-radius: 0.25em;
  overflow: hidden;
  white-space: nowrap;
  font-size: calc(var(--u) * var(--fs) * var(--fit, 1));
  line-height: 1.2;
}

.lg-shift {
  top: var(--pad);
  left: var(--pad);
}

.lg-base {
  bottom: var(--pad);
  left: var(--pad);
}

.lg-altgr {
  right: var(--pad);
  bottom: var(--pad);
}

.lg-center {
  --fs: 0.36;
  top: 50%;
  left: 50%;
  max-width: calc(100% - var(--pad) * 2);
  max-height: calc(100% - var(--pad) * 2);
  transform: translate(-50%, -50%);
  text-align: center;
}

.lg-center.is-name {
  --fs: 0.21;
  white-space: normal;
  line-height: 1.12;
  overflow-wrap: normal;
}

.lg.is-abbr {
  --fs: 0.17;
  font-weight: 650;
  letter-spacing: 0.02em;
}

.lg.is-placeholder {
  width: 0.95em;
  height: 1.2em;
  padding: 0;
}

.lg.is-placeholder:not(.is-active):not([data-status]) {
  display: none;
}

.keyboard[data-mode="edit"] .lg.is-dim {
  opacity: 0.38;
}

.keyboard[data-mode="edit"] .lg.is-active {
  font-weight: 650;
}

.keyboard[data-mode="edit"] .lg.is-placeholder.is-active {
  outline: 1px dashed var(--muted);
  outline-offset: -1px;
  opacity: 0.6;
}

.lg[data-status] {
  background: var(--st-bg);
  color: var(--st-fg);
  box-shadow: inset 0 0 0 1px var(--st);
}

/* ------------------------------------------------------------- popups */

.popup {
  position: absolute;
  z-index: 20;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(320px, calc(100vw - 16px));
  padding: 12px;
}

.editor-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.editor-title {
  font-weight: 650;
}

.editor-note {
  font-size: 13px;
  color: var(--muted);
}

.editor .input {
  font-size: 17px;
}

.editor-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.editor-keys {
  font-size: 12px;
  color: var(--muted);
}

.editor-buttons {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.tooltip {
  max-width: calc(100vw - 16px);
  padding: 8px 10px;
  font-size: 13px;
  pointer-events: none;
}

.tooltip table {
  border-collapse: collapse;
}

.tooltip th,
.tooltip td {
  padding: 3px 8px;
  text-align: left;
  white-space: nowrap;
}

.tooltip thead th {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.tooltip tbody th {
  color: var(--muted);
  font-weight: 550;
}

.tooltip td {
  font-size: 15px;
}

.tooltip td.is-empty {
  color: var(--muted);
}

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.badge[data-status] {
  background: var(--st-bg);
  color: var(--st-fg);
  box-shadow: inset 0 0 0 1px var(--st);
}

/* ------------------------------------------------------------- dialog */

.dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.dialog::backdrop {
  background: rgb(10 12 16 / 0.45);
}

.dialog p {
  margin: 0 0 18px;
  font-size: 16px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ------------------------------------------------------------- footer */

.site-footer {
  padding-block: 20px 28px;
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 560px) {
  .controls {
    align-items: stretch;
  }

  .field,
  #layout {
    width: 100%;
  }

  .button-done {
    width: 100%;
    margin-left: 0;
  }

  .result-actions {
    margin-left: 0;
  }
}
