:root {
  --bg: #f6f1ea;
  --surface: #fffdf9;
  --ink: #2a241d;
  --muted: #8a7f72;
  --line: #e6ddd0;
  --accent: #e8772e;
  --accent-ink: #ffffff;
  --accent-soft: #fbe3d1;
  --ok: #2f9e6b;
  --warn: #d9822b;
  --bad: #d2483c;
  --house: #d9cdbd;
  --inside: #3a3229;
  --panel: #f0e7da;
  --panel-edge: #cbbca8;
  --key: #fffdf9;
  --key-press: #f1e8dc;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 6px 18px rgba(60, 40, 20, .07);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16130f;
    --surface: #211d18;
    --ink: #f3ece2;
    --muted: #a39788;
    --line: #2f2922;
    --accent: #f08a43;
    --accent-ink: #1b140d;
    --accent-soft: #3a2616;
    --house: #3a332b;
    --inside: #0b0907;
    --panel: #5a4f43;
    --panel-edge: #463d33;
    --key: #221e19;
    --key-press: #2e2822;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

.app {
  min-height: 100%;
  max-width: 420px;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ---- door illustration ------------------------------------------------ */

.door-wrap { width: 150px; margin: 8px 0 18px; }
.app[data-view="control"] .door-wrap { width: 210px; margin: 16px 0 22px; }
.door-wrap { transition: width .35s ease, margin .35s ease; }

.door { display: block; width: 100%; height: auto; }
.door-house { fill: var(--house); }
.door-inside { fill: var(--inside); }
.door-panels rect { fill: var(--panel); stroke: var(--panel-edge); stroke-width: 1.5; }
.door-panels { transition: transform 1.2s cubic-bezier(.5, 0, .3, 1); }
.door-light { fill: var(--panel-edge); transition: fill .3s; }

.app[data-door="open"] .door-panels { transform: translateY(-86px); }
.app[data-door="opening"] .door-panels { transform: translateY(-50px); transition-duration: 30s; }
.app[data-door="closing"] .door-panels { transform: translateY(-36px); transition-duration: 30s; }
.app[data-door="open"] .door-light,
.app[data-door="opening"] .door-light,
.app[data-door="closing"] .door-light { fill: #ffc04d; }
.app[data-door="opening"] .door-light,
.app[data-door="closing"] .door-light { animation: blink 1s steps(2) infinite; }

@keyframes blink { 50% { fill: var(--panel-edge); } }

/* ---- views -------------------------------------------------------------- */

.view { display: none; width: 100%; flex-direction: column; align-items: center; text-align: center; }
.app[data-view="pin"] .view-pin,
.app[data-view="control"] .view-control { display: flex; animation: rise .35s ease both; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

h1 { margin: 0; font-size: 28px; font-weight: 700; letter-spacing: -.01em; }
.sub { margin: 6px 0 0; color: var(--muted); min-height: 1.4em; transition: color .2s; }
.sub.is-error { color: var(--bad); }

/* ---- PIN dots ----------------------------------------------------------- */

.dots { display: flex; gap: 14px; margin: 26px 0 30px; }
.dots span {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--muted);
  transition: background .15s, border-color .15s, transform .15s;
}
.dots span.on { background: var(--accent); border-color: var(--accent); transform: scale(1.1); }
.dots.ok span { background: var(--ok); border-color: var(--ok); }
.dots.shake { animation: shake .45s; }
.dots.shake span { border-color: var(--bad); }
.dots.busy span.on { animation: pulse .8s ease-in-out infinite; }
.dots.busy span:nth-child(2) { animation-delay: .1s; }
.dots.busy span:nth-child(3) { animation-delay: .2s; }
.dots.busy span:nth-child(4) { animation-delay: .3s; }
.dots.busy span:nth-child(5) { animation-delay: .4s; }
.dots.busy span:nth-child(6) { animation-delay: .5s; }

@keyframes shake {
  20% { transform: translateX(-10px); } 40% { transform: translateX(9px); }
  60% { transform: translateX(-6px); } 80% { transform: translateX(4px); }
}
@keyframes pulse { 50% { opacity: .35; } }

/* ---- keypad ------------------------------------------------------------- */

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 78px);
  gap: 16px 24px;
  justify-content: center;
}
.keypad button {
  width: 78px; height: 78px; border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--key);
  box-shadow: var(--shadow);
  font-size: 30px; font-weight: 500;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1;
  transition: background .1s, transform .1s;
  touch-action: manipulation;
  user-select: none;
}
.keypad button small { font-size: 9px; letter-spacing: .15em; color: var(--muted); margin-top: 3px; font-weight: 600; }
.keypad button:active, .keypad button.pressed { background: var(--key-press); transform: scale(.94); }
.keypad .key-back { background: transparent; border-color: transparent; box-shadow: none; color: var(--muted); }
.keypad:disabled, .keypad.disabled { opacity: .45; pointer-events: none; }
.keypad button:focus-visible, .big:focus-visible, .secondary:focus-visible, .link:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 3px;
}

/* ---- control ------------------------------------------------------------ */

.status {
  display: inline-flex; align-items: center; gap: 8px;
  margin: 10px 0 28px; padding: 6px 14px;
  border-radius: 999px; background: var(--surface); border: 1px solid var(--line);
  font-weight: 600;
}
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.app[data-door="closed"] .status-dot { background: var(--ok); }
.app[data-door="open"] .status-dot { background: var(--warn); }
.app[data-door="opening"] .status-dot,
.app[data-door="closing"] .status-dot { background: var(--warn); animation: pulse 1s infinite; }
.app[data-door="error"] .status-dot,
.app[data-door="unavailable"] .status-dot { background: var(--bad); }

.big {
  width: 100%; min-height: 72px; border: 0; border-radius: 22px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 21px; font-weight: 700;
  box-shadow: 0 8px 22px rgba(232, 119, 46, .32);
  transition: transform .1s, opacity .2s, box-shadow .2s;
  touch-action: manipulation;
}
.big:active { transform: scale(.98); }
.big:disabled { opacity: .5; box-shadow: none; cursor: default; }
.big.busy { animation: pulse 1s ease-in-out infinite; }

.secondary {
  width: 100%; min-height: 56px; margin-top: 12px;
  border-radius: 18px; border: 1px solid var(--line);
  background: var(--surface); font-size: 17px; font-weight: 600;
}
.secondary:disabled { opacity: .5; cursor: default; }

.foot {
  margin: auto 0 0; padding-top: 32px;
  display: flex; gap: 14px; align-items: center; justify-content: center;
  color: var(--muted); font-size: 14px;
}
.link { background: none; border: 0; padding: 6px 2px; color: var(--accent); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }

/* ---- toast -------------------------------------------------------------- */

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: 14px;
  background: var(--ink); color: var(--bg);
  font-weight: 600; font-size: 15px; text-align: center;
  box-shadow: var(--shadow);
  animation: rise .25s ease both;
}

@media (max-height: 680px) {
  .door-wrap { width: 110px; margin: 0 0 10px; }
  .dots { margin: 18px 0 20px; }
  .keypad { grid-template-columns: repeat(3, 70px); gap: 12px 22px; }
  .keypad button { width: 70px; height: 70px; font-size: 27px; }
}

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