:root {
            color-scheme: dark;
            --bg-primary: #080c14;
            --bg-secondary: #0d1117;
            --bg-card: #111827;
            --bg-card-hover: #1a2332;
            --accent: #00e5b4;
            --accent-hover: #00c49a;
            --accent-glow: rgba(0, 229, 180, 0.15);
            --danger: #ff4757;
            --warning: #ffa502;
            --info: #3b82f6;
            --success: #22c55e;
            --text-primary: #e2e8f0;
            --text-secondary: #94a3b8;
            --text-muted: #64748b;
            --border: rgba(255, 255, 255, 0.06);
            --border-hover: rgba(0, 229, 180, 0.3);
            --radius-sm: 8px;
            --radius-md: 12px;
            --radius-lg: 16px;
            --radius-xl: 24px;
            --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.3);
            --shadow-glow: 0 0 30px rgba(0, 229, 180, 0.1);
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            --sidebar-width: 270px;
            --header-height: 92px;
        }

        [data-theme="light"] {
            color-scheme: light;
            --bg-primary: #f8fafc;
            --bg-secondary: #ffffff;
            --bg-card: #ffffff;
            --bg-card-hover: #f1f5f9;
            --text-primary: #1e293b;
            --text-secondary: #475569;
            --text-muted: #94a3b8;
            --border: rgba(0, 0, 0, 0.08);
            --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.06);
        }

        *,
        *::before,
        *::after {
            box-sizing: border-box;
        }

        html {
            scroll-behavior: smooth;
        }

        body {
            margin: 0;
            min-height: 100vh;
            font-family: "Tajawal", sans-serif;
            color: var(--text-primary);
            background:
                radial-gradient(circle at top right, rgba(0, 229, 180, 0.12), transparent 24%),
                radial-gradient(circle at bottom left, rgba(59, 130, 246, 0.1), transparent 22%),
                linear-gradient(180deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
            overflow-x: hidden;
        }

        body::before {
            content: "";
            position: fixed;
            inset: 0;
            pointer-events: none;
            background-image:
                linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
                linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
            background-size: 120px 120px;
            mask-image: radial-gradient(circle at center, black 45%, transparent 92%);
            opacity: 0.5;
        }

        body.menu-open,
        body.modal-open {
            overflow: hidden;
        }

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

        * {
            scrollbar-width: thin;
            scrollbar-color: var(--accent) transparent;
        }

        ::selection {
            color: #031b16;
            background: var(--accent);
        }

        :focus-visible {
            outline: 3px solid var(--accent);
            outline-offset: 3px;
        }

        .glass {
            background: rgba(17, 24, 39, 0.6);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            box-shadow: var(--shadow-card);
        }

        [data-theme="light"] .glass {
            background: rgba(255, 255, 255, 0.78);
        }

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

        .skip-link {
            position: fixed;
            top: 16px;
            left: 16px;
            z-index: 1000;
            padding: 12px 16px;
            color: #031b16;
            background: var(--accent);
            border-radius: var(--radius-sm);
            text-decoration: none;
            transform: translateY(-150%);
        }

        .skip-link:focus-visible {
            transform: translateY(0);
        }

        .sidebar-backdrop,
        .modal-overlay {
            position: fixed;
            inset: 0;
            z-index: 140;
            background: rgba(3, 7, 18, 0.58);
            opacity: 0;
            visibility: hidden;
            transition: var(--transition);
        }

        .sidebar-backdrop.is-visible,
        .modal-overlay.is-visible {
            opacity: 1;
            visibility: visible;
        }

        .sidebar {
            position: fixed;
            top: 0;
            right: 0;
            z-index: 180;
            width: var(--sidebar-width);
            height: 100vh;
            padding: 24px 18px;
            display: flex;
            flex-direction: column;
            gap: 24px;
            background:
                linear-gradient(180deg, rgba(13, 17, 23, 0.96), rgba(8, 12, 20, 0.94)),
                rgba(17, 24, 39, 0.6);
            border-left: 1px solid var(--border);
            box-shadow: -24px 0 80px rgba(0, 0, 0, 0.28);
        }

        .sidebar-header,
        .brand,
        .brand-title-row,
        .header-side,
        .header-actions,
        .nav-main,
        .filters-row,
        .filters-inline,
        .template-meta,
        .template-actions,
        .preview-layout,
        .modal-actions,
        .toolbar,
        .editor-actions,
        .toast,
        .template-preview-header {
            display: flex;
            align-items: center;
        }

        .sidebar-header {
            justify-content: space-between;
            gap: 12px;
        }

        .brand {
            gap: 14px;
        }

        .brand-logo {
            width: 52px;
            height: 52px;
            padding: 12px;
            border-radius: 18px;
            color: var(--accent);
            background: linear-gradient(135deg, var(--accent-glow), rgba(59, 130, 246, 0.08));
            border: 1px solid rgba(0, 229, 180, 0.18);
            box-shadow: var(--shadow-glow);
        }

        .brand-title-row {
            gap: 10px;
        }

        .brand-name {
            margin: 0;
            color: var(--accent);
            font-size: 1.35rem;
            font-weight: 700;
        }

        .brand-subtitle {
            margin: 6px 0 0;
            color: var(--text-secondary);
            font-size: 0.92rem;
        }

        .badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 4px 10px;
            border-radius: 999px;
            font-size: 0.72rem;
            font-weight: 700;
            color: var(--text-primary);
            background: rgba(59, 130, 246, 0.16);
            border: 1px solid rgba(59, 130, 246, 0.24);
        }

        .sidebar-nav {
            flex: 1;
            overflow-y: auto;
        }

        .nav-list {
            display: grid;
            gap: 10px;
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .nav-link {
            position: relative;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            padding: 14px 16px;
            color: var(--text-secondary);
            text-decoration: none;
            border-radius: var(--radius-md);
            border: 1px solid transparent;
            overflow: hidden;
        }

        .nav-link::after {
            content: "";
            position: absolute;
            top: 10px;
            bottom: 10px;
            right: 0;
            width: 3px;
            border-radius: 999px;
            background: transparent;
        }

        .nav-link:hover {
            color: var(--text-primary);
            border-color: var(--border-hover);
            background: rgba(255, 255, 255, 0.03);
        }

        .nav-link[aria-current="page"] {
            color: var(--accent);
            background: rgba(0, 229, 180, 0.1);
            border-color: rgba(0, 229, 180, 0.2);
        }

        .nav-link[aria-current="page"]::after {
            background: var(--accent);
        }

        .nav-icon {
            width: 20px;
            height: 20px;
        }

        .nav-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 26px;
            height: 26px;
            padding-inline: 8px;
            border-radius: 999px;
            color: #fff;
            background: rgba(255, 71, 87, 0.2);
            border: 1px solid rgba(255, 71, 87, 0.3);
        }

        .user-card {
            padding: 16px;
            border-radius: var(--radius-lg);
        }

        .user-summary {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 14px;
        }

        .avatar {
            width: 48px;
            height: 48px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-weight: 700;
            color: #031b16;
            background: linear-gradient(135deg, #7ef6dd, var(--accent));
        }

        .user-name {
            margin: 0;
            font-weight: 700;
        }

        .user-plan {
            margin: 4px 0 0;
            color: var(--text-secondary);
        }

        .logout-button,
        .primary-button,
        .icon-button,
        .secondary-button,
        .template-button,
        .sort-select,
        .search-input,
        .filter-search {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 12px 16px;
            border-radius: 14px;
            border: 1px solid var(--border);
            transition: var(--transition);
        }

        .logout-button,
        .secondary-button,
        .icon-button,
        .template-button,
        .sort-select,
        .search-input,
        .filter-search {
            color: var(--text-primary);
            background: rgba(17, 24, 39, 0.58);
        }

        .primary-button {
            color: #031b16;
            background: linear-gradient(135deg, var(--accent), #67f5d8);
            border-color: transparent;
            font-weight: 700;
            box-shadow: 0 14px 34px rgba(0, 229, 180, 0.2);
            text-decoration: none;
        }

        .primary-button:hover,
        .secondary-button:hover,
        .template-button:hover,
        .icon-button:hover {
            transform: translateY(-2px);
        }

        .main-content {
            min-height: 100vh;
            margin-right: var(--sidebar-width);
            display: flex;
            flex-direction: column;
        }

        .top-header {
            position: sticky;
            top: 0;
            z-index: 120;
            display: grid;
            grid-template-columns: minmax(180px, 1fr) minmax(280px, 1.4fr) auto;
            align-items: center;
            gap: 20px;
            min-height: var(--header-height);
            padding: 20px 28px;
            border-bottom: 1px solid var(--border);
            background: rgba(8, 12, 20, 0.68);
            backdrop-filter: blur(16px);
        }

        .menu-toggle,
        .close-sidebar {
            display: none;
        }

        .breadcrumb {
            display: inline-flex;
            align-items: center;
            gap: 10px;
            color: var(--text-secondary);
        }

        .breadcrumb a {
            color: inherit;
            text-decoration: none;
        }

        .breadcrumb .current {
            color: var(--text-primary);
            font-weight: 700;
        }

        .search-shell {
            position: relative;
        }

        .search-shell svg {
            position: absolute;
            top: 50%;
            right: 16px;
            width: 18px;
            height: 18px;
            transform: translateY(-50%);
            color: var(--text-muted);
        }

        .search-input {
            width: 100%;
            justify-content: flex-start;
            padding: 14px 122px 14px 48px;
        }

        .search-shortcut {
            position: absolute;
            top: 50%;
            left: 14px;
            transform: translateY(-50%);
            padding: 6px 10px;
            color: var(--text-secondary);
            background: rgba(148, 163, 184, 0.08);
            border: 1px solid var(--border);
            border-radius: 999px;
            font-family: "JetBrains Mono", monospace;
            font-size: 0.72rem;
        }

        .icon-button {
            min-width: 48px;
            height: 48px;
            cursor: pointer;
        }

        .notification-badge {
            position: absolute;
            top: 6px;
            left: 6px;
            min-width: 18px;
            height: 18px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 999px;
            color: #fff;
            background: var(--danger);
            font-size: 0.68rem;
        }

        .api-status {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 6px 12px;
            color: var(--text-secondary);
            background: var(--bg-card);
            border: 1px solid var(--border);
            border-radius: var(--radius-md);
            font-size: 0.8rem;
            cursor: pointer;
            transition: var(--transition);
        }

        .api-status .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: var(--text-muted);
            animation: pulse 2s infinite;
        }

        .api-status[data-status="connected"] .status-dot {
            background: var(--accent);
            animation: none;
        }

        .api-status[data-status="error"] .status-dot {
            background: var(--danger);
            animation: none;
        }

        .api-status:hover {
            background: var(--bg-card-hover);
            border-color: var(--border-hover);
        }

        @keyframes pulse {

            0%,
            100% {
                opacity: 1;
            }

            50% {
                opacity: 0.5;
            }
        }

        .page-content {
            flex: 1;
            padding: 28px;
        }

        .templates-layout {
            display: grid;
            gap: 24px;
        }

        .section-fade {
            animation: fade-view 0.2s ease;
        }

        .page-hero,
        .filters-shell,
        .editor-shell {
            padding: 24px;
            border-radius: var(--radius-xl);
        }

        .page-hero {
            position: relative;
            overflow: hidden;
        }

        .page-hero::before {
            content: "";
            position: absolute;
            inset: 0;
            background:
                linear-gradient(135deg, rgba(0, 229, 180, 0.06), transparent 60%),
                radial-gradient(circle at top left, rgba(59, 130, 246, 0.12), transparent 30%);
            pointer-events: none;
        }

        .page-hero>* {
            position: relative;
            z-index: 1;
        }

        .page-title {
            margin: 0;
            font-size: clamp(2rem, 3vw, 2.4rem);
        }

        .page-subtitle {
            margin: 12px 0 0;
            max-width: 720px;
            color: var(--text-secondary);
            line-height: 1.9;
        }

        .filters-shell {
            display: grid;
            gap: 18px;
        }

        .filters-row {
            justify-content: space-between;
            gap: 18px;
            flex-wrap: wrap;
        }

        .filter-search {
            flex: 1 1 320px;
            justify-content: flex-start;
            width: 100%;
            max-width: 420px;
        }

        .filter-search input,
        .search-input input,
        .sort-select,
        .sort-select select {
            width: 100%;
            color: inherit;
            background: transparent;
            border: 0;
            outline: 0;
        }

        .filters-inline {
            gap: 10px;
            flex-wrap: wrap;
        }

        .filter-tab {
            padding: 10px 16px;
            color: var(--text-secondary);
            background: rgba(148, 163, 184, 0.06);
            border: 1px solid var(--border);
            border-radius: 999px;
            cursor: pointer;
        }

        .filter-tab.is-active {
            color: var(--accent);
            border-color: rgba(0, 229, 180, 0.24);
            background: rgba(0, 229, 180, 0.08);
        }

        .sort-select {
            min-width: 180px;
        }

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

        .template-card {
            position: relative;
            display: grid;
            gap: 16px;
            padding: 22px;
            border-radius: var(--radius-xl);
            transition: var(--transition);
        }

        .template-card:hover {
            transform: translateY(-6px);
            box-shadow: 0 24px 52px rgba(0, 0, 0, 0.24);
        }

        .template-top {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            gap: 12px;
        }

        .template-icon {
            width: 58px;
            height: 58px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 18px;
            background: rgba(255, 255, 255, 0.04);
            border: 1px solid currentColor;
        }

        .template-icon svg {
            width: 28px;
            height: 28px;
        }

        .template-title {
            margin: 0;
            font-size: 1.15rem;
        }

        .template-description {
            margin: 0;
            color: var(--text-secondary);
            line-height: 1.8;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .category-badge,
        .card-highlight,
        .rating-badge,
        .token-chip,
        .template-preview-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 6px 10px;
            border-radius: 999px;
            font-size: 0.78rem;
            font-weight: 700;
        }

        .card-highlight {
            color: #031b16;
            background: linear-gradient(135deg, var(--accent), #72f4da);
        }

        .template-meta {
            justify-content: space-between;
            gap: 10px;
            flex-wrap: wrap;
            color: var(--text-secondary);
            font-size: 0.9rem;
        }

        .template-actions {
            justify-content: space-between;
            gap: 12px;
        }

        .template-button {
            width: 100%;
            text-decoration: none;
            cursor: pointer;
        }

        .template-button.is-primary {
            color: #031b16;
            background: linear-gradient(135deg, var(--accent), #67f5d8);
            border-color: transparent;
            font-weight: 700;
        }

        .modal {
            position: fixed;
            top: 50%;
            left: 50%;
            z-index: 220;
            width: min(80vw, 1120px);
            height: min(85vh, 860px);
            display: grid;
            grid-template-rows: auto 1fr;
            transform: translate(-50%, -46%);
            opacity: 0;
            visibility: hidden;
            transition: var(--transition);
        }

        .modal.is-visible {
            transform: translate(-50%, -50%);
            opacity: 1;
            visibility: visible;
        }

        .modal-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            padding: 18px 22px;
            border-bottom: 1px solid var(--border);
        }

        .modal-title {
            margin: 0;
            font-size: 1.25rem;
        }

        .modal-content {
            padding: 20px;
            overflow: auto;
        }

        .preview-layout {
            align-items: flex-start;
            gap: 20px;
        }

        .preview-info {
            width: 34%;
            display: grid;
            gap: 16px;
        }

        .preview-document-wrap {
            flex: 1;
        }

        .preview-document {
            min-height: 100%;
            padding: 26px;
            border-radius: var(--radius-xl);
            background: linear-gradient(180deg, rgba(255, 255, 255, 0.94), #ffffff);
            color: #0f172a;
            box-shadow: 0 20px 60px rgba(15, 23, 42, 0.18);
        }

        .preview-page {
            width: min(100%, 640px);
            margin: 0 auto 20px;
            padding: 38px 42px;
            border-radius: 20px;
            background: #fff;
            border: 1px solid rgba(15, 23, 42, 0.08);
            box-shadow: 0 10px 32px rgba(15, 23, 42, 0.08);
            line-height: 1.95;
        }

        .preview-page:last-child {
            margin-bottom: 0;
        }

        .preview-page h4,
        .preview-page h5,
        .preview-info h4,
        .editor-title {
            margin: 0 0 12px;
        }

        .preview-page p,
        .preview-page li,
        .preview-info p {
            margin: 0 0 12px;
        }

        .preview-info-card {
            padding: 18px;
            border-radius: var(--radius-lg);
        }

        .preview-list {
            margin: 0;
            padding-right: 18px;
            color: var(--text-secondary);
            line-height: 1.9;
        }

        .modal-actions {
            gap: 10px;
            flex-wrap: wrap;
        }

        .editor-shell {
            display: none;
            gap: 18px;
        }

        .editor-shell.is-visible {
            display: grid;
        }

        .editor-head {
            display: grid;
            gap: 8px;
        }

        .editor-kicker {
            color: var(--accent);
            font-weight: 700;
        }

        .toolbar {
            gap: 10px;
            flex-wrap: wrap;
            padding: 14px;
            border-radius: var(--radius-lg);
        }

        .toolbar button {
            min-width: 44px;
            padding: 10px 12px;
            color: var(--text-primary);
            background: rgba(148, 163, 184, 0.08);
            border: 1px solid var(--border);
            border-radius: 12px;
            cursor: pointer;
        }

        .editor-surface {
            min-height: 420px;
            padding: 24px;
            border-radius: var(--radius-xl);
            line-height: 2;
        }

        .editor-surface[contenteditable="true"]:empty::before {
            content: "Start tweaking the template right here...";
            color: var(--text-muted);
        }

        .editor-surface h3 {
            margin-top: 0;
        }

        .token-chip {
            color: #6b5200;
            background: rgba(250, 204, 21, 0.3);
            border: 1px dashed rgba(202, 138, 4, 0.56);
            cursor: pointer;
        }

        .inline-token-input {
            min-width: 150px;
            padding: 7px 10px;
            border: 1px solid rgba(250, 204, 21, 0.65);
            border-radius: 10px;
            background: rgba(255, 248, 220, 0.96);
            color: #4b3a00;
        }

        .editor-actions {
            gap: 12px;
            flex-wrap: wrap;
        }

        .template-preview-header {
            justify-content: space-between;
            gap: 12px;
        }

        .template-preview-badge {
            color: #031b16;
            background: rgba(0, 229, 180, 0.16);
            border: 1px solid rgba(0, 229, 180, 0.22);
        }

        .toast-stack {
            position: fixed;
            top: 20px;
            left: 20px;
            z-index: 260;
            display: grid;
            gap: 12px;
        }

        .toast {
            min-width: 280px;
            max-width: 360px;
            justify-content: space-between;
            gap: 12px;
            padding: 14px 16px;
            border-radius: 16px;
            animation: toast-in 0.28s ease;
        }

        .toast-success {
            border-color: rgba(34, 197, 94, 0.34);
        }

        .toast-info {
            border-color: rgba(59, 130, 246, 0.34);
        }

        .toast-close {
            color: inherit;
            background: transparent;
            border: 0;
            cursor: pointer;
        }

        .empty-state {
            padding: 34px 20px;
            text-align: center;
            color: var(--text-secondary);
        }

        @keyframes fade-view {
            from {
                opacity: 0;
                transform: translateY(6px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes toast-in {
            from {
                opacity: 0;
                transform: translateX(-14px);
            }

            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        @media (max-width: 1180px) {
            .templates-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }

            .preview-layout {
                flex-direction: column;
            }

            .preview-info {
                width: 100%;
            }
        }

        @media (max-width: 1023px) {
            .sidebar {
                transform: translateX(110%);
                transition: transform 0.3s ease;
            }

            .sidebar.is-open {
                transform: translateX(0);
            }

            .main-content {
                margin-right: 0;
            }

            .close-sidebar,
            .menu-toggle {
                display: inline-flex;
            }

            .top-header {
                grid-template-columns: 1fr;
            }

            .modal {
                width: min(92vw, 1120px);
            }
        }

        @media (max-width: 767px) {

            .page-content,
            .top-header {
                padding-right: 18px;
                padding-left: 18px;
            }

            .header-actions {
                flex-wrap: wrap;
                justify-content: flex-start;
            }

            .templates-grid {
                grid-template-columns: 1fr;
            }

            .filters-row,
            .template-actions,
            .modal-header,
            .editor-actions {
                flex-direction: column;
                align-items: stretch;
            }

            .modal {
                width: 96vw;
                height: 88vh;
            }

            .preview-page {
                padding: 26px 22px;
            }
        }
