@layer app-tokens, app-base, app-components, app-modules, app-responsive;

@layer app-tokens {
    .app-shell {
        --app-bg: #f7f2e9;
        --app-surface: #fffdf9;
        --app-surface-strong: #ffffff;
        --app-ink: #1d1712;
        --app-muted: #776d64;
        --app-line: #e9dcc9;
        --app-gold: #b9823e;
        --app-gold-dark: #8d5e27;
        --app-navy: #28365f;
        --app-success: #26734d;
        --app-warning: #a86214;
        --app-danger: #b93832;
        --app-shadow: 0 18px 50px rgba(78, 52, 27, 0.1);
        --app-radius-lg: 24px;
        --app-radius-md: 16px;
        --app-radius-sm: 12px;
        --app-sidebar-width: 278px;
        color: var(--app-ink);
        font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    }
}

@layer app-base {
    .app-page {
        min-width: 320px;
        min-height: 100%;
        margin: 0;
        background: var(--app-bg, #f7f2e9);
    }

    :where(.app-shell, .app-shell *) {
        box-sizing: border-box;
    }

    .app-shell {
        min-height: 100dvh;
        background:
            radial-gradient(circle at 95% 4%, rgba(210, 174, 123, 0.28), transparent 30rem),
            linear-gradient(135deg, #fbf8f2, var(--app-bg));
    }

    .app-shell img {
        display: block;
        max-width: 100%;
        object-fit: contain;
    }

    .app-shell button,
    .app-shell textarea,
    .app-shell input {
        font: inherit;
    }

    .app-shell button {
        min-height: 44px;
    }

    .app-shell :focus-visible {
        outline: 3px solid rgba(40, 54, 95, 0.48);
        outline-offset: 3px;
    }

    .app-shell [hidden] {
        display: none !important;
    }

    .app-shell__boot {
        position: fixed;
        inset: 0;
        z-index: 100;
        display: grid;
        place-content: center;
        justify-items: center;
        gap: 16px;
        padding: 24px;
        background: #fbf8f2;
        color: var(--app-muted);
        text-align: center;
    }

    .app-shell__boot img {
        width: 150px;
        height: 60px;
    }

    .app-shell[data-state="ready"] .app-shell__boot {
        display: none;
    }

    .app-shell[data-state="booting"] .app-shell__layout {
        visibility: hidden;
    }

    .app-shell__layout {
        min-height: 100dvh;
        display: grid;
        grid-template-columns: var(--app-sidebar-width) minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas:
            "sidebar topbar"
            "sidebar main";
    }
}

@layer app-components {
    .app-shell__sidebar {
        grid-area: sidebar;
        position: sticky;
        top: 0;
        z-index: 30;
        min-height: 100dvh;
        max-height: 100dvh;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
        gap: 24px;
        padding: 24px 18px;
        border-right: 1px solid rgba(185, 130, 62, 0.18);
        background: rgba(255, 253, 249, 0.9);
        backdrop-filter: blur(18px);
    }

    .app-shell__brand {
        display: flex;
        align-items: center;
        gap: 12px;
        min-height: 52px;
        color: var(--app-ink);
        text-decoration: none;
        font-size: 12px;
        font-weight: 800;
        letter-spacing: 0.08em;
        text-transform: uppercase;
    }

    .app-shell__brand img {
        width: 112px;
        height: 45px;
    }

    .app-shell__nav {
        display: grid;
        gap: 10px;
    }

    .app-shell__nav-item {
        width: 100%;
        display: grid;
        grid-template-columns: 42px minmax(0, 1fr);
        align-items: center;
        gap: 12px;
        padding: 11px 12px;
        border: 1px solid transparent;
        border-radius: var(--app-radius-md);
        background: transparent;
        color: var(--app-ink);
        text-align: left;
        cursor: pointer;
    }

    .app-shell__nav-item:hover {
        background: rgba(185, 130, 62, 0.09);
    }

    .app-shell__nav-item.is-active {
        border-color: rgba(185, 130, 62, 0.32);
        background: linear-gradient(135deg, #fff8eb, #f5e5cb);
        box-shadow: 0 10px 24px rgba(141, 94, 39, 0.11);
    }

    .app-shell__nav-icon {
        width: 42px;
        height: 42px;
        display: grid;
        place-items: center;
        border-radius: 14px;
        background: var(--app-navy);
        color: #fff;
        font-size: 22px;
        line-height: 1;
    }

    .app-shell__nav-copy {
        min-width: 0;
        display: grid;
        gap: 3px;
    }

    .app-shell__nav-copy strong,
    .app-shell__nav-copy small {
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .app-shell__nav-copy strong {
        font-size: 14px;
    }

    .app-shell__nav-copy small {
        color: var(--app-muted);
        font-size: 11px;
        line-height: 1.35;
        white-space: normal;
    }

    .app-shell__sidebar-note {
        margin-top: auto;
        display: grid;
        gap: 5px;
        padding: 14px;
        border: 1px dashed rgba(185, 130, 62, 0.4);
        border-radius: var(--app-radius-md);
        color: var(--app-muted);
        font-size: 12px;
        line-height: 1.45;
    }

    .app-shell__sidebar-note strong {
        color: var(--app-gold-dark);
    }

    .app-shell__sidebar-overlay {
        position: fixed;
        inset: 0;
        z-index: 25;
        border: 0;
        background: rgba(22, 18, 15, 0.42);
        cursor: pointer;
    }

    .app-shell__topbar {
        grid-area: topbar;
        position: sticky;
        top: 0;
        z-index: 20;
        min-width: 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 18px;
        min-height: 84px;
        padding: 14px clamp(18px, 3vw, 42px);
        border-bottom: 1px solid rgba(185, 130, 62, 0.15);
        background: rgba(251, 248, 242, 0.88);
        backdrop-filter: blur(18px);
    }

    .app-shell__topbar-leading,
    .app-shell__topbar-actions,
    .app-shell__user {
        min-width: 0;
        display: flex;
        align-items: center;
    }

    .app-shell__topbar-leading,
    .app-shell__topbar-actions {
        gap: 14px;
    }

    .app-shell__topbar-actions {
        flex: 0 0 auto;
    }

    .app-shell__topbar-title {
        min-width: 0;
        display: grid;
        gap: 2px;
    }

    .app-shell__topbar-title span {
        color: var(--app-gold-dark);
        font-size: 10px;
        font-weight: 800;
        letter-spacing: 0.16em;
    }

    .app-shell__topbar-title strong {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 18px;
    }

    .app-shell__menu-button {
        display: none;
    }

    .app-shell__icon-button {
        position: relative;
        width: 44px;
        height: 44px;
        display: inline-grid;
        place-items: center;
        padding: 0;
        border: 1px solid var(--app-line);
        border-radius: 14px;
        background: var(--app-surface-strong);
        color: var(--app-ink);
        cursor: pointer;
    }

    .app-shell__user {
        gap: 10px;
    }

    .app-shell__avatar {
        width: 42px;
        height: 42px;
        display: grid;
        place-items: center;
        flex: 0 0 auto;
        border-radius: 50%;
        background: var(--app-navy);
        color: #fff;
        font-size: 13px;
        font-weight: 800;
    }

    .app-shell__user-copy {
        min-width: 0;
        display: grid;
        gap: 2px;
        max-width: 190px;
    }

    .app-shell__user-copy strong,
    .app-shell__user-copy small {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-shell__user-copy strong {
        font-size: 13px;
    }

    .app-shell__user-copy small {
        color: var(--app-muted);
        font-size: 11px;
    }

    .app-shell__button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 10px 16px;
        border: 1px solid transparent;
        border-radius: 13px;
        font-weight: 750;
        cursor: pointer;
    }

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

    .app-shell__button--secondary {
        border-color: var(--app-line);
        background: #fff;
        color: var(--app-ink);
    }

    .app-shell__button--dark {
        background: var(--app-ink);
        color: #fff;
    }

    .app-shell__button--danger {
        background: var(--app-danger);
        color: #fff;
    }

    .app-shell__main {
        grid-area: main;
        min-width: 0;
        width: 100%;
        padding: clamp(24px, 4vw, 52px);
        outline: none;
    }

    .app-shell__view-heading {
        margin-bottom: 26px;
    }

    .app-shell__eyebrow {
        color: var(--app-gold-dark);
        font-size: 11px;
        font-weight: 850;
        letter-spacing: 0.12em;
    }

    .app-shell__view-heading h1 {
        margin: 8px 0 0;
        font-size: clamp(30px, 4vw, 52px);
        letter-spacing: -0.04em;
    }

    .app-shell__outlet,
    .app-shell__module {
        min-width: 0;
    }

    .app-shell__loading {
        display: grid;
        place-items: center;
        min-height: 220px;
        color: var(--app-muted);
    }

    .app-shell__module-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 20px;
    }

    .app-shell__card {
        min-width: 0;
        padding: clamp(20px, 3vw, 30px);
        border: 1px solid var(--app-line);
        border-radius: var(--app-radius-lg);
        background: rgba(255, 253, 249, 0.94);
        box-shadow: var(--app-shadow);
    }

    .app-shell__card h2,
    .app-shell__card h3 {
        margin: 8px 0 10px;
    }

    .app-shell__card p {
        color: var(--app-muted);
        line-height: 1.65;
    }

    .app-shell__card-heading,
    .app-shell__actions,
    .app-shell__status-row {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 10px;
    }

    .app-shell__card-heading {
        justify-content: space-between;
    }

    .app-shell__status {
        display: inline-flex;
        align-items: center;
        min-height: 30px;
        padding: 5px 10px;
        border-radius: 999px;
        background: #efe9df;
        color: var(--app-muted);
        font-size: 12px;
        font-weight: 750;
    }

    .app-shell__status--success {
        background: #e5f4eb;
        color: var(--app-success);
    }

    .app-shell__status--warning {
        background: #fff1d8;
        color: var(--app-warning);
    }

    .app-shell__code {
        max-width: 100%;
        overflow: auto;
        margin: 12px 0 0;
        padding: 14px;
        border-radius: var(--app-radius-sm);
        background: #28241f;
        color: #fff8e8;
        font: 12px/1.55 ui-monospace, SFMono-Regular, Consolas, monospace;
        white-space: pre-wrap;
        overflow-wrap: anywhere;
    }

    .app-shell__field {
        display: grid;
        gap: 8px;
        margin: 20px 0;
        font-size: 13px;
        font-weight: 750;
    }

    .app-shell__field textarea {
        width: 100%;
        resize: vertical;
        padding: 14px;
        border: 1px solid var(--app-line);
        border-radius: var(--app-radius-sm);
        background: #fff;
        color: var(--app-ink);
        line-height: 1.5;
    }

    .app-shell__muted {
        color: var(--app-muted);
    }

    .mod-foundation__hero,
    .mod-lifecycle__editor {
        grid-column: 1 / -1;
    }

    .mod-lifecycle__metrics {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 12px;
        margin: 20px 0;
    }

    .mod-lifecycle__metrics div {
        padding: 16px;
        border-radius: var(--app-radius-md);
        background: #f4ede2;
    }

    .mod-lifecycle__metrics dt {
        color: var(--app-muted);
        font-size: 12px;
    }

    .mod-lifecycle__metrics dd {
        margin: 6px 0 0;
        font-size: 30px;
        font-weight: 850;
    }

    .app-shell__notification {
        position: relative;
    }

    .app-shell__notification-bell > span:first-child {
        color: var(--app-gold);
        font-size: 18px;
    }

    .app-shell__notification-count {
        position: absolute;
        top: -4px;
        right: -5px;
        min-width: 20px;
        height: 20px;
        display: grid;
        place-items: center;
        padding: 0 5px;
        border-radius: 999px;
        background: var(--app-danger);
        color: #fff;
        font-size: 10px;
        font-weight: 850;
    }

    .app-shell__notification-panel {
        position: absolute;
        top: 52px;
        right: 0;
        width: min(380px, calc(100vw - 32px));
        max-height: min(520px, 75vh);
        overflow-y: auto;
        border: 1px solid var(--app-line);
        border-radius: var(--app-radius-md);
        background: #fff;
        box-shadow: 0 24px 60px rgba(42, 29, 18, 0.2);
    }

    .app-shell__notification-header {
        position: sticky;
        top: 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 14px 16px;
        border-bottom: 1px solid var(--app-line);
        background: #fff;
    }

    .app-shell__notification-header h2 {
        margin: 0;
        font-size: 16px;
    }

    .app-shell__notification-header button {
        border: 0;
        background: transparent;
        color: var(--app-gold-dark);
        font-size: 12px;
        font-weight: 750;
        cursor: pointer;
    }

    .app-shell__notification-list {
        display: grid;
    }

    .app-shell__notification-item {
        display: grid;
        gap: 4px;
        min-height: 0;
        padding: 14px 16px;
        border: 0;
        border-bottom: 1px solid #f0e8dd;
        background: #fff;
        color: var(--app-ink);
        text-align: left;
        cursor: pointer;
    }

    .app-shell__notification-item.is-unread {
        background: #fff8eb;
    }

    .app-shell__notification-item span,
    .app-shell__notification-item small,
    .app-shell__notification-empty {
        color: var(--app-muted);
    }

    .app-shell__notification-item span {
        line-height: 1.45;
    }

    .app-shell__notification-empty {
        padding: 28px 18px;
        text-align: center;
    }

    .app-shell__dialog {
        width: min(480px, calc(100vw - 32px));
        padding: 0;
        border: 0;
        border-radius: var(--app-radius-lg);
        background: transparent;
        color: var(--app-ink);
    }

    .app-shell__dialog::backdrop {
        background: rgba(22, 18, 15, 0.52);
        backdrop-filter: blur(4px);
    }

    .app-shell__dialog-panel {
        padding: 26px;
        border: 1px solid var(--app-line);
        border-radius: var(--app-radius-lg);
        background: #fff;
        box-shadow: 0 24px 80px rgba(42, 29, 18, 0.24);
    }

    .app-shell__dialog-panel h2 {
        margin: 0 0 10px;
    }

    .app-shell__dialog-panel p {
        margin: 0;
        color: var(--app-muted);
        line-height: 1.55;
    }

    .app-shell__dialog-actions {
        display: flex;
        justify-content: flex-end;
        flex-wrap: wrap;
        gap: 10px;
        margin-top: 24px;
    }

    .app-shell__toasts {
        position: fixed;
        right: 20px;
        bottom: 20px;
        z-index: 80;
        width: min(360px, calc(100vw - 40px));
        display: grid;
        gap: 10px;
        pointer-events: none;
    }

    .app-shell__toast {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 14px 16px;
        border-left: 4px solid var(--app-navy);
        border-radius: var(--app-radius-sm);
        background: #fff;
        box-shadow: 0 16px 40px rgba(42, 29, 18, 0.18);
        pointer-events: auto;
    }

    .app-shell__toast--success { border-left-color: var(--app-success); }
    .app-shell__toast--warning { border-left-color: var(--app-warning); }
    .app-shell__toast--danger { border-left-color: var(--app-danger); }

    .app-shell__toast-close {
        width: 44px;
        padding: 0;
        border: 0;
        background: transparent;
        color: var(--app-muted);
        font-size: 22px;
        cursor: pointer;
    }

    .app-shell__live-region {
        position: fixed;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
        white-space: nowrap;
    }

    .app-shell__error-card {
        padding: 28px;
        border: 1px solid #edc4c1;
        border-radius: var(--app-radius-lg);
        background: #fff5f4;
    }

    .app-shell__error-card h2 {
        margin-top: 0;
    }
}

@layer app-responsive {
    @media (max-width: 1023px) {
        .app-shell__layout {
            grid-template-columns: minmax(0, 1fr);
            grid-template-areas:
                "topbar"
                "main";
        }

        .app-shell__sidebar {
            position: fixed;
            inset: 0 auto 0 0;
            width: min(var(--app-sidebar-width), calc(100vw - 48px));
            transform: translateX(-105%);
            transition: transform 180ms ease;
            box-shadow: 16px 0 50px rgba(42, 29, 18, 0.2);
        }

        .app-shell[data-sidebar-open="true"] .app-shell__sidebar {
            transform: translateX(0);
        }

        .app-shell__menu-button {
            display: inline-grid;
        }

        .app-shell__topbar {
            min-height: 74px;
        }

        .app-shell__main {
            padding-top: 30px;
        }
    }

    @media (max-width: 767px) {
        .app-shell__topbar-actions {
            gap: 8px;
        }

        .app-shell__user-copy,
        .app-shell__button--dark,
        .app-shell__topbar-title span {
            display: none;
        }

        .app-shell__topbar {
            padding-inline: 12px;
        }

        .app-shell__main {
            padding: 24px 16px 40px;
        }

        .app-shell__module-grid,
        .mod-lifecycle__metrics {
            grid-template-columns: minmax(0, 1fr);
        }

        .app-shell__actions .app-shell__button,
        .app-shell__dialog-actions .app-shell__button {
            width: 100%;
        }

        .app-shell__notification-panel {
            position: fixed;
            top: 70px;
            right: 12px;
            left: 12px;
            width: auto;
        }
    }

    @media (prefers-reduced-motion: reduce) {
        .app-shell__sidebar {
            transition: none;
        }
    }
}
