/* WhatsApp SaaS Portal — responsive UI foundation */

:root {
    --portal-header-h: 3.5rem;
    --portal-bottom-nav-h: 4rem;
    --portal-sidebar-w: 16rem;
    --portal-page-max: 90rem;
    --portal-touch-min: 44px;
    --portal-radius: 1rem;
    --portal-radius-lg: 1.25rem;
    --portal-gutter: clamp(0.75rem, 2.5vw, 2rem);
    --portal-safe-bottom: env(safe-area-inset-bottom, 0px);
    --portal-safe-top: env(safe-area-inset-top, 0px);
}

*, *::before, *::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    overflow-x: hidden;
}

img, video {
    max-width: 100%;
    height: auto;
}

/* React Flow edge SVGs must stay un-sized; width/height 100% breaks wire coordinates. */
.react-flow__edges svg,
.react-flow__connectionline {
    max-width: none;
    width: auto;
    height: auto;
    overflow: visible;
}

/* ---------- Page layout ---------- */
.portal-shell-main {
    width: 100%;
    min-width: 0;
}

.portal-page {
    width: 100%;
    max-width: var(--portal-page-max);
    margin-inline: auto;
}

.portal-page-header {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: clamp(1.25rem, 3vw, 2rem);
}

@media (min-width: 1024px) {
    .portal-page-header {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }
}

.portal-page-title {
    font-size: clamp(1.35rem, 2.5vw, 1.75rem);
    font-weight: 700;
    line-height: 1.2;
    word-break: break-word;
}

.portal-page-subtitle {
    margin-top: 0.25rem;
    font-size: clamp(0.875rem, 1.5vw, 1rem);
    line-height: 1.5;
}

.portal-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.portal-actions > a,
.portal-actions > button,
.portal-actions > form {
    flex-shrink: 0;
}

/* ---------- Cards & surfaces ---------- */
.portal-card {
    border-radius: var(--portal-radius-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 23, 42, 0.6);
    padding: clamp(1rem, 2.5vw, 1.5rem);
}

html.light .portal-card {
    background: #ffffff;
    border-color: rgba(15, 23, 42, 0.12);
    box-shadow: 0 10px 25px -12px rgba(15, 23, 42, 0.14);
}

/* ---------- Forms ---------- */
.portal-input,
.portal-select,
.portal-textarea {
    width: 100%;
    min-height: var(--portal-touch-min);
    border-radius: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(2, 6, 23, 0.6);
    padding: 0.625rem 0.875rem;
    font-size: 16px; /* prevents iOS zoom on focus */
    line-height: 1.4;
}

html.light .portal-input,
html.light .portal-select,
html.light .portal-textarea {
    background: #ffffff;
    border-color: rgba(15, 23, 42, 0.12);
    color: #0f172a;
}

.portal-filter-row {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

@media (min-width: 640px) {
    .portal-filter-row {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: stretch;
    }

    .portal-filter-row > * {
        flex: 1 1 auto;
        min-width: 8rem;
    }

    .portal-filter-row > input[type="search"],
    .portal-filter-row > input[name="q"] {
        flex: 2 1 12rem;
        min-width: 0;
    }
}

/* ---------- Responsive grids ---------- */
.portal-grid-stats {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .portal-grid-stats {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (min-width: 1280px) {
    .portal-grid-stats {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .portal-grid-stats.cols-3 {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .portal-grid-stats.cols-5 {
        grid-template-columns: repeat(5, minmax(0, 1fr));
    }
}

.portal-grid-3 {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .portal-grid-3 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (min-width: 1280px) {
    .portal-grid-3 {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

.portal-grid-2 {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}

@media (min-width: 768px) {
    .portal-grid-2 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* ---------- Tables ---------- */
.portal-table-wrap {
    position: relative;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--portal-radius-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 23, 42, 0.6);
}

html.light .portal-table-wrap {
    background: #ffffff;
    border-color: rgba(15, 23, 42, 0.12);
}

.portal-table-wrap::after {
    content: '';
    position: sticky;
    left: 100%;
    top: 0;
    display: block;
    width: 1.5rem;
    height: 1px;
    pointer-events: none;
}

@media (max-width: 767px) {
    .portal-table-scroll-hint {
        display: block;
        padding: 0.5rem 1rem;
        font-size: 0.6875rem;
        text-align: center;
        color: #64748b;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    }

    html.light .portal-table-scroll-hint {
        color: #64748b;
        border-color: rgba(15, 23, 42, 0.08);
    }
}

@media (min-width: 768px) {
    .portal-table-scroll-hint {
        display: none;
    }
}

.portal-table {
    width: 100%;
    min-width: 36rem;
    text-align: left;
    font-size: 0.875rem;
}

.portal-table th,
.portal-table td {
    padding: 0.75rem 1rem;
    vertical-align: top;
}

.portal-table th {
    white-space: nowrap;
}

/* Mobile card-style table (optional class on tbody rows) */
@media (max-width: 767px) {
    .portal-table-cards thead {
        display: none;
    }

    .portal-table-cards tbody tr {
        display: block;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
        padding: 0.75rem 1rem;
    }

    html.light .portal-table-cards tbody tr {
        border-color: rgba(15, 23, 42, 0.08);
    }

    .portal-table-cards tbody tr:last-child {
        border-bottom: none;
    }

    .portal-table-cards tbody td {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
        padding: 0.35rem 0;
        border: none;
        min-width: 0 !important;
    }

    .portal-table-cards tbody td::before {
        content: attr(data-label);
        flex-shrink: 0;
        font-weight: 600;
        font-size: 0.6875rem;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: #64748b;
        max-width: 40%;
    }

    .portal-table-cards tbody td:last-child {
        justify-content: flex-end;
        padding-top: 0.5rem;
    }

    .portal-table-cards tbody td:last-child::before {
        display: none;
    }

    .portal-table-cards {
        min-width: 0 !important;
    }
}

/* ---------- Mobile drawer nav ---------- */
.portal-drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 60;
    background: rgba(2, 6, 23, 0.65);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    backdrop-filter: blur(2px);
}

.portal-drawer-backdrop.is-open {
    opacity: 1;
    visibility: visible;
}

.portal-drawer {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 70;
    display: flex;
    flex-direction: column;
    width: min(20rem, 88vw);
    max-width: 100%;
    height: 100%;
    height: 100dvh;
    padding-top: var(--portal-safe-top);
    padding-bottom: var(--portal-safe-bottom);
    background: rgba(15, 23, 42, 0.98);
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    overflow: hidden;
}

html.light .portal-drawer {
    background: #ffffff;
    border-color: rgba(15, 23, 42, 0.12);
}

.portal-drawer.is-open {
    transform: translateX(0);
}

.portal-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: var(--portal-header-h);
    padding: 0 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.portal-drawer-nav {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0.75rem;
}

.portal-drawer-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: var(--portal-touch-min);
    padding: 0.625rem 0.75rem;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    text-decoration: none;
    color: #cbd5e1;
    transition: background 0.15s ease;
}

.portal-drawer-link:hover,
.portal-drawer-link.is-active {
    background: rgba(255, 255, 255, 0.06);
    color: #f8fafc;
}

html.light .portal-drawer-link {
    color: #334155;
}

html.light .portal-drawer-link:hover,
html.light .portal-drawer-link.is-active {
    background: #f1f5f9;
    color: #0f172a;
}

.portal-drawer-footer {
    flex-shrink: 0;
    padding: 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.portal-menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--portal-touch-min);
    min-height: var(--portal-touch-min);
    border-radius: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: transparent;
    color: inherit;
    cursor: pointer;
}

.portal-menu-btn:hover {
    background: rgba(255, 255, 255, 0.06);
}

body.portal-drawer-open {
    overflow: hidden;
}

/* ---------- Bottom nav (mobile) ---------- */
.portal-bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    display: flex;
    align-items: stretch;
    min-height: calc(var(--portal-bottom-nav-h) + var(--portal-safe-bottom));
    padding-bottom: var(--portal-safe-bottom);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 23, 42, 0.97);
    backdrop-filter: blur(8px);
}

html.light .portal-bottom-nav {
    background: rgba(255, 255, 255, 0.97);
    border-color: rgba(15, 23, 42, 0.12);
}

.portal-bottom-nav a,
.portal-bottom-nav button {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.125rem;
    min-width: 0;
    padding: 0.375rem 0.25rem;
    font-size: 0.625rem;
    line-height: 1.2;
    text-decoration: none;
    color: #94a3b8;
    background: none;
    border: none;
    cursor: pointer;
}

.portal-bottom-nav a.is-active,
.portal-bottom-nav button.is-active {
    color: #22c55e;
}

.portal-bottom-nav span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-inline: 0.125rem;
}

.portal-bottom-nav.admin-nav a.is-active,
.portal-bottom-nav.admin-nav button.is-active {
    color: #6366f1;
}

@media (min-width: 1024px) {
    .portal-bottom-nav,
    .portal-mobile-header {
        display: none !important;
    }
}

@media (max-width: 1023px) {
    .portal-desktop-sidebar {
        display: none !important;
    }

    .portal-shell-main {
        padding-bottom: calc(var(--portal-bottom-nav-h) + var(--portal-safe-bottom));
    }

    .portal-shell-main.no-bottom-nav {
        padding-bottom: 0;
    }
}

/* ---------- Mobile header ---------- */
.portal-mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    min-height: var(--portal-header-h);
    padding: 0.5rem var(--portal-gutter);
    padding-top: calc(0.5rem + var(--portal-safe-top));
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 23, 42, 0.92);
    backdrop-filter: blur(8px);
    position: sticky;
    top: 0;
    z-index: 40;
}

html.light .portal-mobile-header {
    background: rgba(255, 255, 255, 0.95);
    border-color: rgba(15, 23, 42, 0.12);
}

/* ---------- Auth pages ---------- */
.portal-auth-wrap {
    display: flex;
    min-height: 100vh;
    min-height: 100dvh;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: clamp(1rem, 4vw, 3rem) var(--portal-gutter);
    padding-top: calc(clamp(1rem, 4vw, 3rem) + var(--portal-safe-top));
    padding-bottom: calc(clamp(1rem, 4vw, 3rem) + var(--portal-safe-bottom));
}

.portal-auth-card {
    width: 100%;
    max-width: 28rem;
    border-radius: var(--portal-radius-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 23, 42, 0.6);
    padding: clamp(1.25rem, 4vw, 2rem);
    box-shadow: 0 20px 40px -20px rgba(0, 0, 0, 0.4);
}

html.light .portal-auth-card {
    background: #ffffff;
    border-color: rgba(15, 23, 42, 0.12);
}

/* ---------- Landing mobile nav ---------- */
.portal-landing-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--portal-touch-min);
    min-height: var(--portal-touch-min);
    border-radius: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: transparent;
    color: inherit;
    cursor: pointer;
}

@media (min-width: 768px) {
    .portal-landing-nav-toggle {
        display: none;
    }

    /* Landing page uses landing.css grid — skip flex override there */
    .portal-landing-nav-menu:not(.landing-nav-panel) {
        display: flex !important;
        position: static !important;
        flex-direction: row !important;
        align-items: center;
        gap: 1rem;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        box-shadow: none !important;
    }
}

@media (max-width: 767px) {
    .portal-landing-nav-menu:not(.landing-nav-panel) {
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        flex-direction: column;
        gap: 0.5rem;
        padding: 1rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(15, 23, 42, 0.98);
        backdrop-filter: blur(8px);
    }

    html.light .portal-landing-nav-menu:not(.landing-nav-panel) {
        background: rgba(255, 255, 255, 0.98);
        border-color: rgba(15, 23, 42, 0.12);
    }

    .portal-landing-nav-menu:not(.landing-nav-panel).is-open {
        display: flex;
    }

    .portal-landing-header-inner {
        position: relative;
    }
}

/* ---------- Utility ---------- */
.portal-main-content {
    flex: 1;
    width: 100%;
    min-width: 0;
    padding: var(--portal-gutter);
}

@media (min-width: 1024px) {
    .portal-main-content {
        padding: clamp(1.25rem, 2.5vw, 2rem);
    }
}

.portal-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--portal-touch-min);
    padding: 0.5rem 1rem;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    text-decoration: none;
    white-space: nowrap;
}

.portal-btn-sm {
    min-height: 2.25rem;
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
}

@media (max-width: 639px) {
    .portal-hide-mobile {
        display: none !important;
    }
}

@media (min-width: 640px) {
    .portal-hide-desktop {
        display: none !important;
    }
}

/* Pagination responsive */
nav[role="navigation"] svg {
    width: 1.25rem;
    height: 1.25rem;
}

/* Prevent layout shift from scrollbar */
@supports (scrollbar-gutter: stable) {
    html {
        scrollbar-gutter: stable;
    }
}

/* ---------- Code / pre blocks ---------- */
.portal-code,
.portal-pre {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 0.75rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(2, 6, 23, 0.8);
    padding: 0.75rem 1rem;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.75rem;
    line-height: 1.5;
    word-break: break-word;
    white-space: pre-wrap;
}

html.light .portal-code,
html.light .portal-pre {
    background: #f8fafc;
    border-color: rgba(15, 23, 42, 0.12);
    color: #0f172a;
}

/* ---------- Flow builder (mobile) ---------- */
#flow-builder-root {
    height: calc(100dvh - var(--portal-header-h) - var(--portal-bottom-nav-h) - 2rem);
    min-height: 420px;
}

@media (min-width: 1024px) {
    #flow-builder-root {
        height: calc(100vh - 6rem);
        min-height: 520px;
    }
}

.flow-builder-shell .react-flow__minimap {
    display: none;
}

@media (min-width: 768px) {
    .flow-builder-shell .react-flow__minimap {
        display: block;
    }
}

@media (max-width: 767px) {
    .flow-builder-shell > div:last-child {
        position: relative;
    }

    .flow-builder-shell aside {
        position: fixed;
        top: 0;
        bottom: 0;
        z-index: 80;
        width: min(18rem, 92vw);
        max-width: 92vw;
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
    }

    .flow-builder-shell aside:first-of-type {
        left: 0;
    }

    .flow-builder-shell aside:last-of-type {
        right: 0;
    }

    .flow-builder-shell button[class*="writing-mode"] {
        min-width: 2.75rem;
        min-height: 2.75rem;
    }

    .flow-builder-shell .portal-btn-toolbar {
        min-height: 2.5rem;
        padding: 0.375rem 0.625rem;
        font-size: 0.75rem;
    }
}
