/* SPDX-License-Identifier: 0BSD */
:root {
  --body: #1e2329;
  --case: #2b3138;
  --case-edge: #3a424b;
  --text: #e9edf1;
  --muted: #9aa5b1;
  --second: #e8c547;
  --alpha: #b9a3ff;
  --key: #4a535d;
  --key-hi: #59636e;
  --key-num: #d9dde1;
  --key-num-text: #1d2227;
  --key-fn: #37546e;
  --key-2nd: #c9a92e;
  --key-dia: #3f8a5c;
  --key-alpha: #6a58b8;
  --key-enter: #2f6fa8;
  --accent: #5aa9ff;
  --danger: #e2574c;
  --gap: clamp(4px, 1.2vmin, 8px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--body);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body { display: flex; justify-content: center; }

.calc {
  width: 100%;
  max-width: 460px;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: calc(env(safe-area-inset-top) + 6px) 12px calc(env(safe-area-inset-bottom) + 10px);
  background: linear-gradient(180deg, var(--case) 0%, #252a30 100%);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

@media (min-width: 520px) {
  .calc { border-left: 1px solid var(--case-edge); border-right: 1px solid var(--case-edge); }
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 32px;
}
.brand { font-weight: 700; letter-spacing: .04em; color: var(--muted); }
.brand small { font-weight: 500; margin-left: .4em; }
.icon-btn {
  background: none; border: 0; color: var(--muted);
  font-size: 22px; width: 40px; height: 32px; border-radius: 8px; cursor: pointer;
}
.icon-btn:hover { background: var(--case-edge); }

.bezel {
  background: #14181c;
  border-radius: 10px;
  padding: clamp(6px, 2vmin, 12px);
  box-shadow: inset 0 2px 6px rgba(0,0,0,.6);
  flex: none;
}
#lcd {
  display: block;
  width: 100%;
  aspect-ratio: 160 / 100;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 3px;
  background: #b0bca0;
}

/* ---- keypad ------------------------------------------------------------- */
.keypad {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: 0.85fr repeat(9, 1fr);
  gap: var(--gap);
}

.topblock {
  grid-column: 1 / span 3;
  grid-row: 2 / span 2;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: var(--gap);
}

.dpad {
  grid-column: 4 / span 2;
  grid-row: 2 / span 2;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 2px;
  background: #20252a;
  border-radius: 50%;
  padding: 4px;
  height: 100%;
  aspect-ratio: 1;
  justify-self: center;
  min-height: 0;
}
.arrow-up { grid-area: 1 / 2; }
.arrow-left { grid-area: 2 / 1; }
.arrow-right { grid-area: 2 / 3; }
.arrow-down { grid-area: 3 / 2; }
.key.arrow { background: var(--key-fn); border-radius: 30%; font-size: clamp(12px, 3.4vmin, 18px); padding: 0; }

.key {
  position: relative;
  border: 0;
  border-radius: 8px;
  background: var(--key);
  color: var(--text);
  font: inherit;
  font-size: clamp(11px, 3.6vmin, 17px);
  font-weight: 600;
  padding: 0 2px;
  min-width: 0;
  min-height: 0;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
  transition: opacity .12s, background-color .12s;
}
.key:active, .key.down { transform: translateY(1px); box-shadow: 0 1px 0 rgba(0,0,0,.45); filter: brightness(1.2); }
.key .kl { line-height: 1; white-space: nowrap; }
.key.long .kl { font-size: .8em; }
.key.xlong .kl { font-size: .66em; }

.key.fn { background: var(--key-fn); border-radius: 6px 6px 12px 12px; }
.key[data-key="esc"], .key[data-key="apps"] { background: var(--key-fn); }
.key.num { background: var(--key-num); color: var(--key-num-text); }
.key.mod-2nd { background: var(--key-2nd); color: #1d1a10; }
.key.mod-dia { background: var(--key-dia); }
.key.mod-alpha { background: var(--key-alpha); }
.key.enter { background: var(--key-enter); }
.key.on { background: #3b4148; }

/* Active modifier: glowing ring; alpha-lock: double ring */
.key.mod.active { box-shadow: 0 0 0 2px var(--text), 0 0 12px 2px rgba(255,255,255,.35); }
.key.mod-alpha.locked { box-shadow: 0 0 0 2px var(--alpha), 0 0 0 4px var(--text); }

/* Labels take the colour of the active modifier */
.keypad[data-mode="2nd"] .key:not(.mod):not(.arrow) { color: var(--second); }
.keypad[data-mode="2nd"] .key.num:not(.mod):not(.arrow) { color: #8a6d00; }
.keypad[data-mode="diamond"] .key:not(.mod):not(.arrow) { color: #8fd9a8; }
.keypad[data-mode="diamond"] .key.num:not(.mod):not(.arrow) { color: #237a45; }
.keypad[data-mode="alpha"] .key:not(.mod):not(.arrow),
.keypad[data-mode="shift"] .key:not(.mod):not(.arrow) { color: var(--alpha); }
.keypad[data-mode="alpha"] .key.num:not(.mod):not(.arrow),
.keypad[data-mode="shift"] .key.num:not(.mod):not(.arrow) { color: #5b45b3; }

/* Keys that keep their normal function in the current mode */
.keypad[data-mode] .key.plain:not(.mod):not(.arrow) { color: var(--text); }
.keypad[data-mode] .key.num.plain:not(.mod):not(.arrow) { color: var(--key-num-text); }

/* Keys with no function in the current mode */
.key.na { opacity: .35; }
.key.na .kl { color: var(--text); }
.key.num.na .kl { color: var(--key-num-text); }

/* ---- loader ------------------------------------------------------------- */
.loader {
  position: fixed;
  inset: 0;
  background: rgba(16, 19, 23, .96);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
.loader[hidden] { display: none; }
.loader.drag .loader-card { outline: 2px dashed var(--accent); outline-offset: 6px; }
.loader-card {
  max-width: 420px;
  background: var(--case);
  border: 1px solid var(--case-edge);
  border-radius: 14px;
  padding: 24px;
  line-height: 1.5;
}
.loader-card h1 { margin: 0 0 8px; font-size: 24px; }
.loader-card a { color: var(--accent); }
.file-btn {
  display: inline-block;
  margin: 8px 0;
  padding: 12px 18px;
  background: var(--accent);
  color: #06213d;
  font-weight: 700;
  border-radius: 10px;
  cursor: pointer;
}
.file-btn input { display: none; }
.hint { color: var(--muted); font-size: 14px; }
.error { color: var(--danger); font-weight: 600; min-height: 1.5em; }
code { background: #1b1f24; padding: 1px 5px; border-radius: 4px; }

/* ---- menu --------------------------------------------------------------- */
.menu {
  background: var(--case);
  color: var(--text);
  border: 1px solid var(--case-edge);
  border-radius: 14px;
  padding: 20px;
  width: min(360px, calc(100vw - 32px));
}
.menu::backdrop { background: rgba(0,0,0,.55); }
.menu h2 { margin: 0 0 12px; font-size: 18px; }
.menu button, .menu select {
  font: inherit;
  color: var(--text);
  background: var(--key);
  border: 0;
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}
.menu > button { display: block; width: 100%; margin-bottom: 8px; text-align: left; }
.menu > button:hover { background: var(--key-hi); }
.menu .danger { color: #ffb4ad; }
.menu .secondary { text-align: center; margin-top: 12px; margin-bottom: 0; }
.menu .row { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.menu .source { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.menu .source a { color: var(--accent); }
.menu details { margin: 8px 0; color: var(--muted); font-size: 14px; }
.menu summary { cursor: pointer; padding: 6px 0; }
.shortcuts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 8px 0 0; }
.shortcuts dt { color: var(--text); font-family: ui-monospace, monospace; font-size: 12px; }
.shortcuts dd { margin: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translate(-50%, 20px);
  background: #0f1215;
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 20;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
