:root {
  --bg: #0b1220;
  --bg-2: #111a2e;
  --card: rgba(255, 255, 255, 0.05);
  --line: rgba(255, 255, 255, 0.09);
  --text: #eef2f8;
  --muted: #8d99b0;
  --accent: #7cc4ff;
  --warm: #ffb86b;
  --cold: #7cc4ff;
  --rain: #5aa9ff;
  --good: #5ad19a;
  --fair: #e8d15a;
  --bad: #ff9a5a;
  --worst: #ff5a6e;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(120% 120% at 0% 0%, var(--bg-2), var(--bg) 60%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Segoe UI", sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  cursor: default;
  user-select: none;
}

body.night { filter: brightness(0.55); }

#app {
  height: 100%;
  height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto minmax(190px, 1fr) auto auto;
  grid-template-areas: "hero" "details" "hourly" "daily" "status";
  gap: clamp(10px, 1.6vw, 22px);
  padding: clamp(14px, 2.4vw, 36px);
  padding-top: max(clamp(14px, 2.4vw, 36px), env(safe-area-inset-top));
  transition: transform 2s ease;
}

section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: clamp(10px, 1.2vw, 18px) clamp(12px, 1.6vw, 24px);
  min-width: 0;
}

h2 {
  margin: 0 0 6px;
  font-size: clamp(12px, 1.1vw, 15px);
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.04em;
}

/* 시계와 현재 날씨 (한 줄에 나란히) */
.hero {
  grid-area: hero;
  --clock: clamp(56px, min(11vw, 16vh), 230px);
  padding-top: clamp(10px, 1.2vw, 18px);
  padding-bottom: clamp(10px, 1.2vw, 18px);
}
.hero-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: clamp(14px, 1.6vw, 24px);
}
.hero-main {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 3vw, 48px);
}
.time {
  font-size: var(--clock);
  font-weight: 200;
  line-height: 1;
  letter-spacing: -0.03em;
  white-space: nowrap;
}
.time .sec {
  font-size: 0.32em;
  color: var(--muted);
  margin-left: 0.15em;
  font-weight: 300;
  letter-spacing: 0;
}

.now-main { display: flex; align-items: center; gap: clamp(8px, 1.6vw, 24px); min-width: 0; }
.now-icon { width: calc(var(--clock) * 1.4); height: calc(var(--clock) * 1.4); flex: none; margin: calc(var(--clock) * -0.2) calc(var(--clock) * -0.1); }
.now-icon svg { width: 100%; height: 100%; overflow: visible; }
.now-text { min-width: 0; }
.now-temp { font-size: calc(var(--clock) * 0.62); font-weight: 300; line-height: 1; white-space: nowrap; }
.now-temp .unit { color: var(--muted); font-weight: 200; }
.now-desc { font-size: calc(var(--clock) * 0.17); margin-top: 4px; white-space: nowrap; }
.now-range { color: var(--muted); font-size: calc(var(--clock) * 0.12); margin-top: 2px; white-space: nowrap; }

/* 상세 정보 */
.details-panel { grid-area: details; padding-top: 10px; padding-bottom: 10px; }
.now-details {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px 12px;
}
.now-details div { min-width: 0; }
.now-details dt { color: var(--muted); font-size: clamp(11px, 0.95vw, 14px); }
.now-details dd { margin: 2px 0 0; font-size: clamp(14px, 1.35vw, 20px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.aq-good { color: var(--good); }
.aq-fair { color: var(--fair); }
.aq-bad { color: var(--bad); }
.aq-worst { color: var(--worst); }

/* 시간별 (가로 자동 스크롤) */
.hourly-panel { grid-area: hourly; display: flex; flex-direction: column; min-height: 0; }
.scroller {
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}
.scroller::-webkit-scrollbar { display: none; }
.hourly-panel .scroller { flex: 1; display: flex; }
.hourly {
  --hour-w: clamp(66px, 6.4vw, 100px);
  display: grid;
  grid-template-columns: repeat(24, var(--hour-w));
  width: max-content;
  min-height: 100%;
  position: relative;
}
.hour {
  display: grid;
  grid-template-rows: auto auto 1fr auto auto;
  justify-items: center;
  align-items: center;
  font-size: clamp(13px, 1.25vw, 18px);
  min-width: 0;
  position: relative;
  z-index: 1;
}
.hour .h-time { color: var(--muted); }
.hour .h-icon { width: calc(var(--hour-w) * 0.62); height: calc(var(--hour-w) * 0.62); margin: 2px 0; }
.hour .h-icon svg { width: 100%; height: 100%; overflow: visible; }
.hour .h-spacer { width: 100%; align-self: stretch; min-height: 14px; }
.hour .h-temp { font-weight: 700; font-size: 1.1em; }
.hour .h-pop { color: var(--rain); font-size: 0.85em; min-height: 1.2em; }
.hour.now .h-time { color: var(--accent); font-weight: 700; }
.hour.midnight::before {
  content: "";
  position: absolute;
  left: 0; top: 10%; bottom: 10%;
  border-left: 1px dashed var(--line);
}
.hourly svg.chart {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 0;
  pointer-events: none;
  z-index: 0;
}

/* 일별 (가로 자동 스크롤) */
.daily-panel { grid-area: daily; }
.daily {
  --day-w: clamp(130px, 16vw, 210px);
  display: grid;
  grid-template-columns: repeat(7, var(--day-w));
  gap: 10px;
  width: max-content;
}
.day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: clamp(14px, 1.35vw, 19px);
  padding: 4px 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.03);
}
.day.today { background: rgba(124, 196, 255, 0.1); }
.day .d-name { color: var(--muted); }
.day.today .d-name { color: var(--accent); font-weight: 700; }
.day .d-icon { width: calc(var(--day-w) * 0.27); height: calc(var(--day-w) * 0.27); }
.day .d-icon svg { width: 100%; height: 100%; overflow: visible; }
.day .d-temps { font-size: 1.1em; }
.day .d-temps .max { color: var(--warm); font-weight: 700; }
.day .d-temps .min { color: var(--cold); }
.day .d-pop { color: var(--rain); font-size: 0.9em; min-height: 1.2em; }

/* 아이콘 입체감 */
.h-icon svg, .d-icon svg { filter: drop-shadow(0 4px 3px rgba(0, 0, 0, 0.45)); }

/* 상태 표시줄 */
.status {
  grid-area: status;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.status.error #updated { color: var(--worst); }
.actions { display: flex; gap: 8px; }
.actions button {
  border: 1px solid var(--line);
  border-radius: 999px;
  font: inherit;
  cursor: pointer;
}
.actions button:hover { color: var(--text); }

.flash { animation: flash 1.2s ease; }
@keyframes flash { from { color: var(--accent); } }

/* 3D 캐릭터 캔버스 */
.now-icon canvas.scene3d { width: 100%; height: 100%; display: block; }

/* 세로 화면 */
@media (orientation: portrait) {
  .hero { --clock: clamp(44px, 15vw, 200px); }
  .now-details { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  #app { grid-template-rows: auto auto 1.4fr 1fr auto; }
  .daily-panel { display: flex; flex-direction: column; min-height: 0; }
  .daily-panel .scroller { flex: 1; display: flex; align-items: center; }
  .daily { --day-w: clamp(120px, 24vw, 200px); }
  .day .d-icon { width: calc(var(--day-w) * 0.42); height: calc(var(--day-w) * 0.42); }
  .hourly { --hour-w: clamp(62px, 12vw, 110px); }
}
/* 가로 화면: 높이에 맞춰 줄이고 7일 예보는 옆으로 누운 카드로 */
@media (orientation: landscape) {
  #app { grid-template-rows: auto auto minmax(0, 1fr) auto auto; padding-bottom: 6px; }
  .hero { --clock: clamp(40px, min(11vw, 15vh), 230px); }
  .hour { font-size: clamp(11px, min(1.25vw, 2.2vh), 18px); }
  .hour .h-icon { width: min(calc(var(--hour-w) * 0.62), 7vh); height: min(calc(var(--hour-w) * 0.62), 7vh); }
  .daily { --day-w: clamp(150px, 15vw, 230px); }
  .day {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-areas: "icon name" "icon temps" "icon pop";
    justify-content: center;
    align-items: center;
    column-gap: 10px;
    row-gap: 0;
    padding: 6px 10px;
    font-size: clamp(12px, min(1.3vw, 2.4vh), 19px);
  }
  .day .d-icon { grid-area: icon; width: min(calc(var(--day-w) * 0.3), 9vh); height: min(calc(var(--day-w) * 0.3), 9vh); }
  .day .d-name { grid-area: name; }
  .day .d-temps { grid-area: temps; }
  .day .d-pop { grid-area: pop; min-height: 0; }
}
/* 높이가 낮은 가로 태블릿 (예: 1024x600) */
@media (orientation: landscape) and (max-height: 790px) {
  #app { gap: 8px; padding-top: 10px; padding-bottom: 4px; }
  .hero { --clock: clamp(40px, min(11vw, 13vh), 200px); padding-top: 6px; padding-bottom: 6px; }
  .details-panel { padding-top: 6px; padding-bottom: 6px; }
  h2 { display: none; }
  .hour .h-icon { width: 5.5vh; height: 5.5vh; margin: 0; }
  .hour .h-spacer { min-height: 6px; }
  .status { padding-top: 2px; padding-bottom: 2px; }
}
/* 폰 가로처럼 아주 낮은 화면은 페이지 스크롤 */
@media (orientation: landscape) and (max-height: 520px) {
  html, body { overflow: auto; }
  #app { height: auto; min-height: 100dvh; grid-template-rows: auto auto 200px auto auto; }
}

/* 폰처럼 아주 좁은 화면은 페이지 스크롤 */
@media (max-width: 480px) {
  html, body { overflow: auto; }
  #app { height: auto; min-height: 100dvh; grid-template-rows: auto; }
  .daily-panel .scroller { display: block; }
  .hero { --clock: 16vw; }
  .now-desc, .now-range { white-space: normal; }
  .now-details { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hourly-panel .scroller { min-height: 200px; }
}

/* 날씨 아이콘 애니메이션 */
.wx-sun-rays { transform-origin: 32px 32px; animation: wx-spin 24s linear infinite; }
.wx-sun-core { transform-origin: 32px 32px; animation: wx-pulse 4s ease-in-out infinite; }
.wx-moon { transform-origin: 40px 32px; animation: wx-rock 8s ease-in-out infinite; }
.wx-cloud { animation: wx-drift 7s ease-in-out infinite; }
.wx-cloud-back { animation: wx-drift 9s ease-in-out infinite reverse; }
.wx-drop { animation: wx-fall 1.1s linear infinite; }
.wx-drop:nth-child(2) { animation-delay: .35s; }
.wx-drop:nth-child(3) { animation-delay: .7s; }
.wx-flake { animation: wx-snow 2.6s linear infinite; }
.wx-flake:nth-child(2) { animation-delay: .9s; }
.wx-flake:nth-child(3) { animation-delay: 1.7s; }
.wx-bolt { animation: wx-flash 3s steps(1) infinite; }
.wx-fog line { animation: wx-slide 5s ease-in-out infinite; }
.wx-fog line:nth-child(even) { animation-direction: reverse; }

@keyframes wx-spin { to { transform: rotate(360deg); } }
@keyframes wx-pulse { 50% { transform: scale(1.08); } }
@keyframes wx-rock { 50% { transform: rotate(-8deg); } }
@keyframes wx-drift { 50% { transform: translateX(3px); } }
@keyframes wx-fall {
  0% { transform: translateY(-6px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translateY(8px); opacity: 0; }
}
@keyframes wx-snow {
  0% { transform: translate(0, -6px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translate(2px, 8px); opacity: 0; }
}
@keyframes wx-flash {
  0%, 84%, 100% { opacity: 1; }
  86%, 90% { opacity: 0.15; }
  88%, 92% { opacity: 1; }
}
@keyframes wx-slide { 50% { transform: translateX(4px); } }

@media (prefers-reduced-motion: reduce) {
  [class^="wx-"], [class*=" wx-"], .wx-fog line { animation: none !important; }
}

/* 하단 버튼은 항상 보이게 */
.status { position: sticky; bottom: 0; z-index: 5; padding: 6px 0; padding-bottom: max(6px, env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 40%); }
.actions button { color: var(--text); background: rgba(255, 255, 255, 0.06); padding: 8px 16px; font-size: 13px; white-space: nowrap; }

/* 날씨 캐릭터 애니메이션 */
.mascot { width: 100%; height: 100%; overflow: visible; }
.mascot .mc-blink { transform-box: fill-box; transform-origin: center; animation: mc-blink 5s infinite; }
.mc-float { animation: mc-float 3.6s ease-in-out infinite; }
.mc-spin { animation: mc-spin 14s linear infinite; }
.mc-rock { transform-box: fill-box; transform-origin: center; animation: mc-rock 6s ease-in-out infinite; }
.mc-twinkle { transform-box: fill-box; transform-origin: center; animation: mc-twinkle 2.4s ease-in-out infinite; }
.mc-zzz { animation: mc-zzz 3s ease-out infinite; }
.mc-drift-far { animation: mc-drift-far 14s ease-in-out infinite; }
.mc-fog { animation: mc-fog 6s ease-in-out infinite; }
.mc-rain { animation: mc-rain 0.9s linear infinite; }
.mc-snow { animation: mc-snowfall 3s linear infinite; }
.mc-ripple { transform-box: fill-box; transform-origin: center; animation: mc-ripple 1.8s ease-out infinite; }
.mc-wave-l { animation: mc-wave-l 1.6s ease-in-out infinite; }
.mc-wave-r { animation: mc-wave-r 1.6s ease-in-out infinite; }
.mc-scarf { animation: mc-scarf 1.2s ease-in-out infinite; }
.mc-bolt { animation: mc-bolt 3.2s infinite; }
.mc-flash-bg { opacity: 0; animation: mc-flash-bg 3.2s infinite; }

@keyframes mc-blink { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(0.1); } }
@keyframes mc-float { 50% { transform: translateY(-4px); } }
@keyframes mc-spin { to { transform: rotate(360deg); } }
@keyframes mc-rock { 50% { transform: rotate(-7deg); } }
@keyframes mc-twinkle { 50% { transform: scale(0.5); opacity: 0.3; } }
@keyframes mc-zzz {
  0% { transform: translate(0, 6px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translate(8px, -14px); opacity: 0; }
}
@keyframes mc-drift-far { 0%, 100% { transform: translateX(-40px); } 50% { transform: translateX(20px); } }
@keyframes mc-fog { 50% { transform: translateX(10px); } }
@keyframes mc-rain {
  0% { transform: translateY(-14px); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: translateY(14px); opacity: 0; }
}
@keyframes mc-snowfall {
  0% { transform: translate(0, -20px); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: translate(6px, 20px); opacity: 0; }
}
@keyframes mc-ripple { 0% { transform: scale(0.3); opacity: 1; } 100% { transform: scale(1.4); opacity: 0; } }
@keyframes mc-wave-l { 50% { transform: rotate(18deg); } }
@keyframes mc-wave-r { 50% { transform: rotate(-24deg); } }
@keyframes mc-scarf { 50% { transform: rotate(10deg); } }
@keyframes mc-bolt { 0%, 70%, 100% { opacity: 0; } 72%, 78% { opacity: 1; } 75% { opacity: 0.2; } }
@keyframes mc-flash-bg { 0%, 71%, 80%, 100% { opacity: 0; } 72%, 77% { opacity: 0.6; } }

@media (prefers-reduced-motion: reduce) {
  [class^="mc-"], [class*=" mc-"] { animation: none !important; }
  .mc-bolt { opacity: 1; }
}

/* 오른쪽 위 새로고침 버튼 */
.hero-right { display: inline-flex; align-items: center; gap: 12px; }
#btn-refresh {
  width: 32px;
  height: 32px;
  margin: -4px 0;
  flex: none;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
}
#btn-refresh:hover { background: rgba(255, 255, 255, 0.12); }
#btn-refresh.spinning { animation: wx-spin 0.8s linear infinite; }
