:root {
  --bg: #14110f;
  --bg2: #221d19;
  --ink: #efe6da;
  --dim: #a99c8c;
  --key: #e9e1d2;
  --keyink: #2b2520;
  --keyedge: #b9ad99;
  --accent: #ffb347;
  --mono: "SF Mono", ui-monospace, Menlo, "Apple SD Gothic Neo", monospace;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); overflow: hidden;
  font-family: -apple-system, "Apple SD Gothic Neo", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; overscroll-behavior: none; touch-action: manipulation; }
button { font: inherit; color: inherit; }

/* ---------- 큰 틀: 기본(세로) = 위 컴퓨터, 아래 키보드 ---------- */
.app { position: fixed; inset: 0; display: grid;
  grid-template-rows: minmax(0, 1fr) 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: radial-gradient(120% 70% at 50% 30%, var(--bg2), var(--bg) 70%); }
.stage { position: relative; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; overflow: hidden; touch-action: pan-y; }
.rigwrap { position: relative; flex: 1; width: 100%; min-height: 0; display: grid; place-items: center; }
.hinge { min-width: 0; min-height: 0; }
.deck { padding: 8px 8px 10px; }

/* 가로: 왼쪽 컴퓨터, 오른쪽 키보드 */
@media (orientation: landscape) and (min-width: 600px) {
  .app { grid-template-rows: 1fr; grid-template-columns: minmax(0, 1.35fr) 0 minmax(0, 1fr); }
  .deck { align-self: center; }
}
/* 좁은 가로 높이(폰 가로)에선 키보드 접어 두고 버튼으로 */
@media (orientation: landscape) and (max-height: 430px) {
  .app { grid-template-columns: minmax(0, 1fr) 0 auto; }
  .deck { width: min(46vw, 420px); }
  .deck .k { min-height: 26px; font-size: 12px; }
}

/* ---------- 접은 자세: 화면이 위·아래 둘로 나뉨 ---------- */
@media (vertical-viewport-segments: 2) {
  .app { padding: 0; grid-template-columns: none;
    grid-template-rows: env(viewport-segment-height 0 0, 50vh)
      calc(env(viewport-segment-top 0 1, 50vh) - env(viewport-segment-bottom 0 0, 50vh))
      minmax(0, 1fr); }
  .stage { padding-top: env(safe-area-inset-top); }
  .deck { display: flex; flex-direction: column; justify-content: center; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .hinge { background: #000; }
}
@media (device-posture: folded) and (orientation: portrait) {
  .app:not(.flat) { grid-template-rows: 1fr 0 1fr; }
  .app:not(.flat) .deck { display: flex; flex-direction: column; justify-content: center; }
}
/* JS가 접힘을 감지했거나 설정에서 '항상 반반'을 고른 경우 */
.app.folded { grid-template-columns: none !important; grid-template-rows: var(--top, 50%) var(--gap, 0px) minmax(0, 1fr) !important; padding-top: 0; }
.app.folded .stage { padding-top: env(safe-area-inset-top); }
.app.folded .hinge { background: linear-gradient(#000, #1b1714 40%, #000); }
.app.folded .deck { display: flex; flex-direction: column; justify-content: center; }
.app.folded .deck .k { min-height: 40px; }

/* ---------- 기종 이름·점 ---------- */
.nav { display: flex; align-items: center; gap: 10px; padding: 2px 0 2px; }
.navbtn { width: 36px; height: 36px; border-radius: 50%; border: 1px solid #ffffff22; background: #ffffff0d; font-size: 22px; line-height: 1; cursor: pointer; }
.navbtn:active { background: #ffffff22; }
.name { min-width: 170px; text-align: center; font-size: 14px; letter-spacing: .02em; color: var(--ink); }
.name small { display: block; font-size: 11px; color: var(--dim); }
.dots { display: flex; gap: 6px; padding-bottom: 6px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: #ffffff30; }
.dots i.on { background: var(--accent); }

/* ---------- 키보드 ---------- */
.kb { display: grid; gap: 5px; max-width: 560px; margin: 0 auto; width: 100%; }
.row { display: flex; gap: 5px; }
.k { flex: 1 1 0; min-width: 0; min-height: 36px; border: 0; border-radius: 6px; cursor: pointer;
  background: linear-gradient(var(--key), color-mix(in srgb, var(--key) 88%, #000));
  color: var(--keyink); font-family: var(--mono); font-size: 14px; font-weight: 600;
  box-shadow: 0 3px 0 var(--keyedge), 0 4px 6px #0006; transition: transform .04s; padding: 0; }
.k.w15 { flex-grow: 1.5; } .k.w2 { flex-grow: 2; } .k.w5 { flex-grow: 5; }
.k.fn { background: linear-gradient(color-mix(in srgb, var(--key) 45%, #2e2a27), color-mix(in srgb, var(--key) 38%, #1c1a18)); color: #f6f0e6; font-size: 12px; }
.k.down, .k:active { transform: translateY(3px); box-shadow: 0 0 0 var(--keyedge), 0 1px 2px #0006; }

/* ---------- 기계 공통 ---------- */
.rig { position: absolute; left: 50%; top: 50%; transform-origin: 50% 50%; filter: drop-shadow(0 18px 18px #0009); transition: opacity .25s; }
.rig.out { opacity: 0; }
.shadow { position: absolute; left: 6%; right: 6%; bottom: -14px; height: 22px; border-radius: 50%; background: radial-gradient(#000a, transparent 70%); filter: blur(4px); }
.lbl { position: absolute; font: 700 9px/1 var(--mono); letter-spacing: .12em; opacity: .7; }
.led { position: absolute; width: 6px; height: 6px; border-radius: 50%; background: #3cff6e; box-shadow: 0 0 6px #3cff6e; animation: blink 3.2s infinite; }
@keyframes blink { 0%, 92% { opacity: 1 } 95% { opacity: .3 } }
.keys { position: absolute; border-radius: 4px;
  background:
    repeating-linear-gradient(90deg, transparent 0 1px, var(--gap, #0003) 1px 2px, transparent 2px 15px),
    repeating-linear-gradient(0deg, transparent 0 1px, var(--gap, #0003) 1px 2px, transparent 2px 9px),
    var(--kc, #ddd); }

/* 화면 */
.scr { position: absolute; overflow: hidden; background: var(--sbg); color: var(--sfg);
  font-family: var(--mono); font-size: calc(var(--sw) / 24); line-height: 1.32; }
.scr .inner { position: absolute; inset: 7% 6%; display: flex; flex-direction: column; }
.scr.crt { border-radius: 10% / 12%; box-shadow: inset 0 0 18px #000c, inset 0 0 3px #000; }
.scr.crt .inner { text-shadow: 0 0 4px color-mix(in srgb, var(--sfg) 70%, transparent); }
.scr.crt::after { content: ""; position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(0deg, #0000 0 2px, #00000059 2px 3px), radial-gradient(ellipse at center, #fff1 0, #0000 55%, #0008 100%);
  animation: flick 4s infinite steps(1); }
.scr.lcd::after { content: ""; position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(90deg, #0000 0 2px, #0000001a 2px 3px), repeating-linear-gradient(0deg, #0000 0 2px, #0000001a 2px 3px); }
.noscan .scr::after { display: none; }
@keyframes flick { 0% { opacity: .95 } 37% { opacity: 1 } 38% { opacity: .88 } 39% { opacity: 1 } }
.scr.crt.on { animation: poweron .5s ease-out; }
@keyframes poweron { 0% { transform: scaleY(.02); filter: brightness(4) } 60% { transform: scaleY(1.04) } 100% { transform: none; filter: none } }

/* 화면 속 앱 */
.cur::after { content: "█"; animation: cur 1s steps(1) infinite; margin-left: 1px; }
@keyframes cur { 50% { opacity: 0 } }
.title { display: flex; justify-content: space-between; border-bottom: 1px solid currentColor; padding-bottom: 2%; margin-bottom: 4%; opacity: .9; }
.menu { list-style: none; margin: 0; padding: 0; }
.menu li { padding: 1.5% 3%; cursor: pointer; }
.menu li.sel { background: var(--sfg); color: var(--sbg); text-shadow: none; }
.big { font-size: 2.6em; text-align: center; letter-spacing: .04em; margin-top: 8%; font-variant-numeric: tabular-nums; }
.mid { text-align: center; margin-top: 3%; }
.foot { margin-top: auto; opacity: .7; font-size: .85em; }
.memo { flex: 1; width: 100%; background: transparent; border: 0; outline: 0; resize: none; color: inherit; font: inherit; line-height: inherit; padding: 0; caret-color: var(--sfg); -webkit-user-select: text; user-select: text; text-shadow: inherit; }
canvas.play { width: 100%; flex: 1; min-height: 0; image-rendering: pixelated; }
.boot { white-space: pre-wrap; }
