:root {
  --bg: #0b0f17;
  --bg2: #131a26;
  --card: #1a2433;
  --line: #263247;
  --txt: #e8eef7;
  --muted: #8a99ad;
  --accent: #38bdf8;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --nav-h: 64px;
}

/* 라이트 모드 */
body[data-theme="light"] {
  --bg: #eef2f7;
  --bg2: #ffffff;
  --card: #ffffff;
  --line: #d8dee8;
  --txt: #0b1320;
  --muted: #5b6675;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--txt);
  font-family: -apple-system, "Segoe UI", "Noto Sans KR", Roboto, sans-serif;
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- 저장소 차단 안내 배너 ---------- */
.storage-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 99;
  background: var(--red); color: #fff;
  font-size: 13px; line-height: 1.45;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  text-align: center;
}

/* ---------- 뷰 전환 (반응형: 넓은 화면에선 중앙 컬럼) ---------- */
.view {
  display: none; height: calc(100dvh - var(--nav-h)); overflow-y: auto;
  width: 100%; max-width: 680px; margin: 0 auto;
}
.view.active { display: flex; flex-direction: column; }
#view-records.active > *, #view-ranking.active > *, #view-settings.active > * { flex-shrink: 0; }

/* ---------- 디스플레이 ---------- */
.disp-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px calc(14px + env(safe-area-inset-top));
}
.conn { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.conn .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn.off .dot { background: #4b5563; }
.conn.live .dot { background: var(--green); box-shadow: 0 0 10px var(--green); }
.conn.waiting .dot { background: var(--amber); box-shadow: 0 0 10px var(--amber); }
.conn.live { color: var(--green); }
.conn.waiting { color: var(--amber); }

.top-right { display: flex; align-items: center; gap: 8px; }
.stacker-chip {
  max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 7px 12px; border-radius: 999px; font-size: 13px; font-weight: 700;
  background: rgba(56,189,248,.15); color: var(--accent); border: 1px solid rgba(56,189,248,.4);
}
.icon-btn {
  background: var(--card); border: 1px solid var(--line); color: var(--txt);
  width: 40px; height: 40px; border-radius: 10px; font-size: 18px; flex: 0 0 auto;
}

.event-tabs { display: flex; gap: 10px; padding: 0 16px; }
.event-tab {
  flex: 1; padding: 18px 8px; border-radius: 14px; font-size: 20px; font-weight: 800;
  white-space: nowrap; background: var(--card); border: 2px solid var(--line); color: var(--muted);
}
.event-tab.active { color: #04121c; background: var(--accent); border-color: var(--accent); }
.event-tab.seq { flex: 0 0 auto; min-width: 62px; font-size: 17px; padding-left: 6px; padding-right: 6px; }

.stage {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  margin: 12px 16px; border-radius: 24px; border: 2px solid var(--line);
  background: radial-gradient(120% 120% at 50% 0%, var(--card) 0%, var(--bg2) 60%);
  transition: border-color .15s, box-shadow .15s, background .15s;
  container-type: inline-size; /* 타이머 숫자를 이 박스 너비 기준으로 스케일 */
}
.big-time {
  font-variant-numeric: tabular-nums;
  font-weight: 800; letter-spacing: 0; line-height: 1;
  /* 글자 수(--len)로 나눠 어떤 길이든 화면을 넘치지 않게 자동 축소 */
  font-size: clamp(40px, calc(150cqi / var(--len, 6)), 240px);
  max-width: 100%;
  font-family: "SFMono-Regular", "Consolas", monospace;
}
.stage-hint { margin-top: 18px; color: var(--muted); font-size: 15px; text-align: center; padding: 0 20px; }

.stage.ready { border-color: var(--green); box-shadow: inset 0 0 60px rgba(34,197,94,.25); }
.stage.ready .big-time { color: var(--green); }
.stage.running { border-color: var(--accent); box-shadow: inset 0 0 60px rgba(56,189,248,.2); }
.stage.running .big-time { color: var(--accent); }
.stage.stopped { border-color: var(--amber); box-shadow: inset 0 0 70px rgba(245,158,11,.25); }
.stage.stopped .big-time { color: var(--txt); animation: stoppop .4s ease; }
@keyframes stoppop { 0% { transform: scale(1); } 40% { transform: scale(1.06); } 100% { transform: scale(1); } }

/* ---------- 오늘 기록 미리보기 모달 ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.75); display: flex; align-items: center; justify-content: center; z-index: 80; padding: 20px; }
.modal[hidden] { display: none; }
.modal-box { background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; padding: 16px; max-width: 420px; width: 100%; max-height: 92vh; overflow: auto; }
.share-img { width: 100%; border-radius: 12px; display: block; }
.modal-box .btn-row { margin-top: 12px; }
.modal-box .ghost-btn.full { margin-top: 8px; }

/* ---------- 신기록(PB) 축하 ---------- */
.pb-overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  pointer-events: none; opacity: 0; z-index: 60; transition: opacity .2s; overflow: hidden;
}
.pb-overlay.show { opacity: 1; }
.pb-text {
  font-size: clamp(40px, 9vw, 96px); font-weight: 900; color: #fff; text-align: center;
  text-shadow: 0 0 30px var(--accent), 0 0 60px var(--accent); animation: pbpop .5s ease;
}
@keyframes pbpop { 0% { transform: scale(.5); opacity: 0; } 60% { transform: scale(1.15); } 100% { transform: scale(1); opacity: 1; } }
.confetti {
  position: absolute; top: -12px; width: 10px; height: 14px; border-radius: 2px;
  animation: confetti-fall 2.3s ease-in forwards;
}
@keyframes confetti-fall {
  0% { transform: translate(0, -10vh) rotate(0); opacity: 1; }
  100% { transform: translate(var(--x), 110vh) rotate(720deg); opacity: .15; }
}

.saved-toast {
  position: fixed; left: 50%; top: 18%; transform: translateX(-50%) translateY(-10px);
  background: var(--green); color: #04121c; font-weight: 800; padding: 10px 18px;
  border-radius: 999px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
}
.saved-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.disp-bottom { padding: 4px 16px 16px; }
.preview-entry { display: block; margin-top: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); color: var(--accent); text-align: center; text-decoration: none; }
.preview-entry strong { display: block; font-size: 13px; line-height: 1.5; }
.preview-entry span { display: block; margin-top: 4px; color: var(--muted); font-size: 11px; line-height: 1.5; }
.preview-entry:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.feature-tabs { display:grid; grid-template-columns:1fr 1fr; gap:8px; }
.recording-entry { border-color:var(--accent); }
body.timing .feature-tabs, body.present .feature-tabs { display: none; }
.mic-btn {
  width: 100%; padding: 16px; border-radius: 14px; font-size: 17px; font-weight: 700;
  background: var(--accent); color: #04121c; border: none;
}
.mic-btn.on { background: var(--card); color: var(--txt); border: 1px solid var(--line); }

/* ---------- 페이지 헤더 ---------- */
.page-head { padding: calc(18px + env(safe-area-inset-top)) 16px 6px; }
.page-head h1 { margin: 0; font-size: 26px; }

/* ---------- 기록 ---------- */
.best-cards { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; padding: 10px 16px; }
.best-card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 8px; text-align: center; }
.best-card .ev { font-size: 12px; color: var(--muted); font-weight: 700; }
.best-card .bt { font-size: 20px; font-weight: 800; margin-top: 4px; font-variant-numeric: tabular-nums; }
.best-card .lb { font-size: 10px; color: var(--muted); }

.filter-chips { display: flex; gap: 8px; padding: 6px 16px; overflow-x: auto; }
.chip {
  flex: 0 0 auto; padding: 8px 14px; border-radius: 999px; font-size: 14px; font-weight: 600;
  background: var(--card); border: 1px solid var(--line); color: var(--muted);
}
.chip.active { background: var(--accent); color: #04121c; border-color: var(--accent); }

.records-actions { display: flex; gap: 8px; padding: 8px 16px; }
.records-actions .mic-btn { width: auto; flex: 1 1 0; white-space: nowrap; }
.ghost-btn {
  flex: 1; padding: 11px; border-radius: 12px; font-size: 14px; font-weight: 600;
  background: var(--card); border: 1px solid var(--line); color: var(--txt);
}
.ghost-btn.danger { color: var(--red); border-color: rgba(239,68,68,.4); }

.record-list { list-style: none; margin: 0; padding: 4px 16px 24px; }
.record-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 14px; margin-bottom: 8px;
}
.record-item .badge {
  font-size: 11px; font-weight: 800; padding: 4px 8px; border-radius: 8px;
  background: var(--bg2); color: var(--accent); border: 1px solid var(--line); white-space: nowrap;
}
.record-item .rtime { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.record-item .rdate { font-size: 12px; color: var(--muted); margin-top: 2px; }
.record-item .grow { flex: 1; }
.record-item .del { background: none; border: none; color: var(--muted); font-size: 18px; padding: 6px; }
.record-item.best { border-color: var(--green); }
.record-item.best .badge { color: var(--green); }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; display: none; }

/* ---------- 설정 ---------- */
.setting-block { margin: 12px 16px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.setting-block.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.setting-label { font-size: 15px; font-weight: 700; margin-bottom: 8px; }
.setting-block.row .setting-label { margin-bottom: 2px; }
.hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; line-height: 1.5; }
.select {
  width: 100%; padding: 12px; border-radius: 10px; font-size: 15px;
  background: var(--bg2); color: var(--txt); border: 1px solid var(--line);
}
.guide { margin: 6px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; line-height: 1.8; }
.guide b { color: var(--txt); }
.version { text-align: center; color: var(--muted); font-size: 12px; padding: 20px; }
.legal-links { display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; }
.legal-links a { color: var(--accent); }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid var(--amber); outline-offset: 2px;
}

/* ---------- 계정/프로필 폼 ---------- */
.input {
  width: 100%; padding: 12px; border-radius: 10px; font-size: 15px; margin-top: 6px;
  background: var(--bg2); color: var(--txt); border: 1px solid var(--line);
}
.field-label { display: block; font-size: 13px; color: var(--muted); margin-top: 12px; }
.birth-row { display: flex; gap: 8px; margin-top: 6px; }
.consent-row { display: flex; gap: 8px; align-items: flex-start; margin-top: 10px; font-size: 13px; color: var(--txt); line-height: 1.5; }
.consent-row input { width: 18px; height: 18px; flex: none; margin-top: 1px; accent-color: var(--accent); }
.birth-row .select { flex: 1; min-width: 0; padding: 12px 8px; text-align: center; }
.birth-row #authBirthY, .birth-row #pfBirthY { flex: 1.4; }
.btn-row { display: flex; gap: 8px; margin-top: 12px; }
.btn-row .ghost-btn, .btn-row .mic-btn { margin-top: 0; }
.ghost-btn.full { width: 100%; margin-top: 8px; }
.text-btn { display: block; margin: 10px auto 0; padding: 6px; border: 0; background: transparent; color: var(--accent); font-size: 13px; text-decoration: underline; }
.mic-btn.small { padding: 12px; font-size: 15px; }
.mic-btn.full { width: 100%; margin-top: 8px; }
.hint.err { color: var(--red); }
.hint.ok { color: var(--green); }
.hint.center { text-align: center; }
.select.sm { padding: 9px; font-size: 13px; }

/* ---------- 테마/아바타 위젯 ---------- */
.swatches { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.swatch { width: 40px; height: 40px; border-radius: 50%; border: 3px solid transparent; padding: 0; cursor: pointer; position: relative; }
.swatch.sel { border-color: var(--txt); }
.swatch.custom {
  width: auto; height: 40px; border-radius: 20px; border: 1px dashed var(--line);
  display: inline-flex; align-items: center; padding: 0 14px; font-size: 12px; color: var(--muted); overflow: hidden;
}
.swatch.custom input[type="color"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.avatar-row { display: flex; gap: 12px; align-items: flex-start; margin-top: 8px; }
.avatar-preview { width: 64px; height: 64px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 34px; flex: 0 0 auto; }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; flex: 1; }
.emoji-btn { font-size: 22px; padding: 6px 0; border-radius: 10px; background: var(--card); border: 1px solid var(--line); color: var(--txt); }
.emoji-btn.sel { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent); }

/* ---------- 랭킹 ---------- */
.disclaimer {
  margin: 8px 16px; padding: 10px 14px; border-radius: 12px; font-size: 13px; font-weight: 600;
  color: var(--amber); background: rgba(245,158,11,.1); border: 1px solid rgba(245,158,11,.35);
  text-align: center;
}
.rank-filters { padding: 4px 16px 8px; display: flex; flex-direction: column; gap: 8px; }
.seg { display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; }
.seg-btn {
  flex: 1; padding: 9px 0; border: none; border-radius: 9px; font-size: 14px; font-weight: 700;
  background: none; color: var(--muted);
}
.seg-btn.active { background: var(--accent); color: #04121c; }
.filter-row { display: flex; gap: 8px; }
.filter-row .select { flex: 1; min-width: 0; }

.rank-list { list-style: none; margin: 0; padding: 4px 16px 24px; }
.rank-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 14px; margin-bottom: 7px;
}
.rank-no { width: 28px; text-align: center; font-weight: 800; font-size: 16px; color: var(--muted); flex: 0 0 auto; }
.rank-item.top .rank-no { color: var(--amber); }
.rank-item .grow { flex: 1; min-width: 0; }
.rank-name { font-size: 16px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.rank-time { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.rank-item.me { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.rank-loading { list-style: none; text-align: center; color: var(--muted); padding: 24px; }

/* 토글 스위치 */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; flex: 0 0 auto; }
.switch input { display: none; }
.slider { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; }
.slider::before { content: ""; position: absolute; width: 24px; height: 24px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--green); }
.switch input:checked + .slider::before { transform: translateX(22px); }

/* ---------- 하단 내비 ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; height: var(--nav-h);
  display: flex; justify-content: center; background: var(--bg2); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  transition: transform .25s ease;
}
/* 측정 중에는 하단 메뉴를 아래로 숨김 */
body.timing .bottom-nav { transform: translateY(120%); }
body.timing .event-tabs { opacity: 0; pointer-events: none; transition: opacity .2s; }
.nav-btn {
  flex: 1 1 0; max-width: 227px; background: none; border: none; color: var(--muted);
  font-size: 11px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
}
.nav-btn span { font-size: 20px; }
.nav-btn em { font-style: normal; }
.nav-btn.active { color: var(--accent); }

/* ---------- 가로 모드: 타이머만 더 크게 (메뉴는 그대로 유지) ---------- */
@media (orientation: landscape) {
  .big-time { font-size: clamp(60px, calc(150cqi / var(--len, 6)), 360px); }
  #view-display .event-tabs { max-width: 600px; margin: 0 auto; }
  #view-display .event-tab { padding: 20px 8px; font-size: 22px; }
  #view-display .disp-bottom { max-width: 600px; margin: 0 auto; width: 100%; }
}

/* ---------- 전체 디스플레이 모드 (⛶ 버튼으로 켜고 끔) ----------
   전광판/촬영용. 메뉴·탭·상태표시를 숨기고 타이머를 꽉 채움.
   ⛶ 버튼은 그대로 남아 있어 다시 누르면 빠져나옴(또는 Esc). */
body.present .bottom-nav,
body.present .event-tabs,
body.present #conn,
body.present .stacker-chip { display: none; }
body.present #view-display.active { height: 100dvh; max-width: none; }
body.present .stage { margin: 8px; }
body.present .big-time { font-size: clamp(72px, calc(150cqi / var(--len, 6)), 420px); }
body.present .disp-bottom { max-width: 520px; margin: 0 auto; width: 100%; }
body.present .disp-top { position: absolute; top: 0; right: 0; padding: 10px; }
