/* app.css - dashboard shell + shared components. Plain CSS (see the note in
   admin-ui.css: output.css is a pre-purged Tailwind build, new utility classes
   aren't guaranteed to exist). Shell classes are prefixed "app-". */

:root {
  --app-bg: #f1f5f9;
  --app-surface: #ffffff;
  --app-text: #0f172a;
  --app-muted: #64748b;
  --app-border: #e2e8f0;
  --app-primary: #2563eb;
  --app-primary-dark: #1d4ed8;
  --app-primary-soft: #eff6ff;
  --app-danger: #dc2626;
  --app-success: #16a34a;
  --app-warning: #d97706;
  --app-radius: .75rem;
  --app-shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .04);
  --app-sidebar-w: 248px;
  --app-topbar-h: 56px;
  /* Space the floating bottom bar occupies; 0 on desktop. Pages use it for
     sticky footers so they sit above the bar instead of under it. */
  --app-bottomnav-h: 62px;
  --app-bottom-space: 0px;

  /* crosstable status colours */
  --st-a: #d4e5fd;
  --st-s: #91f3ee;
  --st-p: #d48bfd;
  --st-b: #ffe4c4;
  --st-n: #cbd5e1;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--app-bg);
  color: var(--app-text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
}

.ic {
  width: 22px;
  height: 22px;
  flex: none;
}

.app-skip {
  position: absolute;
  left: -999px;
  top: 8px;
  z-index: 100;
  background: #fff;
  padding: .5rem 1rem;
  border-radius: .5rem;
}

.app-skip:focus {
  left: 8px;
}

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

.app-shell {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.app-sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--app-sidebar-w);
  background: var(--app-surface);
  border-right: 1px solid var(--app-border);
  display: flex;
  flex-direction: column;
  z-index: 40;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
}

.app-brand {
  display: flex;
  align-items: center;
  gap: .6rem;
  height: var(--app-topbar-h);
  padding: 0 1rem;
  font-weight: 700;
  color: var(--app-text);
  text-decoration: none;
  border-bottom: 1px solid var(--app-border);
}

.app-brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--app-primary);
  color: #fff;
}

.app-brand-mark .ic {
  width: 20px;
  height: 20px;
}

.app-sidenav {
  flex: 1;
  overflow-y: auto;
  padding: .75rem .6rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.app-sidelink {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .55rem .7rem;
  border-radius: .55rem;
  color: #334155;
  text-decoration: none;
  font-weight: 500;
}

.app-sidelink:hover {
  background: var(--app-bg);
}

.app-sidelink.is-active {
  background: var(--app-primary-soft);
  color: var(--app-primary-dark);
}

.app-sidesub {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: .15rem .5rem .5rem 2.6rem;
}

.app-sidesublink {
  font-size: .8rem;
  padding: .2rem .55rem;
  border-radius: 999px;
  border: 1px solid var(--app-border);
  color: #475569;
  text-decoration: none;
}

.app-sidesublink:hover {
  border-color: var(--app-primary);
  color: var(--app-primary);
}

.app-sidesublink.is-active {
  background: var(--app-primary);
  border-color: var(--app-primary);
  color: #fff;
}

.app-sideuser {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .75rem;
  border-top: 1px solid var(--app-border);
  padding-bottom: calc(.75rem + env(safe-area-inset-bottom));
}

.app-sideuser-info {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: .6rem;
  text-decoration: none;
  color: inherit;
}

.app-sideuser-info strong,
.app-sideuser-info small {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-sideuser-info small {
  color: var(--app-muted);
}

.app-avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 50%;
  background: #dbeafe;
  color: var(--app-primary-dark);
  font-weight: 700;
  font-size: .8rem;
  text-decoration: none;
}

.app-iconbtn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: .6rem;
  border: 0;
  background: transparent;
  color: #475569;
  cursor: pointer;
}

.app-iconbtn:hover {
  background: var(--app-bg);
}

.app-main {
  flex: 1;
  min-width: 0;
  margin-left: var(--app-sidebar-w);
  display: flex;
  flex-direction: column;
}

.app-topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: .5rem;
  height: calc(var(--app-topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 1.25rem 0;
  background: rgba(241, 245, 249, .85);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--app-border);
}

.app-title {
  flex: 1;
  margin: 0;
  font-size: 1.1rem;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-menubtn,
.app-topavatar {
  display: none;
}

.app-content {
  flex: 1;
  padding: 1.25rem;
  padding-bottom: calc(1.5rem + var(--app-bottom-space));
  max-width: 1600px;
  width: 100%;
  box-sizing: border-box;
}

.app-bare {
  min-height: 100vh;
  min-height: 100dvh;
}

.app-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .4);
  z-index: 35;
}

/* ---------- floating bottom nav (mobile/tablet) ---------- */

.app-bottomnav {
  display: none;
}

@media (max-width: 1023px) {
  :root {
    --app-bottom-space: calc(var(--app-bottomnav-h) + 20px + env(safe-area-inset-bottom));
  }

  .app-sidebar {
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: 0 10px 40px rgba(15, 23, 42, .2);
  }

  body.nav-open .app-sidebar {
    transform: none;
  }

  body.nav-open {
    overflow: hidden;
  }

  .app-main {
    margin-left: 0;
  }

  .app-menubtn,
  .app-topavatar {
    display: inline-grid;
  }

  .app-topbar {
    padding-left: max(.5rem, env(safe-area-inset-left));
    padding-right: max(.75rem, env(safe-area-inset-right));
  }

  .app-content {
    padding: .9rem max(.9rem, env(safe-area-inset-right)) calc(1rem + var(--app-bottom-space)) max(.9rem, env(safe-area-inset-left));
  }

  .app-bottomnav {
    position: fixed;
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 30;
    display: flex;
    height: var(--app-bottomnav-h);
    padding: 6px;
    gap: 4px;
    background: rgba(255, 255, 255, .92);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border: 1px solid var(--app-border);
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(15, 23, 42, .15);
    transition: transform .25s ease, opacity .25s ease;
    max-width: 560px;
    margin: 0 auto;
    box-sizing: border-box;
  }

  .app-bottomnav.is-hidden {
    transform: translateY(calc(100% + 24px));
    opacity: 0;
    pointer-events: none;
  }

  .app-bottomlink {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border-radius: 14px;
    color: #64748b;
    text-decoration: none;
    font-size: .68rem;
    font-weight: 600;
    -webkit-tap-highlight-color: transparent;
  }

  .app-bottomlink span {
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .app-bottomlink.is-active {
    background: var(--app-primary-soft);
    color: var(--app-primary-dark);
  }
}

/* ---------- components ---------- */

.app-page {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.app-card {
  background: var(--app-surface);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius);
  box-shadow: var(--app-shadow);
  padding: 1rem;
}

.app-card h2,
.app-card h3 {
  margin: 0 0 .6rem;
  font-size: 1rem;
  font-weight: 650;
}

.app-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}

.app-kpi {
  display: flex;
  flex-direction: column;
  gap: .35rem;
  text-decoration: none;
  color: inherit;
  transition: border-color .15s, transform .15s;
}

a.app-kpi:hover {
  border-color: var(--app-primary);
}

.app-kpi-label {
  color: var(--app-muted);
  font-size: .82rem;
  font-weight: 600;
}

.app-kpi-value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.app-kpi-sub {
  font-size: .8rem;
  color: var(--app-muted);
}

.app-meter {
  display: flex;
  height: 8px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--st-n);
  gap: 2px;
}

.app-meter span {
  display: block;
}

.app-meter .m-a {
  background: #3b82f6;
}

.app-meter .m-b {
  background: #f59e0b;
}

.app-tiles {
  display: grid;
  gap: .6rem;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}

.app-tile {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .75rem .85rem;
  background: var(--app-surface);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius);
  text-decoration: none;
  color: var(--app-text);
  font-weight: 600;
  font-size: .9rem;
}

.app-tile:hover {
  border-color: var(--app-primary);
  color: var(--app-primary-dark);
}

.app-tile .ic {
  color: var(--app-primary);
}

.app-pills {
  display: flex;
  gap: .4rem;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}

.app-pills::-webkit-scrollbar {
  display: none;
}

.app-pill {
  flex: none;
  padding: .4rem .85rem;
  border-radius: 999px;
  border: 1px solid var(--app-border);
  background: var(--app-surface);
  color: #334155;
  text-decoration: none;
  font-weight: 600;
  font-size: .85rem;
  cursor: pointer;
  white-space: nowrap;
}

.app-pill:hover {
  border-color: var(--app-primary);
}

.app-pill.is-active {
  background: var(--app-primary);
  border-color: var(--app-primary);
  color: #fff;
}

.app-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.app-list li {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  padding: .55rem 0;
  border-bottom: 1px solid var(--app-border);
  font-size: .9rem;
}

.app-list li:last-child {
  border-bottom: 0;
}

.app-alert {
  padding: .7rem .9rem;
  border-radius: .6rem;
  font-size: .9rem;
}

.app-alert-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.app-alert-ok {
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
}

.app-input,
.app-select {
  width: 100%;
  box-sizing: border-box;
  padding: .6rem .75rem;
  border: 1px solid #cbd5e1;
  border-radius: .55rem;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom-on-focus */
  background: #fff;
}

.app-input:focus,
.app-select:focus {
  outline: 2px solid var(--app-primary);
  outline-offset: -1px;
  border-color: transparent;
}

.app-label {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  font-size: .85rem;
  font-weight: 600;
  color: #334155;
}

.app-form {
  display: flex;
  flex-direction: column;
  gap: .9rem;
}

.app-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .6rem 1rem;
  border-radius: .6rem;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.2;
}

.app-btn-primary {
  background: var(--app-primary);
  color: #fff;
}

.app-btn-primary:hover {
  background: var(--app-primary-dark);
}

.app-btn-secondary {
  background: #fff;
  border-color: var(--app-border);
  color: #334155;
}

.app-btn-secondary:hover {
  background: var(--app-bg);
}

.app-btn-danger {
  background: #fff;
  border-color: #fecaca;
  color: var(--app-danger);
}

.app-btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.app-btn-block {
  width: 100%;
}

.app-muted {
  color: var(--app-muted);
  font-size: .85rem;
}

.app-badge {
  display: inline-block;
  padding: .12rem .5rem;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
  background: var(--app-bg);
  color: #475569;
}

/* ---------- crosstable (shared by viewer and editor) ---------- */

.ct-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: .25rem;
}

.ct-grid {
  display: flex;
  gap: 6px;
  width: max-content;
}

.ct-col {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ct-floors {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--app-surface);
}

.ct-cell {
  width: 34px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 5px;
  background: var(--st-a);
  font-size: .78rem;
  font-weight: 600;
  color: #1e3a8a;
  border: 2px solid transparent;
  cursor: pointer;
  user-select: none;
  padding: 0;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
}

.ct-floor {
  width: 26px;
  height: 30px;
  display: grid;
  place-items: center;
  color: var(--app-muted);
  font-size: .75rem;
  font-weight: 700;
}

.ct-cell.st-s { background: var(--st-s); }
.ct-cell.st-p { background: var(--st-p); }
.ct-cell.st-b { background: var(--st-b); color: #92400e; }
.ct-cell.st-n { background: var(--st-n); color: #64748b; }
.ct-cell.is-empty { visibility: hidden; }
.ct-cell.is-selected { border-color: var(--app-primary-dark); box-shadow: 0 0 0 2px #93c5fd; }
.ct-cell.is-current { outline: 3px solid #0f172a; outline-offset: 1px; }
.ct-cell.is-dirty { background-image: radial-gradient(circle at calc(100% - 5px) 5px, var(--app-warning) 0 3px, transparent 3.5px); }
.ct-cell.has-split { box-shadow: inset 0 -3px 0 #16a34a; }

.ct-legend {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem .9rem;
  font-size: .8rem;
  color: #475569;
}

.ct-legend span {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
}

.ct-legend i {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  display: inline-block;
}

/* ---------- auth pages ---------- */

.app-auth {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  box-sizing: border-box;
}

.app-auth-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 1rem;
  box-shadow: 0 10px 40px rgba(15, 23, 42, .08);
  border: 1px solid var(--app-border);
  padding: 1.75rem 1.5rem;
  box-sizing: border-box;
}

.app-auth-card h1 {
  margin: 1rem 0 1.25rem;
  font-size: 1.35rem;
  text-align: center;
}

.app-check {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: .9rem;
}

.app-check input {
  width: 18px;
  height: 18px;
}

/* Legacy pages inside the shell: undo their full-page assumptions. */
.has-shell #crosstable-container {
  max-width: 100%;
}

@media (max-width: 640px) {
  .app-grid {
    grid-template-columns: 1fr;
  }
}
