:root {
  --bg: #000;
  --num: #333333;
  --num-active: #737373;
  --fn: #a5a5a5;
  --fn-active: #d9d9d9;
  --op: #ff9f0a;
  --op-active: #fcc78d;
  --text: #fff;
  --muted: #8e8e93;
  --card: #1c1c1e;
  --gap: clamp(10px, 3.6vw, 16px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

/* ---------------- 계산기 ---------------- */
.calc {
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: calc(var(--safe-t) + 16px) var(--gap) calc(var(--safe-b) + 24px);
}

.display-wrap {
  flex: 1;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 0 8px 12px;
  overflow: hidden;
}

.display {
  font-size: min(22vw, 96px);
  font-weight: 300;
  line-height: 1;
  letter-spacing: -0.02em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  transform-origin: right bottom;
}

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.key {
  aspect-ratio: 1;
  border: 0;
  border-radius: 999px;
  font: inherit;
  font-size: min(9vw, 38px);
  font-weight: 400;
  color: var(--text);
  background: var(--num);
  cursor: pointer;
  transition: background-color .25s ease;
  touch-action: manipulation;
}
.key.num:active, .key.num.pressed { background: var(--num-active); transition: none; }

.key.fn { background: var(--fn); color: #000; font-size: min(7.6vw, 32px); }
.key.fn:active, .key.fn.pressed { background: var(--fn-active); transition: none; }

.key.op { background: var(--op); font-size: min(10vw, 42px); }
.key.op:active, .key.op.pressed { background: var(--op-active); transition: none; }
.key.op.selected { background: #fff; color: var(--op); }

.key.zero {
  grid-column: span 2;
  aspect-ratio: auto;
  text-align: left;
  padding-left: min(8.5vw, 36px);
}

/* ---------------- 보관함 ---------------- */
.vault {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: fadeIn .25s ease;
}

.vault-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 16px) 12px calc(var(--safe-l) + 16px);
  background: rgba(0, 0, 0, .82);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid #1f1f1f;
}
.vault-title { flex: 1; min-width: 0; }
.vault-title small { display: block; color: var(--muted); font-size: 13px; }
.vault-title h1 { margin: 2px 0 0; font-size: 22px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-count { color: var(--muted); font-size: 14px; }

.head-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; border-radius: 999px;
  background: #1c1c1e; color: var(--text);
  cursor: pointer;
}
.head-btn:active { background: #3a3a3c; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  padding: 16px calc(var(--safe-r) + 16px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 16px);
  max-width: 960px;
  align-items: start;
  margin: 0 auto;
}

.card {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  text-align: left;
  color: inherit;
  font: inherit;
  background: var(--card);
  border-radius: 14px;
  overflow: hidden;
  cursor: pointer;
}
.card:active { transform: scale(.98); }

.thumb {
  position: relative;
  aspect-ratio: 16 / 10;
  background: #2c2c2e;
  overflow: hidden;
}
.thumb video, .thumb img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}
.thumb .play {
  position: absolute; top: 0; right: 0; bottom: 0; left: 0;
  display: grid; place-items: center;
}
.thumb .play span {
  width: 44px; height: 44px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .55);
  display: grid; place-items: center;
}
.thumb .idx {
  position: absolute; left: 8px; top: 8px;
  font-size: 12px; font-weight: 600;
  padding: 2px 8px; border-radius: 999px;
  background: rgba(0, 0, 0, .6);
}
.meta { padding: 10px 12px 12px; }
.meta strong { display: block; font-size: 15px; font-weight: 600; line-height: 1.3; }
.meta p { margin: 4px 0 0; font-size: 13px; color: var(--muted); line-height: 1.35; }

.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 48px 0; }

/* ---------------- 플레이어 ---------------- */
.player {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: 10;
  background: #000;
  animation: fadeIn .2s ease;
}
/* 영상이 화면 전체를 쓰고, 비율은 유지한 채 안에 맞춘다 (잘리지 않음) */
.stage {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 60px) var(--safe-r) var(--safe-b) var(--safe-l);
}
.stage video, .stage iframe {
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: contain;
  background: #000;
  border: 0;
  display: block;
}
.stage .err { color: var(--muted); text-align: center; padding: 24px; line-height: 1.5; }
.player-bar {
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + 12px) 10px calc(var(--safe-l) + 12px);
  transition: opacity .3s ease;
}
.player-title { font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-shadow: 0 1px 4px rgba(0, 0, 0, .8); }

/* 가로 화면: 제목 줄을 영상 위에 겹쳐 띄우고, 재생 중엔 잠시 뒤 숨김 (화면 터치 시 다시 보임) */
@media (orientation: landscape) {
  .stage { padding-top: var(--safe-t); }
  .player-bar { background: linear-gradient(rgba(0, 0, 0, .65), rgba(0, 0, 0, 0)); padding-bottom: 24px; }
  .player.idle .player-bar { opacity: 0; pointer-events: none; }
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* 가로 화면(태블릿/PC)에서 계산기 크기 제한 */
@media (min-aspect-ratio: 3/4) {
  .calc { max-width: 380px; }
  .key { font-size: 30px; }
  .key.fn { font-size: 24px; }
  .key.op { font-size: 34px; }
  .display { font-size: 72px; }
  .key.zero { padding-left: 30px; }
}

/* 폰 가로 화면: 버튼 크기를 높이에 맞춤 */
@media (orientation: landscape) and (max-height: 540px) {
  .calc { max-width: calc((100dvh - 150px) * .8 + 70px); padding-top: 8px; padding-bottom: calc(var(--safe-b) + 10px); }
  .calc .keys { gap: 8px; }
  .calc .key { font-size: 22px; }
  .calc .key.fn { font-size: 17px; }
  .calc .key.op { font-size: 26px; }
  .calc .display { font-size: 48px; }
  .calc .key.zero { padding-left: 20px; }
}

/* ---------------- 공통 추가 ---------------- */
.thumb .dur, .a-thumb .dur {
  position: absolute; right: 6px; bottom: 6px;
  font-size: 11px; font-weight: 600;
  padding: 1px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, .7);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 28px);
  transform: translateX(-50%);
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: rgba(58, 58, 60, .96);
  color: #fff;
  font-size: 14px;
  line-height: 1.4;
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .4);
  animation: fadeIn .2s ease;
}

/* ---------------- 관리 모드 ---------------- */
.admin { z-index: 5; }
.admin-body {
  max-width: 640px;
  margin: 0 auto;
  padding: 8px 16px calc(var(--safe-b) + 40px);
}
.admin input {
  -webkit-user-select: text;
  user-select: text;
}

.a-h {
  margin: 24px 4px 10px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
}
.a-h small { font-weight: 400; margin-left: 4px; }

.a-empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  background: var(--card);
  border-radius: 14px;
  line-height: 1.5;
  font-size: 14px;
}

.a-list {
  background: var(--card);
  border-radius: 14px;
  overflow: hidden;
}
.a-list:empty { display: none; }
.a-row {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  border-bottom: 1px solid #2c2c2e;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.a-row:last-child { border-bottom: 0; }
.a-row:not(.static):active { background: #2c2c2e; }
.a-code {
  min-width: 64px;
  padding: 6px 10px;
  border-radius: 10px;
  background: rgba(255, 159, 10, .16);
  color: var(--op);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
  letter-spacing: .04em;
}
.a-main { flex: 1; min-width: 0; }
.a-main strong { display: block; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.a-main small { color: var(--muted); font-size: 13px; }
.a-chev { color: #5a5a5e; font-size: 24px; line-height: 1; }

.a-btn {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 15px;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.a-btn:active { opacity: .7; }
.a-btn:disabled { opacity: .4; }
.a-btn.primary { background: var(--op); color: #fff; }
.a-btn.danger { color: #ff453a; margin-top: 28px; }
.a-btn.small { flex: none; white-space: nowrap; width: auto; margin: 0; padding: 0 18px; font-size: 15px; background: #3a3a3c; }

.a-card {
  background: var(--card);
  border-radius: 14px;
  padding: 14px 16px 16px;
}
.a-label {
  display: block;
  margin: 10px 0 6px;
  font-size: 13px;
  color: var(--muted);
}
.a-label:first-child { margin-top: 0; }
.a-label small { color: #636366; }
.a-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid #3a3a3c;
  border-radius: 10px;
  background: #000;
  color: var(--text);
  font: inherit;
  font-size: 16px;   /* iOS 자동 확대 방지 */
  outline: none;
}
.a-input:focus { border-color: var(--op); }
.a-input.big { font-size: 26px; font-weight: 600; letter-spacing: .08em; font-variant-numeric: tabular-nums; }
.a-inline { display: flex; gap: 8px; }
.a-meta { margin-top: 12px; font-size: 13px; color: var(--muted); }

.a-tip {
  margin-top: 16px;
  padding: 0 4px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--muted);
}
.a-tip b { color: #c7c7cc; font-weight: 600; }

.a-vids {
  background: var(--card);
  border-radius: 14px;
  overflow: hidden;
}
.a-vid {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid #2c2c2e;
}
.a-vid:last-child { border-bottom: 0; }
.a-thumb {
  position: relative;
  flex: none;
  width: 92px;
  aspect-ratio: 16 / 10;
  padding: 0;
  border: 0;
  border-radius: 8px;
  overflow: hidden;
  background: #2c2c2e;
  cursor: pointer;
}
.a-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.a-thumb-play {
  position: absolute; top: 0; right: 0; bottom: 0; left: 0;
  display: grid; place-items: center;
}
.a-thumb-play svg { filter: drop-shadow(0 1px 3px rgba(0, 0, 0, .6)); }
.a-vinfo { flex: 1; min-width: 0; }
.a-vtitle { padding: 8px 10px; font-size: 15px; }
.a-vinfo small { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); }
.a-vbtns { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.a-vbtns .danger { grid-column: span 2; }
.a-ico {
  min-width: 36px;
  height: 30px;
  border: 0;
  border-radius: 8px;
  background: #3a3a3c;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}
.a-ico:disabled { opacity: .3; }
.a-ico.danger { color: #ff453a; }

.a-status {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(255, 159, 10, .12);
  color: var(--op);
  font-size: 14px;
  line-height: 1.4;
  word-break: break-all;
}

.a-ver { margin-top: 20px; text-align: center; font-size: 12px; color: #48484a; }
