/* ===========================================================
   WEATHER PROTOCOL — app shell
   rail (left) + status bar (top) + page viewport
   =========================================================== */

#app {
  display: grid;
  /* minmax(0, …): a plain 1fr column grows to its widest child, which
     on a phone pushed the page past the screen, the browser zoomed out
     to fit, and a black band appeared under the bottom menu */
  grid-template-columns: var(--rail) minmax(0, 1fr);
  /* the safe-area inset is the iPhone status bar when installed to the
     home screen (it draws over the app); it is 0 everywhere else */
  grid-template-rows: calc(var(--head) + env(safe-area-inset-top, 0px)) 1fr;
  grid-template-areas:
    "rail head"
    "rail page";
  height: 100vh;
  height: 100dvh;
  opacity: 0;
  transition: opacity .5s ease .1s;
}
#app.ready { opacity: 1; }

/* ===========================================================
   left rail
   =========================================================== */
.rail {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 0;
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  z-index: 40;
}

.rail-logo {
  width: 30px; height: 30px;
  margin-bottom: 10px;
  filter: drop-shadow(0 0 8px var(--a-glow));
  flex: none;
}

.rail-btn {
  position: relative;
  width: 44px; height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  background: none;
  color: var(--fg-3);
  cursor: pointer;
  border-radius: var(--r);
  transition: color var(--speed), background var(--speed);
  flex: none;
}
.rail-btn svg { width: 19px; height: 19px; }
.rail-btn:hover { color: var(--a); background: var(--a-wash); }
.rail-btn:focus-visible { outline: 1px solid var(--a); outline-offset: -2px; }
.rail-btn[aria-current="page"] { color: var(--a); }
/* active indicator bar */
.rail-btn[aria-current="page"]::before {
  content: "";
  position: absolute;
  left: 0; top: 50%;
  width: 2px; height: 20px;
  transform: translateY(-50%);
  background: var(--a);
  box-shadow: 0 0 8px var(--a-glow);
}
/* hover tooltip */
.rail-btn::after {
  content: attr(data-tip);
  position: absolute;
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  padding: 4px 8px;
  background: var(--raised);
  border: 1px solid var(--a-line);
  border-radius: var(--r);
  color: var(--fg);
  font-size: var(--fs-micro);
  letter-spacing: .12em;
  text-transform: uppercase;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--speed);
  z-index: 60;
}
.rail-btn:hover::after { opacity: 1; }

.rail-spacer { flex: 1; }

/* alert badge on the rail */
.rail-btn .badge {
  position: absolute;
  top: 7px; right: 7px;
  min-width: 14px; height: 14px;
  padding: 0 3px;
  display: none;
  place-items: center;
  border-radius: 7px;
  background: var(--danger);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
}
.rail-btn .badge.show { display: grid; }

/* ===========================================================
   status bar
   =========================================================== */
.head {
  grid-area: head;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: env(safe-area-inset-top, 0px) 14px 0;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}

/* --- location switcher --- */
.loc-switch { position: relative; flex: none; }

.loc-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  max-width: 300px;
  padding: 6px 10px;
  background: var(--panel);
  border: 1px solid var(--line-2);
  border-radius: var(--r);
  color: var(--fg);
  font: inherit;
  font-size: var(--fs-sm);
  cursor: pointer;
  transition: border-color var(--speed);
}
.loc-btn:hover { border-color: var(--a-line); }
.loc-btn .caret { color: var(--fg-3); font-size: 9px; transition: transform var(--speed); }
.loc-btn[aria-expanded="true"] .caret { transform: rotate(180deg); }
.loc-name { font-weight: 600; }
.loc-region { color: var(--fg-3); font-size: var(--fs-xs); }

.loc-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  width: 340px;
  max-height: 70vh;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--a-line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  z-index: 200;
  display: none;
  flex-direction: column;
  gap: 8px;
}
.loc-menu.open { display: flex; }

.loc-list { max-height: 300px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }

.loc-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 9px;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--r);
  color: var(--fg-2);
  font: inherit;
  font-size: var(--fs-sm);
  text-align: left;
  cursor: pointer;
}
.loc-item:hover { background: var(--a-wash); border-color: var(--a-line); color: var(--fg); }
.loc-item[aria-current="true"] { color: var(--a); border-color: var(--a-line); background: var(--a-wash); }
.loc-item .t { flex: 1; min-width: 0; }
.loc-item .t b { display: block; font-weight: 600; }
.loc-item .t span { color: var(--fg-3); font-size: var(--fs-micro); }
.loc-item .temp { font-variant-numeric: tabular-nums; color: var(--fg); }
.loc-item .kill { opacity: 0; color: var(--fg-3); padding: 2px 4px; border: 0; background: none; cursor: pointer; }
.loc-item:hover .kill { opacity: 1; }
.loc-item .kill:hover { color: var(--danger); }

.loc-search-wrap { position: relative; }
.loc-results {
  display: flex; flex-direction: column; gap: 2px;
  max-height: 230px; overflow-y: auto;
}
.loc-empty { padding: 14px; text-align: center; color: var(--fg-3); font-size: var(--fs-xs); }

/* --- head right side --- */
.head-stats {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
  font-size: var(--fs-xs);
}
.head-stat { display: flex; align-items: baseline; gap: 6px; white-space: nowrap; }
.head-stat .k { color: var(--fg-3); font-size: var(--fs-micro); letter-spacing: .1em; text-transform: uppercase; }
.head-stat .v { color: var(--fg); font-variant-numeric: tabular-nums; }

.head-clock { color: var(--a); font-variant-numeric: tabular-nums; letter-spacing: .05em; }

/* live-warning banner takes over the head when something is active */
.head.alerting { border-bottom-color: var(--danger); }
.head-siren {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border: 1px solid var(--danger);
  border-radius: var(--r);
  background: rgba(255, 77, 61, .1);
  color: var(--danger);
  font-size: var(--fs-micro);
  letter-spacing: .14em;
  text-transform: uppercase;
  font-weight: 600;
  cursor: pointer;
  animation: sirenPulse 1.6s ease-in-out infinite;
}
.head.alerting .head-siren { display: flex; }
@keyframes sirenPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 77, 61, .5) }
  50%      { box-shadow: 0 0 0 5px rgba(255, 77, 61, 0) }
}

/* whole-window warning frame */
#app.alerting::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 7000;
  pointer-events: none;
  border: 2px solid var(--danger);
  animation: sirenFrame 2.4s ease-in-out infinite;
}
@keyframes sirenFrame { 0%, 100% { opacity: .75 } 50% { opacity: .18 } }

/* ===========================================================
   page viewport
   =========================================================== */
.page-host {
  grid-area: page;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

.page {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px;
  display: none;
  animation: pageIn .26s ease;
}
.page.active { display: block; }
.page.nopad { padding: 0; }
@keyframes pageIn {
  from { opacity: 0; transform: translateY(6px) }
  to   { opacity: 1; transform: none }
}

.page-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.page-head h1 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 600;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--fg-hi);
}
.page-head .sub { color: var(--fg-3); font-size: var(--fs-xs); }

/* responsive grid helper used by every page. min(Npx, 100%): a column
   never demands more than the screen has, or phones get clipped. */
.grid {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr));
}
.grid.two  { grid-template-columns: repeat(auto-fit, minmax(min(380px, 100%), 1fr)); }
.grid.wide { grid-template-columns: repeat(auto-fit, minmax(min(520px, 100%), 1fr)); }
.span-2 { grid-column: span 2; }
@media (max-width: 900px) { .span-2 { grid-column: span 1; } }

/* ===========================================================
   toast / transient notices
   =========================================================== */
#toasts {
  position: fixed;
  bottom: 16px; right: 16px;
  z-index: 9500;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  pointer-events: none;
}
.toast {
  min-width: 230px;
  max-width: 380px;
  padding: 10px 13px;
  background: var(--raised);
  border: 1px solid var(--a-line);
  border-left: 2px solid var(--a);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  color: var(--fg);
  font-size: var(--fs-xs);
  pointer-events: auto;
  animation: toastIn .22s ease;
}
.toast.err  { border-color: var(--danger); border-left-color: var(--danger); color: var(--danger); }
.toast.warn { border-color: var(--warn);   border-left-color: var(--warn); }
.toast.out  { animation: toastOut .22s ease forwards; }
@keyframes toastIn  { from { opacity: 0; transform: translateX(18px) } }
@keyframes toastOut { to   { opacity: 0; transform: translateX(18px) } }

/* ===========================================================
   modal
   =========================================================== */
.modal-veil {
  position: fixed;
  inset: 0;
  z-index: 9200;
  display: none;
  place-items: center;
  padding: 20px;
  background: rgba(2, 4, 6, .78);
  backdrop-filter: blur(3px);
}
.modal-veil.open { display: grid; }
.modal {
  width: min(520px, 100%);
  max-height: 85vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--a-line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  padding: 20px;
  animation: pageIn .2s ease;
}

/* ===========================================================
   narrow windows — rail becomes a top bar, above the status bar
   =========================================================== */
@media (max-width: 720px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto var(--head) minmax(0, 1fr);
    grid-template-areas: "rail" "head" "page";
  }
  /* the menu is the top row now, so it is the one that clears the
     iPhone status bar; the header below it no longer needs to */
  .rail {
    flex-direction: row;
    justify-content: space-between;
    gap: 0;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: env(safe-area-inset-top, 0px) 2px 0;
  }
  .rail-logo, .rail-spacer { display: none; }
  .rail-btn::after { display: none; }
  /* squeeze: every button shares the width equally */
  .rail-btn { flex: 1 1 0; width: auto; min-width: 0; height: 44px; }
  .rail-btn svg { width: 18px; height: 18px; }
  .rail-btn[aria-current="page"]::before {
    left: 50%; top: auto; bottom: 0;
    width: 20px; height: 2px;
    transform: translateX(-50%);
  }
  .head-stats { gap: 10px; }
  .head-stat:not(.keep) { display: none; }
  .page { padding: 12px 12px calc(12px + env(safe-area-inset-bottom, 0px)); }
  .page.nopad { padding: 0 0 env(safe-area-inset-bottom, 0px); }
  .loc-menu { width: min(340px, calc(100vw - 28px)); }

  /* the header must fit the screen: the location button gives way
     (truncating its name) instead of pushing the page wider */
  .head { gap: 8px; padding: 0 10px; min-width: 0; }
  .loc-switch { flex: 0 1 auto; min-width: 0; }
  .loc-btn { max-width: 100%; min-width: 0; }
  .loc-region { display: none; }
  .head-stats { flex: none; gap: 8px; }
}

/* Scroll smoothness: keep each page's scroller composited, and stop wheel
   scrolls at the end of a page from chaining into the window. */
.page { will-change: scroll-position; overscroll-behavior: contain; }
