* { box-sizing: border-box; scrollbar-width: none; -webkit-tap-highlight-color: transparent; }
::-webkit-scrollbar { display: none; }
html, body { margin: 0; background: var(--page); overscroll-behavior: none; }
body { min-height: 100dvh; display: flex; flex-wrap: wrap; gap: 20px; align-content: center; align-items: stretch; justify-content: center; padding: 16px; padding-top: max(16px, env(safe-area-inset-top)); padding-bottom: max(16px, env(safe-area-inset-bottom)); font-family: "IBM Plex Sans", system-ui, sans-serif; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.calc { align-self: center; width: 100%; max-width: 380px; background: var(--bg); color: var(--ink); border-radius: 36px; padding: 18px 14px 22px; user-select: none; -webkit-user-select: none; box-shadow: 0 0 0 1px var(--lcd-line); }

/* 화면: 상태 표시 · 설정 · 입력값 · 알림 · 결과 */
.lcd { background: var(--lcd); border: 1px solid var(--lcd-line); border-radius: 16px; padding: 10px 14px 12px; margin-bottom: 12px; font-family: "IBM Plex Mono", monospace; }
.lcd.off > * { visibility: hidden; }
.ind { display: flex; justify-content: space-between; font-size: 9px; font-weight: 500; color: var(--dim); }
.ind .on { color: var(--lit); }
.set { display: flex; justify-content: space-between; gap: 4px; width: 100%; font: 10.5px "IBM Plex Mono", monospace; color: var(--sub); margin: 6px 0 8px; padding: 0 0 6px; border: 0; border-bottom: 1px solid var(--lcd-line); background: none; cursor: pointer; text-align: left; }
.set .mode { color: var(--ink); font-weight: 500; }
.set .changed { color: var(--warn); font-weight: 500; }
.set:focus-visible { outline: 2px solid var(--lit); outline-offset: 2px; border-radius: 4px; }
.vars { min-height: 97px; }
.vars div { display: flex; justify-content: space-between; font-size: 12px; color: var(--sub); padding: 1.5px 0; }
.vars div span:last-child { color: var(--ink); }
.vars .cur span:first-child { color: var(--lit); font-weight: 500; }
.vars .stale span:last-child { color: var(--sub); opacity: .7; }
.vars .stale span:last-child::after { content: " old"; font-size: 9px; }
.vars em, .res em { font-style: normal; color: var(--lit); }
.note { min-height: 16px; margin-top: 6px; font: 500 10.5px/1.45 "IBM Plex Sans", sans-serif; color: var(--lit); transition: opacity .3s; }
.note.error { color: var(--err); }
.note.hide { opacity: 0; }
.res { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-top: 4px; padding-top: 6px; border-top: 1px solid var(--lcd-line); cursor: default; touch-action: none; }
.res span { font-size: 12px; color: var(--sub); white-space: nowrap; }
.res b { font: 500 32px var(--big), monospace; letter-spacing: -.02em; white-space: nowrap; overflow: hidden; }
.res em { font-size: 14px; }
.res.full b { font-size: 22px; color: var(--lit); }

.keys { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.k { position: relative; border: 0; border-radius: 12px; height: 46px; font: 500 14px "IBM Plex Sans", sans-serif; cursor: pointer; padding-top: 8px; background: var(--key); color: var(--ink); transition: filter .1s, transform .1s; touch-action: manipulation; }
.k:active, .k.flash { filter: brightness(1.25); transform: scale(.96); }
.k:focus-visible { outline: 2px solid var(--lit); outline-offset: 2px; }
.k small { position: absolute; top: 3px; left: 0; right: 0; font-size: 9px; color: var(--second); }
.k.n { background: var(--num); font-size: 18px; box-shadow: inset 0 0 0 1px var(--lcd-line); }
.k.t { background: var(--tvm-bg); color: var(--tvm-ink); box-shadow: inset 0 0 0 1px var(--tvm-line); }
.k.t small { color: var(--tvm-ink); opacity: .75; }
.k.c { background: var(--hot); color: var(--hot-ink); font-weight: 600; }
.k.c small { color: var(--hot-ink); opacity: .8; }
.k.s { background: var(--shift); color: var(--shift-ink); font-weight: 600; }
.k.tall { grid-row: span 2; height: auto; font-size: 20px; }
.calc.shift .k.s { box-shadow: 0 0 0 2px var(--ink); }

/* 옆 패널: LOG · REG · SET */
.side { position: relative; width: 100%; max-width: 380px; align-self: stretch; display: flex; flex-direction: column; background: var(--bg); color: var(--ink); border-radius: 24px; padding: 14px 16px 16px; box-shadow: 0 0 0 1px var(--lcd-line); }
@media (min-width: 820px) { .side { max-width: 330px; } .pane { position: absolute; inset: 58px 16px 16px; display: flex; flex-direction: column; } }
@media (max-width: 819px) { #log { max-height: 55dvh; } } /* 폰에서는 REG·SET은 페이지와 함께 스크롤 */
.tabs { display: flex; gap: 4px; margin-bottom: 12px; padding: 3px; border-radius: 10px; background: var(--lcd); border: 1px solid var(--lcd-line); }
.tabs button { flex: 1; border: 0; background: none; border-radius: 7px; padding: 6px 0; min-width: 0; font: 600 11.5px "IBM Plex Sans", sans-serif; color: var(--sub); cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--key); color: var(--ink); }
.tabs .guide { flex: 1; text-align: center; border-radius: 7px; padding: 6px 0; font: 600 11.5px "IBM Plex Sans", sans-serif; color: var(--lit); text-decoration: none; }
.tabs .guide:hover { background: var(--key); }
.pane { display: flex; flex-direction: column; min-height: 0; }
.tools { display: flex; justify-content: flex-end; gap: 6px; margin-bottom: 8px; }
.side .btn, .tools button { border: 1px solid var(--lcd-line); background: transparent; color: var(--sub); border-radius: 8px; padding: 4px 10px; font: 500 12px "IBM Plex Sans", sans-serif; cursor: pointer; }
.side .btn:hover, .tools button:hover { color: var(--ink); }
.side .btn:disabled { opacity: .4; cursor: default; color: var(--sub); }
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 120px; font-family: "IBM Plex Mono", monospace; }

#log .lk { color: var(--sub); word-break: break-word; }
#log .out { color: var(--ink); word-break: break-word; }
#log .out.cpt { color: var(--lit); }
#log .out.err { color: var(--err); }
#log li { padding: 6px 0; border-bottom: 1px solid var(--lcd-line); font-size: 11.5px; line-height: 1.45; }
#log li.live .lk { opacity: .6; }
.empty { color: var(--sub); font: 12px "IBM Plex Sans", sans-serif; padding: 8px 0; }

#reg h3, #settings h3 { font: 600 11px "IBM Plex Sans", sans-serif; color: var(--sub); letter-spacing: .04em; margin: 14px 0 6px; }
#reg h3:first-child, #settings h3:first-child { margin-top: 0; }
#reg .row { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; padding: 2px 0; }
#reg .row span:first-child { color: var(--sub); }
#reg .row.stale span:last-child { opacity: .6; }
#reg .row em { font-style: normal; color: var(--lit); }
#reg .grid { display: grid; grid-template-columns: 1fr 1fr; column-gap: 16px; }

#settings .opt { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--lcd-line); font: 12px "IBM Plex Sans", sans-serif; }
#settings .opt > span { display: flex; flex-direction: column; }
#settings .opt small { color: var(--sub); font-size: 10.5px; }
#settings .opt.changed > span b { color: var(--warn); }
#settings b { font-weight: 600; }
#settings input { width: 72px; padding: 5px 8px; border-radius: 8px; border: 1px solid var(--lcd-line); background: var(--lcd); color: var(--ink); font: 12px "IBM Plex Mono", monospace; text-align: right; }
#settings input:focus { outline: 2px solid var(--lit); outline-offset: 1px; }
.seg { display: inline-flex; padding: 2px; border-radius: 8px; background: var(--lcd); border: 1px solid var(--lcd-line); flex-wrap: wrap; justify-content: flex-end; }
.seg button { border: 0; background: none; border-radius: 6px; padding: 4px 8px; font: 500 11.5px "IBM Plex Sans", sans-serif; color: var(--sub); cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--key); color: var(--ink); }
#settings .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
#settings .hint { color: var(--sub); font: 11px/1.5 "IBM Plex Sans", sans-serif; margin: 8px 0 0; }

/* 그래프 */
.chartpick { align-self: stretch; margin-bottom: 10px; }
.chartpick button { flex: 1; }
.chart { position: relative; font-family: "IBM Plex Sans", sans-serif; }
.chart h4 { margin: 0 0 6px; font: 600 12px "IBM Plex Sans", sans-serif; color: var(--ink); }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--lcd-line); stroke-width: 1; }
.chart .grid line.zero { stroke: var(--sub); opacity: .6; }
.chart .grid text, .chart .endl, .chart .refl { font: 9.5px "IBM Plex Mono", monospace; fill: var(--sub); }
.chart .endl { fill: var(--ink); }
.chart .ref { stroke: var(--sub); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .cross { stroke: var(--sub); stroke-width: 1; }
.chart .dot, .chart .pt { stroke: var(--bg); stroke-width: 2; }
.chart .bar.on, .chart .pt.on { filter: brightness(1.2); }
.chart rect[tabindex]:focus, .chart circle[tabindex]:focus { outline: none; stroke: var(--ink); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 0 0 6px; font: 11px "IBM Plex Sans", sans-serif; color: var(--sub); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; }
.legend i.line { height: 2px; width: 12px; border-radius: 1px; }
.legend i.dot { border-radius: 50%; width: 8px; height: 8px; }
.tip { position: absolute; z-index: 2; pointer-events: none; background: var(--lcd); border: 1px solid var(--lcd-line); border-radius: 8px; padding: 6px 9px; font: 11px "IBM Plex Sans", sans-serif; color: var(--sub); box-shadow: 0 4px 14px rgba(0,0,0,.25); white-space: nowrap; }
.tip .tt { color: var(--sub); margin-bottom: 2px; }
.tip .tr { display: flex; align-items: center; gap: 6px; }
.tip .tr i { width: 10px; height: 2px; border-radius: 1px; }
.tip b { color: var(--ink); font: 600 12px "IBM Plex Mono", monospace; }
.tbl { margin-top: 10px; font: 11px "IBM Plex Sans", sans-serif; color: var(--sub); }
.tbl summary { cursor: pointer; }
.tblwrap { overflow-x: auto; margin-top: 6px; }
.tbl table { width: 100%; border-collapse: collapse; font: 11px "IBM Plex Mono", monospace; color: var(--ink); }
.tbl th { text-align: right; color: var(--sub); font-weight: 500; padding: 3px 4px; border-bottom: 1px solid var(--lcd-line); }
.tbl td { text-align: right; padding: 3px 4px; border-bottom: 1px solid var(--lcd-line); }
.tbl th:first-child, .tbl td:first-child { text-align: left; }

/* EXCEL 탭 */
#xls h3 { font: 600 11px "IBM Plex Sans", sans-serif; color: var(--sub); letter-spacing: .04em; margin: 16px 0 6px; }
#xls h3:first-child { margin-top: 0; }
#xls .xrow { display: flex; align-items: center; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--lcd-line); font: 12px "IBM Plex Sans", sans-serif; }
#xls .xrow > span { flex: 1; min-width: 0; }
#xls .xrow small { display: block; color: var(--sub); font-size: 10.5px; }
#xls textarea { width: 100%; min-height: 84px; resize: vertical; padding: 8px; border-radius: 8px; border: 1px solid var(--lcd-line); background: var(--lcd); color: var(--ink); font: 12px "IBM Plex Mono", monospace; }
#xls textarea:focus { outline: 2px solid var(--lit); outline-offset: 1px; }
#xls .row2 { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
#xls .fx { padding: 6px 0; border-bottom: 1px solid var(--lcd-line); }
#xls .fx code { display: block; font: 11px/1.5 "IBM Plex Mono", monospace; color: var(--ink); word-break: break-all; user-select: all; -webkit-user-select: all; }
#xls .fx .h { display: flex; justify-content: space-between; align-items: center; font: 500 11.5px "IBM Plex Sans", sans-serif; color: var(--sub); margin-bottom: 2px; }
#xls .hint { color: var(--sub); font: 11px/1.5 "IBM Plex Sans", sans-serif; margin: 6px 0 0; }

/* ---------- 폰: 계산기(화면+키 45개)가 스크롤 없이 한 화면에 들어오게 ----------
   iPhone SE(375×548, Safari 주소창 포함)부터 Pro Max(440×956)까지. 키 높이는 남은 높이를 9줄로 나눈다.
   화면이 낮으면 입력값 목록을 줄인다: 700px 미만은 두 칸으로 나눠 5개를 3줄 높이에, 600px 미만은 숨김(REG 탭에서 보기) */
@media (max-width: 540px) and (min-height: 480px) {
  body { padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom)); gap: 12px; align-content: flex-start; }
  .calc { display: flex; flex-direction: column; max-width: none; border-radius: 28px; padding: 10px 9px 12px;
          height: calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom))); max-height: 900px; }
  .lcd { margin-bottom: 8px; padding: 8px 12px 10px; }
  .keys { flex: 1; min-height: 0; grid-auto-rows: 1fr; gap: 6px; }
  .k { height: auto; min-height: 34px; }
  .side { max-width: none; }
  #settings input, #xls textarea { font-size: 16px; } /* 16px 미만이면 iPhone이 입력할 때 화면을 확대한다 */
}
@media (max-width: 540px) and (max-height: 700px) and (min-height: 480px) {
  .vars { min-height: 0; display: grid; grid-template-columns: 1fr 1fr; column-gap: 14px; }
  .vars div { font-size: 11px; padding: 1px 0; min-width: 0; }
  .vars div span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .note { margin-top: 4px; }
}
@media (max-width: 540px) and (max-height: 600px) and (min-height: 480px) {
  .vars { display: none; }
  .set { margin-bottom: 4px; }
}
.res b.long { font-size: 24px; }
.calc, .k { -webkit-touch-callout: none; }

@media (prefers-reduced-motion: reduce) { .k, .note { transition: none; } }
