/* 수영 기록 — 모바일(아이폰 세로) 우선, 시스템 다크모드 대응 */

:root {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --border: #e3e6eb;
  --text: #15181d;
  --text-muted: #5f6672;
  --accent: #1d6fd8;
  --accent-text: #ffffff;
  --accent-soft: #e5effc;
  --danger: #d13a2f;
  --better: #13804a;
  --worse: #c2410c;
  --pb: #e09a00;
  --pb-soft: #fff3d6;
  --pb-text: #7a5200;
  --chart-line: #1d6fd8;
  --chart-grid: #e8ebef;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%);

  --radius: 14px;
  --tap: 48px;
  --appbar-h: 52px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  color-scheme: light dark;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --border: #38383a;
    --text: #f2f2f7;
    --text-muted: #a1a1aa;
    --accent: #3d8fe0;
    --accent-text: #ffffff;
    --accent-soft: #15304f;
    --danger: #ff6b5f;
    --better: #4ade80;
    --worse: #fb923c;
    --pb: #f5b83d;
    --pb-soft: #3d2f0f;
    --pb-text: #f5c96a;
    --chart-line: #3d8fe0;
    --chart-grid: #2f2f33;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h2 { font-size: 17px; }
p { margin: 0; }
[hidden] { display: none !important; }

/* ---------- 상단 바 (노치 영역 회피) ---------- */
.appbar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 20;
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  align-items: center;
  height: calc(var(--appbar-h) + var(--safe-top));
  padding: var(--safe-top) calc(4px + var(--safe-right)) 0 calc(4px + var(--safe-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 0.5px solid var(--border);
}
.appbar h1 {
  grid-column: 2;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.appbar .back { grid-column: 1; justify-self: start; font-size: 34px; line-height: 1; padding-bottom: 4px; }
.appbar-action { grid-column: 3; justify-self: end; display: flex; }

/* ---------- 본문 ---------- */
main {
  max-width: 640px;
  margin: 0 auto;
  padding:
    calc(var(--appbar-h) + var(--safe-top) + 16px)
    calc(16px + var(--safe-right))
    calc(var(--tabbar-h) + var(--safe-bottom) + 24px)
    calc(16px + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 20px;
  outline: none;
}
body.logged-out main { padding-bottom: calc(var(--safe-bottom) + 24px); }

section { display: flex; flex-direction: column; gap: 10px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; padding: 0 4px; }
.section-head h2 { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.muted { color: var(--text-muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.link { font-size: 15px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
#pb-body, #trend-body { display: flex; flex-direction: column; gap: 20px; }
.icon { width: 26px; height: 26px; fill: currentColor; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card-title { font-size: 16px; }

/* ---------- 하단 탭 (홈 인디케이터 영역 회피) ---------- */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-top: 0.5px solid var(--border);
}
body.logged-out .tabbar { display: none; }
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  min-height: var(--tap);
}
.tabbar svg { width: 26px; height: 26px; fill: currentColor; }
.tabbar a[aria-current="page"] { color: var(--accent); }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.card .btn { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.dashed { background: transparent; border: 1.5px dashed var(--border); color: var(--accent); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn:active:not(:disabled) { transform: scale(0.98); }
.file-btn { position: relative; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
}
.icon-btn.small { min-width: 40px; min-height: 40px; font-size: 16px; color: var(--text-muted); }
.text-btn { display: inline-flex; align-items: center; min-height: 44px; padding: 0 8px; font-size: 17px; }

/* ---------- 폼 ---------- */
.form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; border: 0; padding: 0; margin: 0; }
.field > span, .field > legend { font-size: 13px; font-weight: 600; color: var(--text-muted); padding: 0; }
.req { font-style: normal; font-weight: 500; color: var(--accent); font-size: 11px; margin-left: 4px; }

input[type=text], input[type=email], input[type=password], input[type=date], select, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 17px; /* 16px 이상이어야 iOS가 입력 시 확대하지 않음 */
  -webkit-appearance: none;
  appearance: none;
}
input[type=date] { display: flex; align-items: center; }
input[type=date]::-webkit-date-and-time-value { text-align: left; }
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 52%, calc(100% - 12px) 52%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-right: 28px;
}
textarea { resize: vertical; min-height: 72px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.invalid { border-color: var(--danger) !important; background: color-mix(in srgb, var(--danger) 8%, var(--surface-2)) !important; }
.field-error, .form-error { color: var(--danger); font-size: 14px; }
.form-error { padding: 0 4px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1.4fr 1fr 0.8fr; gap: 10px; }
.time-field input { font-variant-numeric: tabular-nums; font-weight: 600; }

/* 세그먼트 버튼 (25m / 50m) */
.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; }
.segmented span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border-radius: 9px;
  font-weight: 600;
  color: var(--text-muted);
}
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / 15%); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
.sticky-seg { position: sticky; top: calc(var(--appbar-h) + var(--safe-top) + 6px); z-index: 5; background: var(--surface-2); }

.result-row { gap: 10px; }
.row-head { display: flex; align-items: center; justify-content: space-between; margin: -6px -8px -4px 0; }
.row-num { font-weight: 700; font-size: 14px; color: var(--accent); }

.sticky-actions {
  position: sticky;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px);
  z-index: 5;
}
.sticky-actions .btn { box-shadow: 0 6px 20px rgb(0 0 0 / 18%); }

/* ---------- 목록 ---------- */
.list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.list > li + li { border-top: 0.5px solid var(--border); }
.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 10px 12px 10px 16px;
  color: inherit;
}
a.list-item:active { background: var(--surface-2); }
.list-main { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.list-title { font-weight: 600; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.list-sub { color: var(--text-muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-sub.small { font-size: 12px; }
.list-sub:empty { display: none; }
.list-end { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.chevron { color: var(--text-muted); font-size: 22px; line-height: 1; }

.time { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 18px; white-space: nowrap; }
.time.big { font-size: 20px; }
.time.struck { color: var(--text-muted); text-decoration: line-through; font-weight: 500; }
.pb-dist { font-weight: 700; min-width: 56px; color: var(--accent); }

.delta { font-size: 12px; font-weight: 600; color: var(--text-muted); white-space: nowrap; }
.delta.better { color: var(--better); }
.delta.worse { color: var(--worse); }

/* ---------- 배지 ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.badge.pb { background: var(--pb-soft); color: var(--pb-text); border: 1px solid color-mix(in srgb, var(--pb) 50%, transparent); }
.badge.dq { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.badge.dns { background: var(--surface-2); color: var(--text-muted); }
.pool-chip {
  display: inline-flex;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

/* ---------- 홈 ---------- */
.summary-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; flex-direction: row; }
.stat {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.stat-value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12px; color: var(--text-muted); }

.pb-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.pb-tile {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
}
.pb-tile:active { background: var(--surface-2); }
.pb-event { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.pb-time { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.pb-date { font-size: 12px; color: var(--text-muted); }

/* ---------- 상세 ---------- */
.meet-info { gap: 6px; }
.meet-info-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.memo { white-space: pre-wrap; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: 15px; }
.result-item { padding-right: 4px; }

/* ---------- 추이 ---------- */
.filters { gap: 12px; }
.chart-card { gap: 10px; }
.chart-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.chart-head h2 { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chart-best { display: flex; flex-direction: column; align-items: flex-end; }
.chart-best strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.chart-wrap { position: relative; height: 260px; margin: 0 -6px; }
.chart-total { font-size: 14px; font-weight: 600; text-align: center; }
.chart-total.better { color: var(--better); }
.chart-total.worse { color: var(--worse); }

/* ---------- 로그인 ---------- */
.login { align-items: center; text-align: center; padding-top: 4vh; gap: 12px; }
.login-logo { border-radius: 22px; box-shadow: 0 6px 20px rgb(0 0 0 / 15%); }
.login h1 { font-size: 26px; }
.login .card { width: 100%; text-align: left; margin-top: 8px; }

/* ---------- 상태 화면 ---------- */
.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 48px 16px;
  text-align: center;
  color: var(--text-muted);
}
.state-icon { font-size: 40px; }
.error-state p { color: var(--text); }
.spinner {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.notice { padding: 12px 14px; border-radius: 10px; font-size: 14px; }
.notice ul { margin: 6px 0 0; padding-left: 18px; }
.notice.warn { background: color-mix(in srgb, var(--worse) 12%, transparent); }
.notice.ok { background: color-mix(in srgb, var(--better) 14%, transparent); }
.import-summary { display: flex; flex-direction: column; gap: 8px; }

/* ---------- 토스트 ---------- */
.toast {
  position: fixed;
  left: calc(16px + var(--safe-left));
  right: calc(16px + var(--safe-right));
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 76px);
  z-index: 50;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 12px;
  background: #23262d;
  color: #fff;
  font-size: 15px;
  box-shadow: 0 8px 28px rgb(0 0 0 / 25%);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: none; pointer-events: auto; }
.toast.error { background: #b42318; }
.toast.success { background: #146c43; }
body.logged-out .toast { bottom: calc(var(--safe-bottom) + 24px); }

/* ---------- 확인 시트 ---------- */
dialog.sheet {
  width: min(100% - 32px, 420px);
  border: 0;
  border-radius: 18px;
  padding: 20px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(0 0 0 / 35%);
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 40%); }
dialog.sheet form { display: flex; flex-direction: column; gap: 12px; }
dialog.sheet p { color: var(--text-muted); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 6px; }

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