/* ==========================================================================
   App shell — sidebar, topbar, page frame
   ========================================================================== */

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 100%;
  height: 100%;
}

/* ----------------------------------------------------------- sidebar -- */

.sidebar {
  grid-column: 1;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 40;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--topbar-h);
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo {
  width: 30px; height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--brand-500), var(--brand-700));
  display: grid; place-items: center;
  color: #fff; font-weight: 800; font-size: .9rem;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

.logo-text { font-weight: 700; font-size: 1.02rem; letter-spacing: -.03em; }
.logo-text span { color: var(--brand-text); }

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 12px 10px 8px;
  min-height: 0;
}

.nav-group + .nav-group { margin-top: 16px; }

.nav-label {
  font-size: .66rem;
  font-weight: 700;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 0 10px;
  margin-bottom: 6px;
}

/* U07 — nav groups collapse and remember it. Reset button chrome so a
   clickable label reads identically to the plain caption it replaces. */
.nav-label-btn {
  display: flex; align-items: center; width: 100%;
  border: none; background: transparent; cursor: pointer;
  font: inherit; text-align: left;
}
.nav-label-btn .chev { font-size: .8rem; opacity: .6; }
.nav-label-btn:hover { color: var(--text-2); }
.nav-group.collapsed { margin-bottom: 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: .855rem;
  font-weight: 550;
  text-align: left;
  transition: background-color .12s var(--ease), color .12s var(--ease);
}

.nav-item:hover { background: var(--surface-2); color: var(--text); }

.nav-item.active {
  background: var(--brand-50);
  color: var(--brand-600);
  font-weight: 650;
}

[data-theme="dark"] .nav-item.active { color: var(--brand-300); }

.nav-item .ico { width: 17px; text-align: center; font-size: .95rem; flex-shrink: 0; }

.nav-item .count {
  margin-left: auto;
  font-size: .68rem;
  font-weight: 700;
  background: var(--surface-3);
  color: var(--text-2);
  padding: 1px 7px;
  border-radius: var(--radius-full);
  min-width: 20px;
  text-align: center;
}

.nav-item.active .count { background: var(--brand-500); color: #fff; }
.nav-item .count.alert { background: var(--danger); color: #fff; }

/* U02 — pin a nav item so it floats into its own group at the top */
.nav-row { display: flex; align-items: center; gap: 2px; }
.nav-row .nav-item { flex: 1; min-width: 0; }
.pin-toggle {
  flex-shrink: 0;
  width: 22px; height: 22px;
  border: none; background: transparent;
  border-radius: var(--radius-xs);
  color: var(--text-3);
  font-size: .78rem;
  opacity: 0;
  transition: opacity .12s var(--ease), color .12s var(--ease), background-color .12s var(--ease);
}
.nav-row:hover .pin-toggle, .pin-toggle.on { opacity: 1; }
.pin-toggle:hover { background: var(--surface-3); color: var(--text); }
.pin-toggle.on { color: var(--warn); }
.nav-group.pinned-group .nav-label { color: var(--warn); }

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 10px;
  flex-shrink: 0;
}

.profile-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 7px 8px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  text-align: left;
}
.profile-chip:hover { background: var(--surface-2); }

/* ------------------------------------------------------------ topbar -- */

.main {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

/* U27 — pull to refresh: the expected gesture for re-syncing on a phone. */
.ptr-indicator {
  display: none;
  position: absolute;
  top: var(--topbar-h); left: 0; right: 0;
  height: 44px;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 25;
  transform: translateY(-44px);
}
.ptr-spin {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  display: grid; place-items: center;
  font-size: .95rem;
  color: var(--brand-600);
  transition: transform .15s var(--ease);
}
.ptr-indicator.armed .ptr-spin { transform: rotate(180deg); }
.ptr-indicator.loading .ptr-spin { animation: ptrSpin .7s linear infinite; }
@keyframes ptrSpin { to { transform: rotate(360deg); } }

.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.hamburger {
  display: none;
  width: 34px; height: 34px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  place-items: center;
  font-size: 1rem;
}

.searchbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 10px;
  min-width: 190px;
  max-width: 340px;
  flex: 1;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-3);
  font-size: .82rem;
  text-align: left;
}
.searchbar:hover { border-color: var(--border-strong); }
.searchbar kbd {
  margin-left: auto;
  font-family: var(--font);
  font-size: .68rem;
  font-weight: 650;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  color: var(--text-3);
}

.today-strip {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 10px;
  height: 30px;
  max-width: 260px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-2);
  font-size: .78rem;
  font-weight: 600;
}
.today-strip:empty,
.today-strip[hidden] { display: none; }
.today-strip .dim { font-weight: 500; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-size: .95rem;
  position: relative;
  transition: all .12s var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-strong); }

.icon-btn .dot {
  position: absolute; top: 6px; right: 6px;
  width: 7px; height: 7px;
  background: var(--danger);
  border: 2px solid var(--surface);
  border-radius: 50%;
}

/* -------------------------------------------------------------- page -- */

.page {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  min-height: 0;
}
/* U44 — scroll-behavior isn't covered by the animation/transition-duration
   override below, so it needs its own reduced-motion guard. */
@media (prefers-reduced-motion: no-preference) {
  .page { scroll-behavior: smooth; }
}

.page-inner { max-width: 1280px; margin: 0 auto; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.page-head .sub { color: var(--text-2); font-size: .875rem; margin-top: 3px; }
.page-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* --------------------------------------------------------- grid sets -- */

.grid { display: grid; gap: 16px; }
.g-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-main { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); align-items: start; }
.g-side { grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); align-items: start; }

@media (max-width: 1100px) {
  .g-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .g-main, .g-side { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 640px) {
  .g-2, .g-3, .g-4 { grid-template-columns: minmax(0, 1fr); }
  .page { padding: 16px; }
  h1, h2, h3, h4, h5, h6 { text-align: center; }
}

/* U48 — 44px is the WCAG-recommended minimum touch target; several icon
   buttons fall short at their compact desktop size. Targets an actual
   touchscreen (not just a narrow window) so mouse users keep the tighter,
   denser controls. */
@media (pointer: coarse) {
  .icon-btn { width: 44px; height: 44px; }
  .pin-toggle { width: 44px; height: 44px; }
  .chip { min-height: 36px; padding: 8px 14px; }
  .habit-day { width: 36px; height: 36px; }
  /* .btn-sm sets height:28px and loads after this file at equal specificity,
     so `.icon-btn.btn-sm` — the row Edit/Delete pair, used around twenty
     times — resolved to 44x28 and stayed under the minimum. This wins it
     back on touch without affecting the desktop density. */
  .icon-btn.btn-sm { width: 44px; height: 44px; }
  .btn-sm { min-height: 40px; }
}

/* ------------------------------------------------------ mobile shell -- */

.scrim {
  position: fixed; inset: 0;
  background: rgba(8, 11, 20, .5);
  backdrop-filter: blur(2px);
  z-index: 39;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s var(--ease);
}
.scrim.show { opacity: 1; pointer-events: auto; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform .22s var(--ease);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }
  .main { grid-column: 1; }
  .hamburger { display: grid; }
  .searchbar { min-width: 0; }
  .searchbar .label, .searchbar kbd { display: none; }
  .today-strip { display: none !important; }
  .page { padding-bottom: calc(70px + env(safe-area-inset-bottom, 0px)); }
  .ptr-indicator { display: flex; }
}

/* ==========================================================================
   U05 — mobile bottom tab bar: the five most-used screens, one tap away
   U06 — floating quick-add: capture is always reachable, wherever you scroll
   ========================================================================== */

.mobile-tabbar { display: none; }
.fab-add { display: none; }

@media (max-width: 860px) {
  .mobile-tabbar {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 35;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom, 0px));
  }
  .tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 2px;
    border-radius: var(--radius-sm);
    color: var(--text-3);
    font-size: .66rem;
    font-weight: 600;
    position: relative;
  }
  .tab-item .ico { font-size: 1.05rem; line-height: 1; }
  .tab-item.active { color: var(--brand-600); }
  .tab-dot {
    position: absolute;
    top: 3px; right: 24%;
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--text-3);
  }
  .tab-dot.alert { background: var(--danger); }

  /* Element+class beats the plain [data-tip] attribute selector in
     components.css that also sets position — that tie previously left
     this button relatively positioned in normal flow instead of fixed. */
  button.fab-add {
    display: grid;
    place-items: center;
    position: fixed;
    right: 16px;
    bottom: calc(66px + env(safe-area-inset-bottom, 0px));
    width: 52px; height: 52px;
    border-radius: 50%;
    background: var(--brand-600);
    color: #fff;
    font-size: 1.6rem;
    line-height: 1;
    box-shadow: var(--shadow-lg);
    border: none;
    z-index: 36;
  }
  .fab-add:active { background: var(--brand-700); }
}

/* ==========================================================================
   U01 — collapsible sidebar (icon-only mode)
   U10 — density setting (compact spacing)
   ========================================================================== */

html[data-sidebar="collapsed"] {
  --sidebar-w: 60px;
}
html[data-sidebar="collapsed"] .logo-text,
html[data-sidebar="collapsed"] .beta-tag,
html[data-sidebar="collapsed"] .nav-label,
html[data-sidebar="collapsed"] .nav-item .lbl,
html[data-sidebar="collapsed"] .nav-item .grow,
html[data-sidebar="collapsed"] .nav-item .count,
html[data-sidebar="collapsed"] .pin-toggle,
html[data-sidebar="collapsed"] .profile-chip .grow {
  display: none;
}
html[data-sidebar="collapsed"] .nav-item { justify-content: center; padding: 8px; }
html[data-sidebar="collapsed"] .sidebar-head { justify-content: center; padding: 0 8px; }
html[data-sidebar="collapsed"] .profile-chip { justify-content: center; }

.sidebar-collapse-btn {
  display: flex; align-items: center; gap: 8px;
  width: 100%;
  margin-top: 6px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-2);
  border-radius: var(--radius-sm);
  font-size: 11.5px;
  font-weight: 600;
}
.sidebar-collapse-btn:hover { background: var(--surface-3); color: var(--text); }
html[data-sidebar="collapsed"] .sidebar-collapse-btn { justify-content: center; }
html[data-sidebar="collapsed"] .sidebar-collapse-btn .lbl { display: none; }

/* Density: shave padding/row-height across the shell without changing type
   scale or removing any information — same content, tighter rhythm. */
html[data-density="compact"] .page { padding: 14px 16px 18px; }
html[data-density="compact"] .card-body { padding: 12px; }
html[data-density="compact"] .card-head { padding: 9px 12px; }
html[data-density="compact"] .list-item { padding: 7px 12px; }
html[data-density="compact"] .hw-row { padding: 7px 12px; }
html[data-density="compact"] .table th, html[data-density="compact"] .table td { padding: 6px 10px; }
html[data-density="compact"] .stat { padding: 10px 11px; }
html[data-density="compact"] .grid { gap: 10px; }
html[data-density="compact"] .nav-item { padding: 5px 10px; }
html[data-density="compact"] .navgroup-label,
html[data-density="compact"] .nav-label { margin-bottom: 3px; }
html[data-density="compact"] .nav-group + .nav-group { margin-top: 10px; }
