/* =====================================================
   Nestoryx — Mobile-first PWA design system
   Uses CSS custom properties for easy theming per org.
   ===================================================== */

/* ---- Custom properties ---- */
:root {
    --brand-600: #1a56db;
    --brand-700: #1648c0;
    --brand-100: #e8eefb;
    --brand-50: #f0f5ff;

    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-500: #6b7280;
    --gray-700: #374151;
    --gray-900: #111827;

    --green-100: #d1fae5;
    --green-700: #047857;
    --red-100: #fee2e2;
    --red-600: #dc2626;
    --red-700: #b91c1c;
    --amber-100: #fef3c7;
    --amber-700: #b45309;
    --blue-100: #dbeafe;
    --blue-700: #1d4ed8;

    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 16px;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / .05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / .1), 0 1px 2px -1px rgb(0 0 0 / .1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / .1), 0 2px 4px -2px rgb(0 0 0 / .1);

    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    --font-mono: 'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;

    --header-h: 56px;
    --bottom-nav-h: 64px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---- Reset ---- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

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

body {
    font-family: var(--font);
    color: var(--gray-900);
    background: var(--gray-50);
    line-height: 1.5;
    min-height: 100dvh;
    padding-top: var(--header-h);
    padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
    -webkit-font-smoothing: antialiased;
}

img {
    max-width: 100%;
    display: block;
}

a {
    color: var(--brand-600);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

button,
input,
select,
textarea {
    font: inherit;
}

/* ---- Utilities ---- */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.text-muted {
    color: var(--gray-500);
}

.text-sm {
    font-size: .875rem;
}

.text-danger {
    color: var(--red-600);
}

.text-center {
    text-align: center;
}

.link-sm {
    font-size: .875rem;
}

.view-all {
    display: block;
    text-align: center;
    padding: 8px;
}

/* ---- Header ---- */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-h);
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    z-index: 100;
    padding-top: env(safe-area-inset-top, 0px);
}

.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    padding: 0 16px;
    max-width: 768px;
    margin: 0 auto;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 1.125rem;
    color: var(--gray-900);
    text-decoration: none;
}

.header-nav {
    display: flex;
    align-items: center;
    gap: 4px;
}

.nav-icon {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    background: none;
    border: none;
    cursor: pointer;
    color: var(--gray-700);
}

.nav-icon:hover {
    background: var(--gray-100);
}

.badge {
    position: absolute;
    top: 6px;
    right: 6px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    background: var(--red-600);
    color: #fff;
    border-radius: 8px;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
}

/* ---- Bottom navigation ---- */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--bottom-nav-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: #fff;
    border-top: 1px solid var(--gray-200);
    display: flex;
    z-index: 100;
}

.bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: var(--gray-500);
    text-decoration: none;
    font-size: .7rem;
    font-weight: 500;
    transition: color .15s;
    padding-top: 6px;
}

.bottom-nav-item.active,
.bottom-nav-item:hover {
    color: var(--brand-600);
}

.bottom-nav-item.active svg {
    stroke: var(--brand-600);
}

/* ---- Drawer / side menu ---- */
.drawer {
    position: fixed;
    inset: 0;
    z-index: 200;
    pointer-events: none;
}

.drawer.open {
    pointer-events: auto;
}

.drawer-overlay {
    position: absolute;
    inset: 0;
    background: rgb(0 0 0 / .4);
    opacity: 0;
    transition: opacity .25s;
}

.drawer.open .drawer-overlay {
    opacity: 1;
}

.drawer-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(300px, 85vw);
    background: #fff;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform .25s cubic-bezier(.4, 0, .2, 1);
    overflow-y: auto;
}

.drawer.open .drawer-panel {
    transform: none;
}

.drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 16px 16px;
    border-bottom: 1px solid var(--gray-200);
}

.drawer-name {
    font-weight: 600;
    font-size: 1rem;
}

.drawer-close {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.1rem;
    color: var(--gray-500);
}

.drawer-links {
    list-style: none;
    padding: 8px 0;
    flex: 1;
}

.drawer-links li a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    color: var(--gray-700);
    font-size: .95rem;
    text-decoration: none;
}

.drawer-links li a:hover {
    background: var(--gray-50);
}

.drawer-section {
    padding: 16px 16px 4px;
    font-size: .75rem;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: .05em;
}

.drawer-footer {
    border-top: 1px solid var(--gray-200);
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.drawer-footer a {
    font-size: .875rem;
    color: var(--gray-700);
}

.logout-link {
    color: var(--red-600) !important;
}

/* ---- Main content area ---- */
.app-main {
    max-width: 768px;
    margin: 0 auto;
    padding: 16px 16px 24px;
}

/* ---- Page header ---- */
.page-header {
    margin-bottom: 20px;
}

.page-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.25;
}

.page-header .text-muted {
    margin-top: 2px;
    font-size: .9375rem;
}

.back-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: .875rem;
    margin-bottom: 8px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 24px 0 12px;
}

.section-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
}

/* ---- Cards ---- */
.card {
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 12px;
}

.card-label {
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--gray-500);
    margin-bottom: 6px;
}

/* Lease card */
.lease-card {
    background: var(--brand-600);
    color: #fff;
    border-color: transparent;
}

.lease-card .card-label {
    color: rgb(255 255 255 / .7);
}

.lease-address {
    font-size: 1.125rem;
    font-weight: 600;
}

.lease-unit {
    font-size: .875rem;
    opacity: .85;
    margin-top: 2px;
}

.lease-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 14px;
}

.lease-meta .link-sm {
    color: #fff;
    font-weight: 500;
}

/* Payment card */
.payment-card {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.payment-card--urgent {
    border-color: var(--amber-700);
    background: var(--amber-100);
}

.payment-amount {
    font-size: 2rem;
    font-weight: 700;
}

.payment-due {
    font-size: .875rem;
    color: var(--gray-500);
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: 32px 16px;
    color: var(--gray-500);
}

.empty-state svg {
    margin: 0 auto 12px;
    stroke: var(--gray-300);
}

/* ---- KPI row (landlord) ---- */
.kpi-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 20px;
}

.kpi-card {
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 14px;
    text-align: center;
}

.kpi-card--alert {
    border-color: var(--red-600);
    background: var(--red-100);
}

.kpi-value {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1;
}

.kpi-label {
    font-size: .8125rem;
    color: var(--gray-500);
    margin-top: 4px;
}

/* ---- List cards ---- */
.list-cards {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.list-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 14px 12px 14px 14px;
    text-decoration: none;
    color: inherit;
    box-shadow: var(--shadow-sm);
    transition: box-shadow .15s;
}

.list-card:hover {
    box-shadow: var(--shadow);
}

.list-card--alert {
    border-color: var(--red-600);
    background: var(--red-100);
}

.list-card-main {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}

.list-card-title {
    font-weight: 600;
    font-size: .9375rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.list-card-sub {
    font-size: .8125rem;
    color: var(--gray-500);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.list-card-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* ---- Property grid ---- */
.property-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.property-card {
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 14px;
}

.property-name {
    font-weight: 600;
}

.property-address {
    font-size: .8125rem;
    color: var(--gray-500);
    margin-top: 2px;
}

/* ---- Badges ---- */
.badge-urgency {
    font-size: .7rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 4px;
    text-transform: uppercase;
    letter-spacing: .04em;
}

.badge-urgency--routine {
    background: var(--gray-100);
    color: var(--gray-700);
}

.badge-urgency--urgent {
    background: var(--amber-100);
    color: var(--amber-700);
}

.badge-urgency--emergency {
    background: var(--red-100);
    color: var(--red-700);
}

.status-badge {
    font-size: .75rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 12px;
}

.status-badge--open,
.status-badge--submitted {
    background: var(--blue-100);
    color: var(--blue-700);
}

.status-badge--in_progress {
    background: var(--amber-100);
    color: var(--amber-700);
}

.status-badge--completed,
.status-badge--paid,
.status-badge--approved {
    background: var(--green-100);
    color: var(--green-700);
}

.status-badge--cancelled,
.status-badge--denied,
.status-badge--failed {
    background: var(--red-100);
    color: var(--red-600);
}

.status-badge--draft,
.status-badge--scheduled {
    background: var(--gray-100);
    color: var(--gray-700);
}

/* ---- Forms ---- */
.field {
    margin-bottom: 16px;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

label {
    display: block;
    font-size: .875rem;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 5px;
}

.field-link {
    float: right;
    font-weight: 400;
}

.label-optional {
    font-weight: 400;
    color: var(--gray-500);
}

.label-hint {
    font-weight: 400;
    font-size: .8125rem;
    color: var(--gray-500);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="tel"],
input[type="number"],
input[type="date"],
input[type="time"],
select,
textarea {
    display: block;
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-sm);
    background: #fff;
    color: var(--gray-900);
    transition: border-color .15s, box-shadow .15s;
    appearance: none;
    -webkit-appearance: none;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--brand-600);
    box-shadow: 0 0 0 3px rgb(26 86 219 / .15);
}

select {
    background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236B7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

textarea {
    resize: vertical;
    min-height: 80px;
}

.password-wrap {
    position: relative;
}

.password-wrap input {
    padding-right: 44px;
}

.pw-toggle {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    color: var(--gray-500);
    padding: 4px;
}

.pw-toggle:hover {
    color: var(--gray-900);
}

.code-input {
    text-align: center;
    font-size: 1.5rem;
    letter-spacing: .2em;
    font-family: var(--font-mono);
}

.checkbox-label {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    cursor: pointer;
    font-size: .9375rem;
    margin-bottom: 10px;
}

.checkbox-label input {
    width: 18px;
    height: 18px;
    margin-top: 2px;
    accent-color: var(--brand-600);
    flex-shrink: 0;
}

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    font-size: .9375rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    border: 2px solid transparent;
    transition: background .15s, border-color .15s, box-shadow .15s;
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
}

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

.btn-primary:hover {
    background: var(--brand-700);
    text-decoration: none;
}

.btn-outline {
    background: transparent;
    color: var(--brand-600);
    border-color: var(--brand-600);
}

.btn-outline:hover {
    background: var(--brand-50);
    text-decoration: none;
}

.btn-danger {
    background: var(--red-600);
    color: #fff;
}

.btn-full {
    width: 100%;
}

.btn-sm {
    padding: 6px 12px;
    font-size: .8125rem;
}

.btn-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

/* ---- Alerts ---- */
.alert {
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    font-size: .9375rem;
    margin-bottom: 16px;
}

.alert-error {
    background: var(--red-100);
    color: var(--red-700);
}

.alert-success {
    background: var(--green-100);
    color: var(--green-700);
}

.alert-warn {
    background: var(--amber-100);
    color: var(--amber-700);
}

.alert-info {
    background: var(--blue-100);
    color: var(--blue-700);
}

/* ---- Auth pages ---- */
.auth-page {
    display: flex;
    min-height: 100dvh;
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
    background: var(--gray-50);
}

.auth-card {
    width: 100%;
    max-width: 400px;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: 32px 24px;
    box-shadow: var(--shadow-md);
}

.auth-card--wide {
    max-width: 500px;
}

.auth-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 24px;
    font-size: 1.25rem;
    font-weight: 700;
}

.auth-title {
    font-size: 1.375rem;
    font-weight: 700;
    margin-bottom: 20px;
}

.auth-subtitle {
    font-size: .9375rem;
    color: var(--gray-500);
    margin-top: -14px;
    margin-bottom: 20px;
}

.auth-switch {
    text-align: center;
    font-size: .875rem;
    margin-top: 20px;
    color: var(--gray-500);
}

/* ---- TOTP setup ---- */
.setup-steps {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.step {
    display: flex;
    gap: 16px;
}

.step-number {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--brand-600);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.step-content {
    flex: 1;
}

.qr-wrap {
    margin: 12px 0;
    max-width: 200px;
}

.qr-wrap svg {
    width: 100% !important;
    height: auto !important;
}

.manual-code {
    margin-top: 8px;
    font-size: .875rem;
}

.secret-code {
    display: block;
    margin-top: 6px;
    font-family: var(--font-mono);
    background: var(--gray-100);
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    word-break: break-all;
    letter-spacing: .15em;
}

/* ---- Form steps ---- */
.form-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.tab {
    padding: 8px 14px;
    border-radius: 20px;
    border: none;
    background: var(--gray-100);
    color: var(--gray-700);
    cursor: pointer;
    font-size: .875rem;
    font-weight: 500;
    white-space: nowrap;
}

.tab.active {
    background: var(--brand-600);
    color: #fff;
}

.step-nav {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.form-section-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 14px;
    margin-top: 4px;
}

/* Consent cards */
.consent-card {
    margin-bottom: 16px;
}

.consent-card h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 8px;
}

.consent-card p {
    font-size: .875rem;
    color: var(--gray-700);
    margin-bottom: 12px;
}

/* ---- File upload ---- */
.file-drop {
    border: 2px dashed var(--gray-300);
    border-radius: var(--radius);
    padding: 24px;
    text-align: center;
    position: relative;
    transition: border-color .15s;
}

.file-drop.drop-active {
    border-color: var(--brand-600);
    background: var(--brand-50);
}

.file-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
}

.file-drop-ui svg {
    margin: 0 auto 8px;
    stroke: var(--gray-400);
}

.file-drop-ui p {
    color: var(--gray-500);
    font-size: .875rem;
}

.file-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}

.preview-thumb {
    width: 72px;
    height: 72px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    border: 1px solid var(--gray-200);
}

/* ---- Password strength ---- */
.password-strength {
    height: 4px;
    border-radius: 2px;
    margin-top: 6px;
    background: var(--gray-200);
    transition: all .2s;
}

.password-strength[data-strength="weak"] {
    background: var(--red-600);
    width: 33%;
}

.password-strength[data-strength="medium"] {
    background: var(--amber-700);
    width: 66%;
}

.password-strength[data-strength="strong"] {
    background: var(--green-700);
    width: 100%;
}

/* ---- Document pages ---- */
.doc-page {
    max-width: 680px;
    margin: 0 auto;
    background: #fff;
    padding: 32px;
    font-family: Georgia, serif;
    font-size: .9375rem;
    line-height: 1.7;
}

.doc-page h1 {
    font-size: 1.25rem;
    text-align: center;
    margin-bottom: 24px;
}

.doc-page h2 {
    font-size: 1rem;
    margin: 20px 0 8px;
}

.doc-page p {
    margin-bottom: 10px;
}

.doc-signature {
    margin-top: 40px;
    border-top: 1px solid #000;
    padding-top: 6px;
    display: inline-block;
    min-width: 200px;
}

.doc-date {
    font-size: .875rem;
    color: #666;
}

/* ---- Responsive ---- */
@media (min-width: 640px) {
    .app-main {
        padding: 24px;
    }

    .kpi-row {
        grid-template-columns: repeat(4, 1fr);
    }

    .field-row {
        grid-template-columns: 1fr 1fr;
    }
}

@media (min-width: 768px) {
    body {
        padding-bottom: 0;
    }

    .bottom-nav {
        display: none;
    }

    .app-header {
        left: 0;
    }
}

/* ---- Print (for documents) ---- */
@media print {

    .app-header,
    .bottom-nav,
    .drawer,
    nav {
        display: none !important;
    }

    body {
        padding: 0;
        background: #fff;
    }

    .card {
        box-shadow: none;
        border: none;
    }
}

/* ---- Dark mode (respects system pref) ---- */
@media (prefers-color-scheme: dark) {
    :root {
        --gray-50: #111827;
        --gray-100: #1f2937;
        --gray-200: #374151;
        --gray-300: #4b5563;
        --gray-500: #9ca3af;
        --gray-700: #d1d5db;
        --gray-900: #f9fafb;
    }

    body {
        background: var(--gray-50);
    }

    .app-header,
    .bottom-nav,
    .drawer-panel,
    .card,
    .list-card,
    .auth-card,
    .property-card,
    .kpi-card {
        background: #1f2937;
        border-color: var(--gray-200);
    }

    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="time"],
    select,
    textarea {
        background: #374151;
        border-color: var(--gray-300);
        color: var(--gray-900);
    }

    .tab {
        background: var(--gray-200);
        color: var(--gray-700);
    }

    .file-drop {
        border-color: var(--gray-300);
    }

    .lease-card {
        background: var(--brand-700);
    }
}

/* ---- Reduced motion ---- */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        transition-duration: .01ms !important;
    }
}