:root {
  --accent: #5e2f86;
  --accent-hi: #8452ad;
  --bg: #2a2a2a;
  --bar: #303030;
  --panel: #232323;
  --line: #383838;
  --text: #ededed;
  --muted: #9b9b9b;
  --key-top: #3c3c3c;
  --key-bot: #313131;
  --key-edge: #171717;
  --gap: 7px;
  --radius: 3px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --mono: ui-monospace, "Roboto Mono", "SF Mono", Menlo, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  /* faint grain, like the brushed panel in the reference app */
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 .05 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  color: var(--text);
  font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: var(--sat) var(--sar) 0 var(--sal);
}

/* ---------- top bar ---------- */
.bar {
  flex: 0 0 auto;
  height: 54px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 8px;
  background: var(--bar);
  border-bottom: 1px solid #1c1c1c;
}
.bar .title {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: .06em;
  color: #d6d6d6;
  white-space: nowrap;
  overflow: hidden;
}
.flags { display: inline-flex; gap: 6px; }
.flag {
  font-size: 11px; font-weight: 700; letter-spacing: .04em;
  padding: 2px 6px; border-radius: 4px;
  background: var(--accent); color: #fff;
}
.icon {
  width: 44px; height: 44px; flex: 0 0 44px;
  display: grid; place-items: center;
  border: 0; background: none; border-radius: 10px; cursor: pointer;
}
.icon:active { background: #ffffff14; }
.icon svg {
  width: 24px; height: 24px; fill: none; stroke: #cfcfcf;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.pill {
  height: 36px; padding: 0 14px; border-radius: 18px;
  border: 1px solid #4a4a4a; background: #3a3a3a; cursor: pointer;
  font-size: 14px; font-weight: 600;
}
.pill.solid { background: var(--accent); border-color: var(--accent-hi); color: #fff; }
.pill:active { filter: brightness(1.2); }
.editbar .title { justify-content: center; font-size: 13px; }

/* ---------- body: cover screen = stacked, unfolded = side by side ---------- */
.body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: 4px 6px calc(6px + var(--sab));
}
.display, .editpanel {
  flex: 0 0 33%;
  min-height: 0;
}
.keypad { flex: 1; min-height: 0; }

body.wide .body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  /* keeps the keys off the hinge crease when the Fold is open */
  column-gap: max(28px, 3.2vw);
  padding: 10px 12px calc(12px + var(--sab));
}
body.wide .display, body.wide .editpanel { grid-column: 1; grid-row: 1; }
body.wide .keypad { grid-column: 2; grid-row: 1; }

/* ---------- stack display ---------- */
.display {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 2px solid #171717;
  border-radius: var(--radius);
  overflow: hidden;
}
.lvl {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: stretch;
  border-top: 1px solid var(--line);
}
.lvl:first-child { border-top: 0; }
.lvl .tag {
  flex: 0 0 36px;
  display: grid; place-items: center;
  background: var(--accent);
  color: #f2eaf8;
  font-size: 17px;
  border-top: 1px solid #00000040;
}
.lvl:first-child .tag { border-top: 0; }
.lvl .val {
  flex: 1; min-width: 0;
  display: flex; align-items: center; justify-content: flex-end;
  padding: 0 12px;
  font-size: clamp(16px, 4.6vw, 22px);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  color: #e6e6e6;
}
.lvl.x .val { font-family: var(--mono); font-size: clamp(22px, 7.4vw, 34px); }
.lvl .val.long { font-size: clamp(14px, 3.8vw, 18px); }
.lvl.x .val.long { font-size: clamp(16px, 5.4vw, 26px); }
.lvl.x .val.dim { color: #8a8a8a; }
.lvl .val:active { background: #ffffff0a; }
.caret {
  display: inline-block; width: 2px; height: 1em; margin-left: 3px;
  background: var(--accent-hi); animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

body.wide .lvl .tag { flex-basis: 48px; font-size: 19px; }
body.wide .lvl .val { font-size: 24px; padding: 0 18px; }
body.wide .lvl.x .val { font-size: 40px; }
body.wide .lvl .val.long { font-size: 19px; }
body.wide .lvl.x .val.long { font-size: 28px; }

/* ---------- keypad ---------- */
.keypad {
  display: grid;
  gap: var(--gap);
  touch-action: manipulation;
}
.key {
  position: relative;
  container-type: size;
  min-width: 0; min-height: 0;
  padding: 0;
  display: grid; place-items: center;
  border: 1.5px solid var(--key-edge);
  border-radius: var(--radius);
  background: linear-gradient(var(--key-top), var(--key-bot));
  box-shadow: inset 0 1px 0 #ffffff12, 0 1px 0 #00000055;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 60ms, filter 60ms;
}
.key .lbl {
  --chars: 1;
  display: block;
  line-height: 1;
  white-space: nowrap;
  font-weight: 400;
}
.ico { display: block; width: 1.15em; height: 1.15em; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linejoin: round; stroke-linecap: round; }
.chip .ico { width: 1.3em; height: 1.3em; margin: 0 auto; }
.key .lbl sup { font-size: .62em; vertical-align: .55em; line-height: 0; margin-left: .04em; }
.key.s-xl .lbl { font-size: min(46cqh, 48cqw, calc(150cqw / max(var(--chars), 1.6))); }
.key.s-lg .lbl { font-size: min(38cqh, 40cqw, calc(150cqw / max(var(--chars), 1.6))); }
.key.s-md .lbl { font-size: min(32cqh, 34cqw, calc(160cqw / max(var(--chars), 2))); }
.key.s-sm .lbl { font-size: min(22cqh, 22cqw, calc(150cqw / max(var(--chars), 3))); font-weight: 500; letter-spacing: .02em; }
.key.s-xl .lbl, .key.s-lg .lbl { font-weight: 400; }
.key.f-enter .lbl, .key.f-del .lbl, .key.f-clr .lbl { font-weight: 500; }

.key.c-accent {
  background: linear-gradient(color-mix(in srgb, var(--accent) 88%, white 12%), var(--accent));
  border-color: color-mix(in srgb, var(--accent) 55%, white 45%);
  color: #fff;
}
.key.c-dark { background: linear-gradient(#2a2a2a, #232323); }
.key.c-light { background: linear-gradient(#e2e2e2, #c9c9c9); color: #1b1b1b; border-color: #8d8d8d; }
.key.c-gold { background: linear-gradient(#a8812f, #8a6a24); color: #fff8e6; border-color: #c9a352; }
.key.blank { background: transparent; border-color: transparent; box-shadow: none; cursor: default; }

.key.pressed { transform: scale(.965); filter: brightness(1.28); }

/* edit mode */
body.editing .key { cursor: grab; }
body.editing .key::after {
  content: ""; position: absolute; inset: 3px;
  border: 1.5px dashed #ffffff40; border-radius: 2px; pointer-events: none;
}
body.editing .key.blank { border: 1.5px dashed #ffffff33; }
body.editing .key.blank::before { content: "empty"; font-size: 11px; color: #ffffff55; }
body.editing .keypad { touch-action: none; }
.key.dragging { z-index: 5; opacity: .92; box-shadow: 0 12px 26px #000a; transition: none; }
.key.drop-target { outline: 3px solid var(--accent-hi); outline-offset: 1px; }
.key.just-moved { animation: pop .35s ease; }
@keyframes pop { 40% { transform: scale(1.06); } }

/* ---------- edit panel (replaces the stack while editing) ---------- */
.editpanel {
  overflow-y: auto;
  background: var(--panel);
  border: 2px solid #171717;
  border-radius: var(--radius);
  padding: 14px 14px 16px;
  font-size: 14px;
  color: #cfcfcf;
}
.editpanel p { margin: 0 0 12px; line-height: 1.4; color: #bdbdbd; }
.editpanel .row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.editpanel .row > .lab { color: var(--muted); font-size: 12px; letter-spacing: .06em; text-transform: uppercase; min-width: 70px; }

/* ---------- shared controls ---------- */
.stepper { display: inline-flex; align-items: center; border: 1px solid #4a4a4a; border-radius: 10px; overflow: hidden; }
.stepper button { width: 40px; height: 36px; border: 0; background: #383838; font-size: 20px; cursor: pointer; }
.stepper button:disabled { opacity: .35; }
.stepper button:active { background: #474747; }
.stepper span { min-width: 34px; text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }
.chipset { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-width: 46px; height: 38px; padding: 0 10px;
  border: 1px solid #484848; border-radius: 8px; background: #333;
  font-size: 15px; cursor: pointer; white-space: nowrap;
}
.chip sup { font-size: .65em; }
.chip.on { background: var(--accent); border-color: var(--accent-hi); color: #fff; }
.chip:active { filter: brightness(1.2); }
.seg { display: inline-flex; border: 1px solid #484848; border-radius: 10px; overflow: hidden; }
.seg button { height: 36px; padding: 0 14px; border: 0; background: #333; cursor: pointer; font-size: 14px; }
.seg button + button { border-left: 1px solid #484848; }
.seg button.on { background: var(--accent); color: #fff; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatch {
  width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
  border: 2px solid #ffffff22; display: grid; place-items: center; font-size: 12px;
}
.swatch.on { outline: 3px solid #fff; outline-offset: 2px; }
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  height: 42px; padding: 0 16px; border-radius: 10px; cursor: pointer;
  border: 1px solid #4a4a4a; background: #383838; font-size: 15px; font-weight: 500;
}
.btn:active { filter: brightness(1.2); }
.btn.danger { color: #ff9b9b; border-color: #6b3a3a; background: #3a2a2a; }
.btn.primary { background: var(--accent); border-color: var(--accent-hi); color: #fff; }
.btn.grow { flex: 1; }
input[type=text], textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: #1c1c1c; border: 1px solid #4a4a4a; border-radius: 10px; padding: 10px 12px;
  -webkit-user-select: text; user-select: text;
}
textarea { min-height: 120px; font-family: var(--mono); font-size: 12px; }
input:focus, textarea:focus { outline: 2px solid var(--accent-hi); outline-offset: 0; }

/* ---------- bottom sheet ---------- */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; }
.scrim { position: absolute; inset: 0; background: #0009; opacity: 0; transition: opacity .2s; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 86dvh;
  display: flex; flex-direction: column;
  background: #262626;
  border-top: 1px solid #444;
  border-radius: 18px 18px 0 0;
  padding-bottom: var(--sab);
  transform: translateY(100%);
  transition: transform .22s cubic-bezier(.2,.8,.2,1);
}
.sheet-wrap.open .scrim { opacity: 1; }
.sheet-wrap.open .sheet { transform: none; }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: #555; margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; padding: 4px 8px 4px 18px; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 18px; font-weight: 600; }
.sheet-body { overflow-y: auto; padding: 4px 18px 22px; overscroll-behavior: contain; }
.sec { margin: 14px 0 0; }
.sec > h3 {
  margin: 0 0 8px; font-size: 12px; font-weight: 600; color: var(--muted);
  letter-spacing: .08em; text-transform: uppercase;
}
.sec .hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; line-height: 1.4; }
.grp-name { font-size: 12px; color: #8d8d8d; margin: 10px 0 6px; }
.grp-name:first-child { margin-top: 0; }
.setrow { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; border-bottom: 1px solid #333; }
.setrow .name { font-size: 15px; }
.setrow .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.switch { width: 50px; height: 30px; border-radius: 15px; border: 0; background: #4a4a4a; position: relative; cursor: pointer; flex: 0 0 50px; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #ddd; transition: left .15s; }
.switch.on { background: var(--accent); }
.switch.on::after { left: 23px; background: #fff; }

.preview-key { width: 92px; height: 74px; margin: 2px auto 4px; }

.hist { list-style: none; margin: 0; padding: 0; }
.hist li { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; padding: 10px 4px; border-bottom: 1px solid #333; cursor: pointer; }
.hist li:active { background: #ffffff0a; }
.hist .expr { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.hist .res { font-size: 20px; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); text-align: center; padding: 28px 0; }

body.wide .sheet {
  left: 50%; right: auto; bottom: 50%;
  width: min(560px, 92vw);
  max-height: 84dvh;
  border-radius: 18px; border: 1px solid #444;
  transform: translate(-50%, 60%); opacity: 0;
  transition: transform .2s, opacity .2s;
}
body.wide .sheet-wrap.open .sheet { transform: translate(-50%, 50%); opacity: 1; }
body.wide .sheet-grip { display: none; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; top: calc(var(--sat) + 64px);
  transform: translate(-50%, -10px); opacity: 0; pointer-events: none;
  background: #111; border: 1px solid #555; color: #fff;
  padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw; text-align: center;
  transition: opacity .18s, transform .18s; z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { border-color: #8a3b3b; background: #2a1414; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
