/* ============================================================================
   APP SHELL  ·  Sidebar, Topbar, Layout, Command Palette, Auth
   ============================================================================ */

.layout { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }

body.is-tablet {
    --tablet-safe-top: max(32px, env(safe-area-inset-top, 0px));
    --tablet-safe-bottom: max(24px, env(safe-area-inset-bottom, 0px));
}
body.is-tablet .layout { min-height: 100dvh; }
body.is-tablet .sidebar { height: 100dvh; }
body.is-tablet .sidebar-brand { padding-top: calc(var(--sp-5) + var(--tablet-safe-top)); }
body.is-tablet .sidebar-foot { padding-bottom: calc(var(--sp-4) + var(--tablet-safe-bottom)); }
body.is-tablet .topbar {
    height: calc(var(--topbar-h) + var(--tablet-safe-top));
    padding-top: var(--tablet-safe-top);
}
body.is-tablet .content { padding-bottom: calc(var(--sp-12) + var(--tablet-safe-bottom)); }

/* ------------------------------- Sidebar ------------------------------- */
.sidebar {
    position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
    background: var(--sidebar-bg); border-right: 1px solid var(--sidebar-line);
    z-index: 40;
}
.sidebar-brand {
    display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-5) var(--sp-5) var(--sp-4);
}
.brand-mark {
    width: 40px; height: 40px; border-radius: var(--r-sm); flex: 0 0 auto; overflow: hidden;
    display: grid; place-items: center; color: var(--accent-ink); font-family: var(--font-display); font-weight: 700; font-size: 1.1rem;
    background: linear-gradient(140deg, var(--accent-2), var(--accent));
    box-shadow: 0 6px 18px -6px var(--accent-glow);
}
.brand-mark img { width: 100%; height: 100%; object-fit: cover; display: block; }
.brand-mark.has-logo { background: var(--surface-2); box-shadow: var(--shadow-sm); }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text .name { font-family: var(--font-display); font-weight: 600; font-size: var(--text-md); color: #f5f0ea; }
.brand-text .dept { font-size: var(--text-2xs); color: rgba(245,240,235,.42); text-transform: uppercase; letter-spacing: .1em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar-nav { flex: 1; overflow-y: auto; padding: var(--sp-2) var(--sp-3) var(--sp-4); }
.sidebar-nav::-webkit-scrollbar { width: 6px; }
.nav-group { margin-bottom: var(--sp-4); }
.nav-group-label { padding: var(--sp-3) var(--sp-3) var(--sp-2); font-size: var(--text-2xs); text-transform: uppercase; letter-spacing: .14em; color: rgba(245,240,235,.3); font-weight: 600; }
.nav-item {
    display: flex; align-items: center; gap: var(--sp-3); padding: .6rem .7rem; border-radius: var(--r-sm);
    color: rgba(245,240,235,.55); font-size: var(--text-md); font-weight: 500; position: relative;
    transition: background var(--t-fast), color var(--t-fast);
}
.nav-item .ico { width: 19px; height: 19px; flex: 0 0 auto; color: rgba(245,240,235,.42); transition: color var(--t-fast); }
.nav-item:hover { background: rgba(245,240,235,.05); color: rgba(245,240,235,.92); }
.nav-item:hover .ico { color: rgba(245,240,235,.75); }
.nav-item.active { background: var(--accent-soft); color: #fff; font-weight: 600; }
.nav-item.active .ico { color: var(--accent); }
.nav-item.active::before { content: ''; position: absolute; left: -3px; top: 50%; transform: translateY(-50%); width: 3px; height: 60%; border-radius: 99px; background: var(--accent); }
.nav-item .nav-badge { margin-left: auto; font-size: var(--text-2xs); font-weight: 700; padding: .1rem .4rem; border-radius: 99px; background: var(--accent); color: var(--accent-ink); }

.sidebar-foot { padding: var(--sp-4); border-top: 1px solid var(--sidebar-line); }
.agency-tag { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3); border-radius: var(--r-sm); background: rgba(245,240,235,.04); }
.agency-tag .dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; box-shadow: 0 0 0 3px rgba(255,255,255,.04); }
.agency-tag .meta { min-width: 0; }
.agency-tag .meta .t { font-size: var(--text-sm); font-weight: 600; color: #f5f0ea; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agency-tag .meta .s { font-size: var(--text-2xs); color: rgba(245,240,235,.4); }

/* ------------------------------- Main ---------------------------------- */
.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
    position: sticky; top: 0; z-index: 30; height: var(--topbar-h);
    display: flex; align-items: center; gap: var(--sp-3); padding: 0 var(--sp-6);
    background: color-mix(in srgb, var(--bg-body) 78%, transparent);
    backdrop-filter: blur(14px) saturate(140%);
    border-bottom: 1px solid var(--line);
}
.topbar-title { font-family: var(--font-display); font-weight: 600; font-size: var(--text-lg); }
.topbar-spacer { flex: 1; }

.icon-btn {
    width: 38px; height: 38px; border-radius: var(--r-sm); display: grid; place-items: center;
    color: var(--text-dim); border: 1px solid transparent; transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast); position: relative;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn .ico { width: 19px; height: 19px; }
.icon-btn .dot-badge { position: absolute; top: 8px; right: 9px; width: 7px; height: 7px; border-radius: 50%; background: var(--danger); border: 2px solid var(--bg-body); }

/* Search trigger */
.search-trigger {
    display: flex; align-items: center; gap: var(--sp-3); height: 38px; padding: 0 .8rem; min-width: 220px;
    background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-sm); color: var(--text-mute); font-size: var(--text-md);
    transition: border-color var(--t-fast), background var(--t-fast);
}
.search-trigger:hover { border-color: var(--line-2); }
.search-trigger .ico { width: 17px; height: 17px; }
.search-trigger .kbd { margin-left: auto; }

/* Tenant switcher */
.tenant-switch { display: flex; align-items: center; gap: var(--sp-2); height: 38px; padding: 0 .7rem; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-sm); font-size: var(--text-sm); font-weight: 600; color: var(--text); }
.tenant-switch .dot { width: 8px; height: 8px; border-radius: 50%; }
.tenant-switch .ico { width: 15px; height: 15px; color: var(--text-mute); }

/* User chip */
.user-chip { display: flex; align-items: center; gap: var(--sp-3); height: 42px; padding: 0 .35rem 0 .35rem; border-radius: var(--r-sm); transition: background var(--t-fast); }
.user-chip:hover { background: var(--surface-2); }
.user-chip .who { line-height: 1.2; text-align: right; }
.user-chip .who .n { font-size: var(--text-sm); font-weight: 600; }
.user-chip .who .r { font-size: var(--text-2xs); color: var(--text-mute); }

/* ------------------------------- Content ------------------------------- */
.content { flex: 1; padding: var(--sp-8) var(--sp-6) var(--sp-12); }
.content-inner { max-width: var(--container); margin-inline: auto; }

.page-head { margin-bottom: var(--sp-8); }
.page-head .eyebrow { margin-bottom: var(--sp-2); }
.page-head h1 { font-size: var(--text-3xl); }
.page-head .lead { color: var(--text-mute); margin-top: var(--sp-2); max-width: 64ch; }
.page-actions { display: flex; gap: var(--sp-3); flex-wrap: wrap; }

.breadcrumb { display: flex; align-items: center; gap: var(--sp-2); font-size: var(--text-sm); color: var(--text-mute); }
.breadcrumb a:hover { color: var(--text); }
.breadcrumb .ico { width: 14px; height: 14px; }

/* ------------------------------- Mobile -------------------------------- */
.menu-toggle { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 1400px) {
    body.is-tablet { --sidebar-w: 240px; }
    body.is-tablet .search-trigger { width: 38px; min-width: 38px; padding: 0; justify-content: center; }
    body.is-tablet .search-trigger .label-text,
    body.is-tablet .search-trigger .kbd,
    body.is-tablet .user-chip .who { display: none; }
}

@media (max-width: 1180px) {
    body.is-tablet .tenant-switch .label-text { display: none; }
}

@media (max-width: 1024px) {
    .layout { grid-template-columns: 1fr; }
    .sidebar {
        position: fixed; left: 0; top: 0; width: var(--sidebar-w); transform: translateX(-100%);
        transition: transform var(--t-base) var(--ease); box-shadow: var(--shadow-lg);
    }
    body.nav-open .sidebar { transform: none; }
    body.nav-open .sidebar-backdrop { display: block; position: fixed; inset: 0; background: var(--overlay); z-index: 35; }
    .menu-toggle { display: grid; }
    .search-trigger { min-width: 0; }
    .search-trigger .label-text, .search-trigger .kbd { display: none; }
    .user-chip .who { display: none; }
}
@media (max-width: 600px) {
    .content { padding: var(--sp-6) var(--sp-4) var(--sp-10); }
    .tenant-switch .label-text { display: none; }
}

@media (max-width: 720px) {
    body.is-tablet .topbar { gap: var(--sp-2); padding-inline: var(--sp-3); }
    body.is-tablet .topbar-title { max-width: 30vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    body.is-tablet .content { padding-inline: var(--sp-4); }
}

/* ------------------------------- Command palette ----------------------- */
.cmdk-overlay { position: fixed; inset: 0; background: var(--overlay); backdrop-filter: blur(8px); z-index: 150; display: flex; align-items: flex-start; justify-content: center; padding-top: 14vh; opacity: 0; pointer-events: none; transition: opacity var(--t-fast); }
.cmdk-overlay.open { opacity: 1; pointer-events: auto; }
.cmdk { width: 100%; max-width: 580px; background: var(--elevated); border: 1px solid var(--line-2); border-radius: var(--r-lg); box-shadow: var(--shadow-lg); overflow: hidden; transform: translateY(-10px) scale(.99); transition: transform var(--t-base) var(--ease); }
.cmdk-overlay.open .cmdk { transform: none; }
.cmdk-input-row { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--line); }
.cmdk-input-row .ico { color: var(--text-mute); width: 20px; height: 20px; }
.cmdk-input { flex: 1; background: none; border: none; font-size: var(--text-lg); color: var(--text); outline: none; }
.cmdk-results { max-height: 56vh; overflow-y: auto; padding: var(--sp-2); }
.cmdk-item { display: flex; align-items: center; gap: var(--sp-3); padding: .65rem .8rem; border-radius: var(--r-sm); color: var(--text-dim); cursor: pointer; }
.cmdk-item .ico { width: 18px; height: 18px; color: var(--text-mute); }
.cmdk-item.sel, .cmdk-item:hover { background: var(--accent-soft); color: var(--text); }
.cmdk-item.sel .ico { color: var(--accent); }
.cmdk-empty { padding: var(--sp-8); text-align: center; color: var(--text-mute); font-size: var(--text-md); }

/* ============================================================================
   AUTH (Login / Onboarding)
   ============================================================================ */
body.auth {
    display: grid; grid-template-columns: 1.05fr .95fr; min-height: 100vh;
}
.auth-aside {
    position: relative; overflow: hidden; padding: var(--sp-12);
    display: flex; flex-direction: column; justify-content: space-between;
    background:
        radial-gradient(820px 520px at 18% 12%, var(--accent-soft), transparent 58%),
        linear-gradient(160deg, #18130f 0%, #0c0a09 60%);
    border-right: 1px solid var(--line);
}
.auth-aside .glow { position: absolute; width: 460px; height: 460px; border-radius: 50%; right: -120px; bottom: -160px;
    background: radial-gradient(circle, var(--accent-glow), transparent 65%); filter: blur(20px); opacity: .5; }
.auth-brand { display: flex; align-items: center; gap: var(--sp-3); position: relative; }
.auth-brand .brand-mark { width: 48px; height: 48px; font-size: 1.3rem; }
.auth-brand .name { font-family: var(--font-display); font-size: var(--text-lg); font-weight: 600; color: #f5f0ea; }
.auth-hero { position: relative; max-width: 30ch; }
.auth-hero h1 { font-size: clamp(2rem, 1.4rem + 2.6vw, 3.1rem); color: #f7f2ec; line-height: 1.08; }
.auth-hero p { color: rgba(245,240,235,.55); margin-top: var(--sp-4); font-size: var(--text-lg); }
.auth-features { position: relative; display: flex; flex-direction: column; gap: var(--sp-3); }
.auth-feature { display: flex; align-items: center; gap: var(--sp-3); color: rgba(245,240,235,.7); font-size: var(--text-md); }
.auth-feature .ico { color: var(--accent); width: 18px; height: 18px; }

.auth-main { display: grid; place-items: center; padding: var(--sp-8); background: var(--bg-body); }
.auth-card { width: 100%; max-width: 400px; }
.auth-card h2 { font-size: var(--text-2xl); }
.auth-card .sub { color: var(--text-mute); margin-top: var(--sp-2); margin-bottom: var(--sp-8); }
.auth-foot { margin-top: var(--sp-6); text-align: center; font-size: var(--text-sm); color: var(--text-mute); }
.auth-demo { margin-top: var(--sp-6); padding: var(--sp-4); border: 1px dashed var(--line-2); border-radius: var(--r-md); font-size: var(--text-sm); color: var(--text-dim); background: var(--surface-2); }
.auth-demo code { color: var(--accent); }

@media (max-width: 880px) {
    body.auth { grid-template-columns: 1fr; }
    .auth-aside { display: none; }
}
