@php
    $membership = auth()->user()?->memberships()->with('organization')->first();
    $organization = $membership?->organization;
    $canUpdateLogo = $membership !== null && ($membership->role === 'owner' || $membership->hasPrivilege('customize_widget'));
@endphp
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ $title ?? 'VejalNai Chatbot' }}</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/@phosphor-icons/web"></script>
    <style>
        :root {
            color-scheme: light;
            --sidebar-w: 245px;
            --inbox-list-w: 366px;
            --inbox-context-w: 294px;
            --bg: #f8fafc;
            --surface: rgba(255, 255, 255, 0.25);
            --surface-soft: rgba(255, 255, 255, 0.15);
            --surface-blue: rgba(94, 106, 210, 0.08);
            --active-blue: rgba(94, 106, 210, 0.15);
            --border: rgba(255, 255, 255, 0.5);
            --border-strong: rgba(255, 255, 255, 0.8);
            --glass-blur: blur(36px) saturate(220%);
            --text: #0f172a;
            --muted: #64748b;
            --blue: #6366f1;
            --blue-dark: #4f46e5;
            --green: #22c55e;
            --green-dark: #16a34a;
            --red: #ef4444;
            --amber: #f59e0b;
            --amber-soft: rgba(245, 158, 11, 0.15);
            --red-soft: rgba(239, 68, 68, 0.15);
            --shadow: 0 12px 40px rgba(31, 38, 135, 0.12), inset 0 1px 1px rgba(255, 255, 255, 0.8), inset 1px 0 1px rgba(255, 255, 255, 0.4);
            font-family: 'Outfit', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
            color: var(--text);
            background: var(--bg);
        }

        * {
            box-sizing: border-box;
        }

        @keyframes floatMesh {
            0% {
                background-position: 0% 0%;
            }

            50% {
                background-position: 100% 100%;
            }

            100% {
                background-position: 0% 0%;
            }
        }

        body.weather-night {
            background:
                radial-gradient(at 80% 10%, rgba(88, 28, 135, 0.35) 0%, transparent 50%),
                radial-gradient(at 10% 90%, rgba(59, 130, 246, 0.25) 0%, transparent 50%),
                radial-gradient(at 50% 50%, rgba(30, 27, 75, 0.2) 0%, transparent 60%),
                radial-gradient(at 10% 10%, rgba(30, 58, 138, 0.25) 0%, transparent 50%),
                radial-gradient(at 90% 90%, rgba(55, 48, 163, 0.2) 0%, transparent 50%);
            background-color: #f1f5f9;
        }

        body.weather-default {
            background: #f8fafc;
            animation: none;
        }

        .rain-overlay {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            pointer-events: none;
            z-index: 0;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cline x1='20' y1='0' x2='20' y2='30' stroke='rgba(200,230,255,0.5)' stroke-width='2'/%3E%3Cline x1='80' y1='50' x2='80' y2='90' stroke='rgba(220,240,255,0.4)' stroke-width='1.5'/%3E%3Cline x1='140' y1='20' x2='140' y2='60' stroke='rgba(210,235,255,0.45)' stroke-width='2'/%3E%3Cline x1='180' y1='100' x2='180' y2='130' stroke='rgba(230,245,255,0.3)' stroke-width='1'/%3E%3C/svg%3E");
            animation: rainFall 1.8s linear infinite;
        }

        @keyframes rainFall {
            0% { background-position: 0px 0px; }
            100% { background-position: 30px 1000px; }
        }

        .lightning-overlay {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            pointer-events: none;
            z-index: 1;
            background-color: transparent;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath d='M50,0 L20,50 L40,50 L10,100 L70,45 L45,45 Z' fill='rgba(255,255,255,0.9)' /%3E%3C/svg%3E");
            background-size: 150px 300px;
            background-position: 75% 0;
            background-repeat: no-repeat;
            opacity: 0;
            animation: lightningFlash 15s infinite;
        }

        @keyframes lightningFlash {
            0%, 94% { opacity: 0; background-color: transparent; }
            95% { opacity: 1; background-color: rgba(255, 255, 255, 0.4); }
            96% { opacity: 0; background-color: transparent; }
            97% { opacity: 1; background-color: rgba(255, 255, 255, 0.6); }
            98%, 100% { opacity: 0; background-color: transparent; }
        }

        .sun-overlay {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            pointer-events: none;
            z-index: 0;
            background: radial-gradient(circle at 15% 15%, rgba(255, 230, 150, 0.5) 0%, transparent 60%);
        }

        .cloud-overlay {
            position: fixed;
            top: -10%; left: -20%; right: -20%; height: 60vh;
            pointer-events: none;
            z-index: 0;
            opacity: 0.9;
            -webkit-mask-image: linear-gradient(to bottom, black 30%, transparent 100%);
            mask-image: linear-gradient(to bottom, black 30%, transparent 100%);
            animation: cloudDrift 60s ease-in-out infinite alternate;
        }

        body.weather-cloudy .cloud-overlay, body.weather-snowy .cloud-overlay {
            background-image: url("data:image/svg+xml,%3Csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='clouds'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.01 0.015' numOctaves='4' seed='15' /%3E%3CfeColorMatrix type='matrix' values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1.5 -0.4' /%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23clouds)' /%3E%3C/svg%3E");
            background-size: cover;
        }

        body.weather-rainy .cloud-overlay {
            background-image: url("data:image/svg+xml,%3Csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='clouds'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.01 0.015' numOctaves='4' seed='15' /%3E%3CfeColorMatrix type='matrix' values='0 0 0 0 0.3 0 0 0 0 0.35 0 0 0 0 0.45 0 0 0 1.8 -0.5' /%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23clouds)' /%3E%3C/svg%3E");
            background-size: cover;
        }

        @keyframes cloudDrift {
            0% { transform: translateX(-5%); }
            100% { transform: translateX(5%); }
        }

        body {
            background:
                radial-gradient(at 80% 10%, rgba(255, 107, 237, 0.35) 0%, transparent 50%),
                radial-gradient(at 10% 90%, rgba(59, 130, 246, 0.35) 0%, transparent 50%),
                radial-gradient(at 50% 50%, rgba(168, 85, 247, 0.25) 0%, transparent 60%),
                radial-gradient(at 10% 10%, rgba(253, 186, 116, 0.25) 0%, transparent 50%),
                radial-gradient(at 90% 90%, rgba(45, 212, 191, 0.35) 0%, transparent 50%);
            background-attachment: fixed;
            background-color: #f8fafc;
            background-size: 150% 150%;
            animation: floatMesh 30s ease-in-out infinite alternate;
            margin: 0;
            min-height: 100vh;
        }

        /* Custom sleek scrollbars */
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }

        ::-webkit-scrollbar-track {
            background: transparent;
        }

        ::-webkit-scrollbar-thumb {
            background: rgba(115, 129, 151, 0.2);
            border-radius: 10px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: rgba(115, 129, 151, 0.4);
        }

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

        button,
        input,
        textarea {
            font: inherit;
        }

        .app-shell {
            display: grid;
            grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
            height: 100vh;
            position: relative;
            z-index: 10;
            overflow: hidden;
            padding: 12px;
            gap: 12px;
        }

        .resizer {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 8px;
            cursor: col-resize;
            z-index: 50;
            transition: background 0.2s;
        }
        
        .resizer:hover, .resizer.dragging {
            background: var(--blue);
            opacity: 0.5;
        }
        
        .resizer[data-edge="right"] { right: -4px; }
        .resizer[data-edge="left"] { left: -4px; }

        .sidebar {
            background: var(--surface);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border: 1px solid var(--border);
            border-radius: 28px;
            box-shadow: var(--shadow);
            color: var(--text);
            display: flex;
            flex-direction: column;
            height: 100%;
            min-width: 0;
            padding: 16px 8px;
            overflow: hidden;
            position: sticky;
            top: 0;
            z-index: 30;
        }

        .brand {
            align-items: center;
            display: flex;
            gap: 10px;
            padding: 2px 8px 18px;
        }

        .brand-mark {
            align-items: center;
            background: linear-gradient(135deg, #6366f1 0%, #a855f7 50%, #ec4899 100%);
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
            border-radius: 999px;
            color: #ffffff;
            display: inline-flex;
            font-size: 18px;
            font-weight: 900;
            height: 42px;
            justify-content: center;
            position: relative;
            width: 42px;
        }

        .brand-mark::before {
            content: "";
            background: #43c73a;
            border: 2px solid #ffffff;
            border-radius: 999px;
            height: 8px;
            left: -1px;
            position: absolute;
            top: -1px;
            width: 8px;
        }

        .brand-name {
            color: var(--text);
            font-size: 15px;
            font-weight: 700;
        }

        .brand-logo-img {
            width: 42px;
            height: 42px;
            border-radius: 999px;
            object-fit: cover;
            background: transparent;
        }

        .workspace-name {
            color: var(--muted);
            display: block;
            font-size: 12px;
            font-weight: 500;
            margin-top: 2px;
        }

        .brand-logo-wrapper:hover .logo-upload-overlay {
            opacity: 1 !important;
        }

        .user-card-wrapper:hover .avatar-upload-overlay {
            opacity: 1 !important;
        }

        .home-button {
            align-items: center;
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 8px;
            color: var(--text);
            display: flex;
            font-size: 14px;
            font-weight: 600;
            gap: 8px;
            justify-content: center;
            margin: 14px 0 24px;
            min-height: 38px;
            padding: 8px 12px;
            text-decoration: none;
            transition: background-color 0.2s;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .home-button:hover {
            background: rgba(255, 255, 255, 0.5);
        }

        .setup-badge {
            align-items: center;
            background: var(--red);
            border-radius: 999px;
            display: inline-flex;
            font-size: 11px;
            height: 18px;
            justify-content: center;
            min-width: 18px;
            padding: 0 5px;
        }

        .nav,
        .sidebar-bottom {
            display: grid;
            gap: 6px;
        }

        .nav {
            overflow-y: auto;
            flex: 1;
            min-height: 0;
            padding-right: 4px;
        }

        .nav-section {
            color: var(--muted);
            font-size: 12px;
            margin: 6px 12px 2px;
        }

        .nav a,
        .logout-button {
            align-items: center;
            appearance: none;
            background: transparent;
            border: 1px solid transparent;
            border-radius: 8px;
            color: var(--muted);
            cursor: pointer;
            display: flex;
            font-size: 14px;
            font-weight: 500;
            gap: 10px;
            min-height: 36px;
            padding: 7px 12px;
            text-align: left;
            width: 100%;
            transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
        }

        .nav a:hover,
        .logout-button:hover {
            background: var(--surface-soft);
            color: var(--text);
            transform: translateX(4px);
        }

        .nav a.active {
            background: var(--surface-blue);
            border-color: rgba(99, 102, 241, 0.2);
            color: var(--blue);
            box-shadow: 0 2px 8px rgba(99, 102, 241, 0.1);
        }

        .nav-key {
            align-items: center;
            color: var(--muted);
            display: inline-flex;
            font-size: 15px;
            height: 20px;
            justify-content: center;
            width: 20px;
        }

        .nav a.active .nav-key {
            color: var(--blue);
        }

        .sidebar-bottom {
            border-top: 1px solid var(--border);
            margin: auto -8px 0;
            padding: 18px 8px 0;
        }

        .user-card {
            align-items: center;
            display: grid;
            gap: 8px;
            grid-template-columns: 34px minmax(0, 1fr);
            margin-top: 16px;
            padding: 0 12px;
        }

        .avatar {
            align-items: center;
            background: #b7cee8;
            border-radius: 999px;
            color: #ffffff;
            display: inline-flex;
            font-weight: 850;
            height: 34px;
            justify-content: center;
            width: 34px;
        }

        .main-shell {
            min-width: 0;
            display: flex;
            flex-direction: column;
            height: 100%;
            overflow-y: auto;
        }

        .main-shell main {
            display: flex;
            flex-direction: column;
            flex: 1;
            min-height: 0;
        }

        .topbar {
            align-items: center;
            background: var(--surface);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border-bottom: 1px solid var(--border);
            display: flex;
            gap: 16px;
            justify-content: space-between;
            min-height: 62px;
            padding: 0 24px;
            position: sticky;
            top: 0;
            z-index: 20;
        }

        .topbar-title {
            display: grid;
            gap: 2px;
        }

        .topbar-title strong {
            font-size: 15px;
        }

        .page {
            margin: 0 auto;
            max-width: 1220px;
            padding: 26px;
            width: 100%;
        }

        .page.flush {
            max-width: none;
            padding: 0;
            display: flex;
            flex-direction: column;
            flex: 1;
            min-height: 0;
        }

        .section-header {
            align-items: flex-start;
            display: flex;
            gap: 18px;
            justify-content: space-between;
            margin-bottom: 22px;
        }

        h1,
        h2,
        h3,
        p {
            letter-spacing: 0;
        }

        h1 {
            font-size: 28px;
            line-height: 1.15;
            margin: 0 0 6px;
        }

        h2 {
            font-size: 18px;
            line-height: 1.25;
            margin: 0;
        }

        h3 {
            font-size: 15px;
            line-height: 1.3;
            margin: 0;
        }

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

        .button {
            align-items: center;
            appearance: none;
            background: linear-gradient(180deg, var(--blue), var(--blue-dark));
            border: 1px solid var(--blue-dark);
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.2);
            border-radius: 8px;
            color: #ffffff;
            cursor: pointer;
            display: inline-flex;
            font-weight: 600;
            justify-content: center;
            min-height: 38px;
            padding: 8px 16px;
            text-decoration: none;
            white-space: nowrap;
            transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
        }

        .button:hover {
            filter: brightness(1.15);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.2);
        }

        .button.secondary {
            background: var(--surface-soft);
            border-color: var(--border-strong);
            color: var(--text);
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
        }

        .button.secondary:hover {
            background: var(--surface);
            border-color: var(--muted);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }

        .button.subtle {
            background: transparent;
            border-color: transparent;
            color: var(--muted);
            box-shadow: none;
        }

        .button.subtle:hover {
            background: var(--surface-soft);
            color: var(--text);
            border-color: var(--border);
        }

        .icon-button {
            align-items: center;
            appearance: none;
            background: transparent;
            border: 0;
            border-radius: 6px;
            color: var(--muted);
            cursor: pointer;
            display: inline-flex;
            font-weight: 850;
            height: 34px;
            justify-content: center;
            min-width: 34px;
            padding: 0 8px;
        }

        .icon-button:hover {
            background: #eef2f7;
            color: var(--text);
        }

        .panel,
        .card {
            background: var(--surface);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border: 1px solid var(--border);
            border-radius: 12px;
            box-shadow: var(--shadow);
            transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.2s;
        }

        .card:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
        }

        .panel {
            overflow: hidden;
        }

        .panel-header {
            align-items: center;
            border-bottom: 1px solid var(--border);
            display: flex;
            gap: 12px;
            justify-content: space-between;
            min-height: 52px;
            padding: 12px 14px;
        }

        .panel-body {
            padding: 18px;
        }

        .card {
            padding: 18px;
        }

        .grid {
            display: grid;
            gap: 16px;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        }

        .stack {
            display: grid;
            gap: 14px;
        }

        .metric-card {
            min-height: 116px;
        }

        .metric-value {
            display: block;
            font-size: 30px;
            font-weight: 850;
            line-height: 1;
            margin: 12px 0 6px;
        }

        .metric-label {
            color: var(--muted);
            font-size: 12px;
            font-weight: 850;
            text-transform: uppercase;
        }

        .badge {
            align-items: center;
            background: #e9f7ef;
            border-radius: 999px;
            color: #087a4d;
            display: inline-flex;
            font-size: 11px;
            font-weight: 850;
            padding: 4px 8px;
            white-space: nowrap;
        }

        .badge.info {
            background: var(--surface-blue);
            color: #0f5bcc;
        }

        .badge.warning {
            background: var(--amber-soft);
            color: var(--amber);
        }

        .badge.danger {
            background: var(--red-soft);
            color: #b42337;
        }

        .table-wrap {
            overflow-x: auto;
        }

        .table {
            border-collapse: collapse;
            width: 100%;
        }

        .table th,
        .table td {
            border-bottom: 1px solid var(--border);
            padding: 13px 0;
            text-align: left;
            vertical-align: top;
        }

        .table th {
            color: var(--muted);
            font-size: 12px;
            font-weight: 850;
            text-transform: uppercase;
        }

        .table tr:last-child td {
            border-bottom: 0;
        }

        .form-control {
            background: rgba(255, 255, 255, 0.4);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border: 1px solid var(--border-strong);
            border-radius: 8px;
            color: var(--text);
            min-height: 42px;
            padding: 10px 14px;
            width: 100%;
            transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
        }

        .form-control:focus {
            background: rgba(255, 255, 255, 0.7);
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15), inset 0 1px 3px rgba(0, 0, 0, 0.05);
            outline: 0;
        }

        textarea.form-control {
            min-height: 96px;
            resize: vertical;
        }

        .field-label {
            color: var(--muted);
            display: block;
            font-size: 12px;
            font-weight: 850;
            margin-bottom: 6px;
            text-transform: uppercase;
        }

        .flash {
            background: #e9f7ef;
            border: 1px solid #bce7d0;
            border-radius: 7px;
            color: #087a4d;
            margin: 12px;
            padding: 10px 12px;
        }

        .page:not(.flush) .flash {
            margin: 0 0 16px;
        }

        .empty-state {
            color: var(--muted);
            padding: 36px 18px;
            text-align: center;
        }

        .inbox-shell {
            border: 0;
            border-radius: 0;
            box-shadow: none;
            display: grid;
            grid-template-columns: var(--inbox-list-w) minmax(0, 1fr) var(--inbox-context-w);
            grid-template-rows: minmax(0, 1fr);
            flex: 1;
            min-height: 0;
            position: relative;
            background: transparent;
            gap: 12px;
        }

        .inbox-list,
        .inbox-thread,
        .inbox-context {
            min-width: 0;
            background: var(--surface);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border: 1px solid var(--border);
            border-radius: 28px;
            box-shadow: var(--shadow);
            overflow: hidden;
            position: relative;
        }

        .inbox-context {
            display: flex;
            flex-direction: column;
        }

        .inbox-list {
            display: grid;
            grid-template-rows: auto 1fr auto;
            min-height: 0;
        }

        .list-scroll,
        .thread-scroll,
        .context-scroll {
            overflow-y: auto;
            padding: 18px;
            min-width: 0;
            min-height: 0;
        }

        .filter-pill {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 600;
            color: var(--text-muted, #64748b);
            background: var(--surface-soft);
            text-decoration: none;
            transition: all 0.2s ease;
            white-space: nowrap;
        }

        .filter-pill:hover {
            background: var(--surface-hover, #e2e8f0);
            color: var(--text);
        }

        .filter-pill.active {
            background: var(--blue);
            color: white;
            box-shadow: 0 2px 6px rgba(59, 130, 246, 0.4);
        }

        .filter-select {
            align-items: center;
            background: var(--surface-soft);
            border: 1px solid var(--border-strong);
            border-radius: 6px;
            display: inline-flex;
            font-weight: 750;
            min-height: 34px;
            padding: 6px 12px;
            color: var(--text);
        }

        .conversation-row {
            border-bottom: 1px solid var(--border);
            display: grid;
            gap: 8px;
            padding: 16px;
            transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
            cursor: pointer;
        }

        .conversation-row:hover,
        .conversation-row.active {
            background: var(--surface-soft);
        }

        .conversation-row.active {
            border-left: 4px solid var(--blue);
            background: var(--active-blue);
            padding-left: 12px;
        }

        .row-title {
            align-items: center;
            display: flex;
            gap: 10px;
            justify-content: space-between;
        }

        .row-avatar {
            align-items: center;
            background: #1da4f2;
            border-radius: 999px;
            color: #ffffff;
            display: inline-flex;
            font-size: 12px;
            font-weight: 900;
            height: 38px;
            justify-content: center;
            width: 38px;
        }

        .row-avatar-wrapper {
            position: relative;
            display: inline-flex;
        }

        .row-channel-badge {
            position: absolute;
            bottom: -2px;
            right: -2px;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
        }

        .row-channel-badge i {
            font-size: 14px;
        }

        .channel-card {
            background: #e8edf4;
            border-radius: 6px;
            margin: 16px;
            padding: 26px 14px 14px;
        }

        .channel-icons {
            display: flex;
            gap: 7px;
            margin-bottom: 22px;
        }

        .channel-icon {
            align-items: center;
            background: #ffffff;
            border-radius: 4px;
            color: var(--blue);
            display: inline-flex;
            font-size: 13px;
            font-weight: 900;
            height: 40px;
            justify-content: center;
            width: 40px;
        }

        .inbox-thread {
            display: grid;
            grid-template-rows: auto 1fr auto;
            min-height: 0;
        }

        .thread-toolbar {
            align-items: center;
            background: var(--surface);
            border-bottom: 1px solid var(--border);
            display: flex;
            gap: 10px;
            justify-content: space-between;
            padding: 8px 12px;
        }

        .thread-scroll {
            display: grid;
            gap: 10px;
            padding: 18px 10px 18px 42px;
            align-content: start;
        }

        .message {
            max-width: min(520px, 88%);
            padding: 10px 14px;
            word-break: break-word;
            font-size: 14.5px;
            line-height: 1.4;
            position: relative;
        }

        .message.customer {
            justify-self: start;
            background: var(--surface);
            border: none;
            box-shadow: 0 1px 2px rgba(0,0,0,0.05);
            border-radius: 18px 18px 18px 4px;
            color: var(--text);
        }

        .message.bot {
            justify-self: end;
            background: var(--org-bot-bg, var(--green));
            border: none;
            box-shadow: 0 1px 2px rgba(0,0,0, 0.15);
            border-radius: 18px 18px 4px 18px;
            color: var(--org-bot-text, #ffffff);
        }

        .message {
            position: relative;
        }

        .message-actions {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            opacity: 0;
            transition: opacity 0.2s ease;
            display: flex;
            gap: 4px;
        }

        .message-time {
            color: var(--muted);
            font-size: 11px;
            font-weight: 600;
            margin-top: 6px;
            text-align: right;
        }

        .message.bot .message-time {
            color: rgba(255, 255, 255, 0.75);
        }

        .message.customer .message-actions {
            right: -36px;
        }

        .message.bot .message-actions {
            left: -36px;
        }

        .message:hover .message-actions {
            opacity: 1;
        }

        .reply-button {
            width: 28px;
            height: 28px;
            padding: 0;
            border-radius: 999px;
            background: #ffffff;
            border: 1px solid var(--border);
            color: var(--muted);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
            cursor: pointer;
        }

        .reply-button:hover {
            color: var(--blue);
            border-color: var(--blue);
        }

        .message-quoted {
            background: rgba(0, 0, 0, 0.04);
            border-left: 3px solid var(--blue);
            padding: 8px 12px;
            margin-bottom: 8px;
            border-radius: 4px;
            font-size: 13px;
            color: var(--muted);
            display: flex;
            gap: 8px;
            align-items: flex-start;
        }

        .message-quoted i {
            margin-top: 2px;
        }

        .reply-preview-box {
            background: #f4f7fb;
            border-left: 3px solid var(--blue);
            padding: 8px 30px 8px 12px;
            margin: 12px 12px 0 12px;
            border-radius: 4px;
            position: relative;
        }

        .reply-cancel-btn {
            position: absolute;
            right: 8px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            cursor: pointer;
            color: var(--muted);
            font-size: 16px;
            padding: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .reply-cancel-btn:hover {
            color: var(--text);
        }

        .message-meta {
            align-items: center;
            color: var(--muted);
            display: flex;
            font-size: 12px;
            gap: 10px;
            justify-content: space-between;
            margin-bottom: 6px;
        }

        .composer {
            background: var(--surface);
            backdrop-filter: var(--glass-blur);
            -webkit-backdrop-filter: var(--glass-blur);
            border-radius: 12px;
            box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.4), 0 0 0 1px var(--border);
            margin: 0;
            padding: 16px;
            z-index: 10;
        }

        .composer textarea.form-control {
            border: 0;
            box-shadow: none;
            padding: 0;
            resize: none;
            min-height: 80px;
            background: transparent;
        }

        .composer textarea.form-control:focus {
            box-shadow: none;
        }

        .composer-tabs {
            align-items: center;
            background: var(--surface-soft);
            border-radius: 6px;
            display: inline-flex;
            gap: 4px;
            margin-bottom: 12px;
            padding: 4px;
        }

        .composer-tabs span {
            border-radius: 4px;
            color: var(--muted);
            cursor: pointer;
            font-size: 13px;
            font-weight: 600;
            padding: 6px 14px;
            transition: all 0.15s ease;
        }

        .composer-tabs span:hover {
            color: var(--text);
        }

        .composer-tabs span:first-child {
            background: var(--surface-soft);
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
            color: var(--blue);
        }



        .context-profile {
            align-items: center;
            display: grid;
            gap: 8px;
            justify-items: center;
            padding: 18px;
            text-align: center;
        }

        .profile-avatar {
            align-items: center;
            background: #1da4f2;
            border-radius: 999px;
            color: #ffffff;
            display: inline-flex;
            font-size: 22px;
            font-weight: 900;
            height: 62px;
            justify-content: center;
            width: 62px;
        }

        .context-block {
            border-top: 1px solid var(--border);
            padding: 14px 16px;
        }

        .context-title {
            align-items: center;
            display: flex;
            justify-content: space-between;
        }

        .activity-row {
            align-items: center;
            border-top: 1px solid var(--border);
            display: grid;
            gap: 8px;
            grid-template-columns: minmax(0, 1fr) auto;
            padding: 12px 0;
        }

        .activity-row:first-child {
            border-top: 0;
            padding-top: 0;
        }

        .code-box {
            background: #0f172a;
            border: 1px solid #1e293b;
            border-radius: 8px;
            color: #dbeafe;
            font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
            font-size: 12px;
            line-height: 1.55;
            min-height: 140px;
            padding: 14px;
            width: 100%;
        }

        .settings-tabs {
            align-items: center;
            display: flex;
            gap: 8px;
            margin-bottom: 16px;
        }

        .settings-tabs a {
            border: 1px solid var(--border);
            border-radius: 999px;
            color: var(--muted);
            font-size: 13px;
            font-weight: 850;
            padding: 8px 12px;
        }

        .settings-tabs a.active,
        .settings-tabs a:hover {
            background: var(--surface-blue);
            border-color: #bdd5ff;
            color: var(--blue);
        }

        .field-hint {
            color: var(--muted);
            display: block;
            font-size: 12px;
            margin-top: 6px;
        }

        .field-error {
            color: #b42337;
            display: block;
            font-size: 12px;
            font-weight: 750;
            margin-top: 6px;
        }

        .check-row {
            align-items: center;
            display: flex;
            gap: 9px;
        }

        .permission-grid {
            display: grid;
            gap: 8px;
            grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
        }

        .permission-item {
            align-items: center;
            background: var(--surface-soft);
            border: 1px solid var(--border);
            border-radius: 7px;
            display: flex;
            gap: 8px;
            min-height: 38px;
            padding: 8px 10px;
        }

        .member-card {
            display: grid;
            gap: 14px;
        }

        .team-table {
            border-collapse: separate;
            border-spacing: 0;
            min-width: 1180px;
            width: 100%;
        }

        .team-table th {
            background: #f8fafc;
            border-bottom: 1px solid var(--border);
            color: var(--muted);
            font-size: 11px;
            font-weight: 850;
            padding: 10px;
            text-align: left;
            text-transform: uppercase;
            white-space: nowrap;
        }

        .team-table td {
            border-bottom: 1px solid var(--border);
            padding: 10px;
            vertical-align: top;
        }

        .team-table tr:last-child td {
            border-bottom: 0;
        }

        .team-table .form-control {
            min-height: 36px;
            padding: 8px 10px;
        }

        .team-user-cell {
            display: grid;
            gap: 8px;
            min-width: 230px;
        }

        .team-privileges {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
            min-width: 430px;
        }

        .team-privilege {
            align-items: center;
            background: var(--surface-soft);
            border: 1px solid var(--border);
            border-radius: 6px;
            display: inline-flex;
            font-size: 12px;
            font-weight: 750;
            gap: 6px;
            line-height: 1.1;
            min-height: 30px;
            padding: 6px 8px;
            white-space: nowrap;
        }

        .team-actions {
            display: flex;
            gap: 8px;
            justify-content: flex-end;
            min-width: 150px;
        }

        .color-row {
            align-items: center;
            display: grid;
            gap: 10px;
            grid-template-columns: 56px minmax(0, 1fr);
        }

        .color-row input[type="color"] {
            background: transparent;
            border: 0;
            height: 42px;
            padding: 0;
            width: 56px;
        }

        .appearance-preview {
            background: #eef3f9;
            border: 1px solid var(--border);
            border-radius: 8px;
            max-height: calc(100vh - 100px);
            overflow-y: auto;
            overflow-x: hidden;
            padding: 18px;
            position: sticky;
            top: 84px;
        }

        .appearance-layout {
            align-items: start;
            display: grid;
            gap: 16px;
            grid-template-columns: minmax(0, 1.05fr) minmax(360px, 0.95fr);
        }

        .form-two-col {
            display: grid;
            gap: 16px;
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }

        .preview-widget {
            background: #ffffff;
            border: 1px solid var(--border);
            border-radius: 16px;
            box-shadow: 0 18px 54px rgba(21, 40, 73, 0.18);
            display: grid;
            grid-template-rows: auto 1fr auto;
            margin-left: auto;
            max-width: 390px;
            min-height: 540px;
            overflow: hidden;
        }

        .preview-widget.left {
            margin-left: 0;
            margin-right: auto;
        }

        .preview-scale-wrapper {
            transform: scale(var(--preview-scale, 1));
            transform-origin: bottom right;
            display: flex;
            flex-direction: column;
            width: 100%;
            transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
        }

        .preview-scale-wrapper.left {
            transform-origin: bottom left;
        }

        .preview-header {
            background: var(--preview-header-bg, linear-gradient(135deg, var(--preview-accent, var(--blue)), #0d54c7));
            color: var(--preview-header-text, #ffffff);
            display: grid;
            gap: 12px;
            padding: 16px;
        }

        .preview-brand {
            align-items: center;
            display: flex;
            gap: 10px;
            min-width: 0;
        }

        .preview-logo {
            align-items: center;
            background: #ffffff;
            background-position: center;
            background-size: cover;
            border-radius: 50%;
            color: var(--preview-accent, var(--blue));
            display: inline-flex;
            flex: 0 0 auto;
            font-weight: 950;
            height: 42px;
            justify-content: center;
            width: 42px;
            aspect-ratio: 1 / 1;
        }

        .preview-status {
            align-items: center;
            background: var(--preview-status-bg, rgba(255, 255, 255, 0.13));
            border: 1px solid rgba(255, 255, 255, 0.18);
            border-radius: 12px;
            display: flex;
            font-size: 12px;
            gap: 8px;
            padding: 8px 10px;
        }

        .preview-body {
            background: var(--preview-body-bg, #f6f8fc);
            display: grid;
            gap: 10px;
            padding: 14px;
        }

        .preview-date {
            background: var(--preview-date-bg, #ffffff);
            border-radius: 8px;
            color: var(--preview-date-text, var(--muted));
            font-weight: 850;
            line-height: 1;
            padding: 6px 9px;
            justify-self: center;
        }

        .preview-bubble {
            background: var(--preview-bot-bg, #ffffff);
            border: 1px solid var(--border);
            border-radius: 10px;
            border-bottom-left-radius: 4px;
            box-shadow: 0 1px 4px rgba(21, 40, 73, 0.06);
            color: var(--preview-bot-text, var(--text));
            line-height: 1.45;
            max-width: 84%;
            padding: 12px 14px;
        }

        .preview-chips {
            display: flex;
            flex-wrap: wrap;
            gap: 7px;
            max-width: 92%;
        }

        .preview-chip {
            background: var(--preview-chip-bg, #ffffff);
            border: 1px solid var(--border);
            border-radius: 9px;
            box-shadow: 0 1px 4px rgba(21, 40, 73, 0.04);
            color: var(--preview-chip-text, var(--text));
            font-size: 12px;
            font-weight: 850;
            line-height: 1.1;
            max-width: 100%;
            padding: 8px 10px;
            white-space: normal;
        }

        .preview-composer {
            background: var(--preview-composer-bg, #ffffff);
            border-top: 1px solid var(--border);
            display: grid;
            gap: 8px;
            grid-template-columns: 1fr 50px;
            padding: 11px;
        }

        .preview-input {
            background: var(--preview-input-bg, #ffffff);
            border: 1px solid var(--border-strong);
            border-radius: 12px;
            color: var(--muted);
            min-height: 44px;
            padding: 11px 12px;
        }

        .preview-send {
            align-items: center;
            background: var(--preview-send-bg, var(--preview-accent, var(--blue)));
            border-radius: 12px;
            color: #ffffff;
            display: inline-flex;
            font-size: 22px;
            justify-content: center;
        }

        @media (max-width: 1120px) {
            .inbox-shell {
                grid-template-columns: 320px minmax(360px, 1fr);
            }

            .inbox-context {
                display: none;
            }
        }

        @media (max-width: 820px) {
            .app-shell {
                grid-template-columns: 1fr;
            }

            .sidebar {
                height: auto;
                position: static;
            }

            .nav {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }

            .sidebar-bottom {
                margin-top: 18px;
            }

            .topbar {
                position: static;
            }

            .inbox-shell {
                grid-template-columns: 1fr;
                height: auto;
            }

            .inbox-list {
                border-right: 0;
                max-height: 420px;
            }

            .inbox-thread {
                border-top: 1px solid var(--border);
            }

            .thread-scroll {
                padding-left: 14px;
            }

            .appearance-layout {
                grid-template-columns: 1fr;
            }

            .appearance-preview {
                max-height: none;
                position: static;
            }
        }

        .builder-editable {
            outline: 2px dashed transparent;
            outline-offset: 2px;
            transition: outline-color 0.15s ease, background-color 0.15s ease;
            cursor: pointer;
            border-radius: 4px;
        }
        
        .builder-editable:hover {
            outline-color: rgba(99, 102, 241, 0.4);
        }

        .builder-editable:focus {
            outline: 2px solid var(--blue);
            background-color: rgba(255, 255, 255, 0.1);
        }

        .builder-section {
            cursor: pointer;
            position: relative;
        }

        .builder-section::after {
            content: '';
            position: absolute;
            inset: -4px;
            border: 2px dashed transparent;
            border-radius: inherit;
            pointer-events: none;
            transition: border-color 0.15s ease;
        }

        .builder-section:hover::after {
            border-color: rgba(99, 102, 241, 0.4);
        }

        @media (max-width: 620px) {
            .form-two-col {
                grid-template-columns: 1fr;
            }

            .settings-tabs {
                align-items: stretch;
                flex-direction: column;
            }
        }
    </style>
    @if(isset($organization))
    <style>
        :root {
            --org-bot-bg: {{ $organization->widget_bot_message_background_color ?: 'var(--green)' }};
            --org-bot-text: {{ $organization->widget_bot_message_text_color ?: '#ffffff' }};
        }
    </style>
    @endif
</head>

<body>
    @auth
    <div class="app-shell" style="--sidebar-w: {{ $_COOKIE['sidebar-w'] ?? '245' }}px">
        <aside class="sidebar" style="position: relative; overflow-x: hidden;">
            <div class="resizer" data-target="sidebar-w" data-edge="right" data-default="245" data-min="60" data-max="400"></div>
            <div class="brand" style="display: flex; align-items: center; gap: 12px; text-decoration: none;">
                <div class="brand-logo-wrapper" @style(['position: relative', 'cursor: pointer' => $canUpdateLogo, 'cursor: default' => !$canUpdateLogo]) @if($canUpdateLogo) onclick="document.getElementById('logo-upload-input').click()" title="Click to update logo" @endif>
                    @if($organization && $organization->logo_path)
                        <img src="{{ Storage::url($organization->logo_path) }}" alt="Logo" class="brand-logo-img">
                    @else
                        <span class="brand-mark">V</span>
                    @endif
                    
                    @if($canUpdateLogo)
                        <div class="logo-upload-overlay" style="position: absolute; inset: 0; background: rgba(0,0,0,0.5); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.2s;">
                            <i class="ph ph-camera" style="color: white; font-size: 16px;"></i>
                        </div>
                    @endif
                </div>

                <a href="{{ route('dashboard') }}" style="text-decoration: none; display: flex; flex-direction: column;">
                    <span class="brand-name">{{ $organization?->name ?? 'VejalNai' }}</span>
                    <span class="workspace-name">{{ $organization?->slug ?? 'www.vejalnai.com' }}</span>
                </a>
            </div>

            @if($canUpdateLogo)
                <form id="logo-upload-form" action="{{ route('settings.logo.update') }}" method="POST" enctype="multipart/form-data" style="display: none;">
                    @csrf
                    <input type="file" id="logo-upload-input" name="logo" accept="image/*" onchange="document.getElementById('logo-upload-form').submit()">
                </form>
            @endif
            
            <div class="panel-header" style="padding: 16px 8px 8px 8px;">
                <div style="font-weight: 700; font-size: 16px; color: var(--text);">Menu</div>
            </div>

            <a class="home-button" href="{{ route('dashboard') }}">
                <i class="ph ph-house" style="font-size: 18px;"></i>
                <span>Dashboard</span>
            </a>

            <nav class="nav" aria-label="Primary navigation">
                <a @class(['active'=> request()->routeIs('conversations.*')]) href="{{ route('conversations.index') }}">
                    <span class="nav-key">o</span>
                    <span>Inbox</span>
                </a>
                <div class="nav-section">Default Inboxes</div>
                <a @class(['active'=> request()->routeIs('conversations.*')]) href="{{ route('conversations.index') }}" style="margin-left: 12px; width: calc(100% - 12px);">
                    <span class="nav-key">.</span>
                    <span>Main Inbox</span>
                </a>
                <div class="nav-section">Your Inboxes</div>
                <a href="{{ route('conversations.index') }}" style="margin-left: 12px; width: calc(100% - 12px);">
                    <span class="nav-key">+</span>
                    <span>New sub-inbox</span>
                </a>
                <div class="nav-section">Other Inboxes</div>
                <a href="{{ route('unknown-questions.index') }}" @class(['active'=> request()->routeIs('unknown-questions.*')]) style="margin-left: 12px; width: calc(100% - 12px);">
                    <span class="nav-key">R</span>
                    <span>Reports</span>
                </a>
            </nav>

            <div class="sidebar-bottom">
                <nav class="nav" aria-label="Workspace navigation">
                    <a href="{{ route('knowledge-base.index') }}" @class(['active'=> request()->routeIs('knowledge-base.*')])>
                        <span class="nav-key">K</span>
                        <span>Knowledge base</span>
                    </a>
                    <a href="{{ route('teams.index') }}" @class(['active'=> request()->routeIs('teams.*')])>
                        <span class="nav-key">T</span>
                        <span>Teams</span>
                    </a>
                    <a href="{{ route('settings.appearance.edit') }}" @class(['active'=> request()->routeIs('settings.*')])>
                        <span class="nav-key">G</span>
                        <span>Settings</span>
                    </a>
                </nav>

                <div class="user-card user-card-wrapper" style="position: relative; cursor: pointer;" onclick="document.getElementById('avatar-upload-input').click()" title="Click to update avatar">
                    <div style="position: relative; display: flex;">
                        @if(auth()->user()->avatar_path)
                            <img src="{{ Storage::url(auth()->user()->avatar_path) }}" alt="Avatar" class="avatar" style="object-fit: cover; background: transparent; padding: 0;">
                        @else
                            <span class="avatar">{{ strtoupper(substr(auth()->user()->name, 0, 1)) }}</span>
                        @endif
                        
                        <div class="avatar-upload-overlay" style="position: absolute; inset: 0; background: rgba(0,0,0,0.5); border-radius: 999px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.2s;">
                            <i class="ph ph-camera" style="color: white; font-size: 16px;"></i>
                        </div>
                    </div>
                    <span style="min-width: 0;">
                        <strong style="display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ auth()->user()->name }}</strong>
                        <span class="muted" style="display: block; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ auth()->user()->email }}</span>
                    </span>
                </div>

                <form id="avatar-upload-form" action="{{ route('settings.avatar.update') }}" method="POST" enctype="multipart/form-data" style="display: none;">
                    @csrf
                    <input type="file" id="avatar-upload-input" name="avatar" accept="image/*" onchange="document.getElementById('avatar-upload-form').submit()">
                </form>

                <form method="POST" action="{{ route('logout') }}" style="margin-top: 8px;">
                    @csrf
                    <button class="logout-button" type="submit">
                        <span class="nav-key">Q</span>
                        <span>Logout</span>
                    </button>
                </form>
            </div>
        </aside>

        <div class="main-shell">
            <main class="main-content">
                @yield('content')
            </main>
        </div>
    </div>
    @else
        @yield('content')
    @endauth

    <dialog id="confirm-dialog" style="padding: 24px; border: none; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.2); max-width: 400px; background: #fff; margin: auto;">
        <div style="display: flex; flex-direction: column; gap: 16px;">
            <h3 id="confirm-dialog-title" style="margin: 0; font-size: 18px; color: var(--text);">Are you sure?</h3>
            <p id="confirm-dialog-message" style="margin: 0; color: var(--muted); font-size: 14px; line-height: 1.5;"></p>
            <div style="display: flex; gap: 12px; justify-content: flex-end; margin-top: 8px;">
                <button id="confirm-dialog-no" class="button secondary" style="min-height: 36px; padding: 0 16px; font-size: 14px;">No, keep it</button>
                <button id="confirm-dialog-yes" class="button" style="min-height: 36px; padding: 0 16px; font-size: 14px; background: #ef4444; border-color: #ef4444; color: white;">Yes, delete</button>
            </div>
        </div>
    </dialog>

    <style>
        dialog#confirm-dialog::backdrop {
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(2px);
        }
    </style>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            let isDragging = false;
            let currentResizer = null;
            let startX = 0;
            let startWidth = 0;

            const resizers = document.querySelectorAll('.resizer');
            
            resizers.forEach(resizer => {
                const targetVar = '--' + resizer.dataset.target;
                const shell = resizer.closest('.app-shell, .inbox-shell');
                const savedWidth = localStorage.getItem(targetVar);
                if (savedWidth !== null) {
                    if (shell) shell.style.setProperty(targetVar, savedWidth + 'px');
                    document.documentElement.style.setProperty(targetVar, savedWidth + 'px');
                }

                resizer.addEventListener('mousedown', (e) => {
                    isDragging = true;
                    currentResizer = resizer;
                    startX = e.clientX;
                    
                    const varName = '--' + currentResizer.dataset.target;
                    const shellStyles = getComputedStyle(currentResizer.closest('.app-shell, .inbox-shell'));
                    const rootStyles = getComputedStyle(document.documentElement);
                    
                    let currentWidthStr = currentResizer.closest('.app-shell, .inbox-shell').style.getPropertyValue(varName);
                    if(!currentWidthStr) {
                         currentWidthStr = shellStyles.getPropertyValue(varName) || rootStyles.getPropertyValue(varName);
                    }
                    startWidth = parseInt(currentWidthStr) || parseInt(currentResizer.dataset.default);
                    
                    currentResizer.classList.add('dragging');
                    document.body.style.cursor = 'col-resize';
                    document.body.style.userSelect = 'none';
                });

                resizer.addEventListener('dblclick', (e) => {
                    const targetVar = '--' + resizer.dataset.target;
                    const defaultWidth = resizer.dataset.default;
                    const minWidth = resizer.dataset.min || 0;
                    
                    const shell = resizer.closest('.app-shell, .inbox-shell');
                    let currentWidthStr = shell.style.getPropertyValue(targetVar);
                    if (!currentWidthStr) {
                        currentWidthStr = getComputedStyle(shell).getPropertyValue(targetVar) || getComputedStyle(document.documentElement).getPropertyValue(targetVar);
                    }
                    const currentWidth = parseInt(currentWidthStr) || parseInt(defaultWidth);
                    
                    let newWidth = defaultWidth;
                    if (currentWidth > parseInt(minWidth) + 10) {
                        newWidth = minWidth; // Collapse
                    }
                    
                    if (shell) shell.style.setProperty(targetVar, newWidth + 'px');
                    document.documentElement.style.setProperty(targetVar, newWidth + 'px');
                    localStorage.setItem(targetVar, newWidth);
                    document.cookie = `${resizer.dataset.target}=${newWidth}; path=/; max-age=31536000`;
                });
            });

            document.addEventListener('mousemove', (e) => {
                if (!isDragging) return;
                
                const deltaX = e.clientX - startX;
                const isRightEdge = currentResizer.dataset.edge === 'right';
                const modifier = isRightEdge ? 1 : -1;
                
                let newWidth = startWidth + (deltaX * modifier);
                
                const minWidth = parseInt(currentResizer.dataset.min) || 0;
                const maxWidth = parseInt(currentResizer.dataset.max) || 800;
                
                if (newWidth < minWidth) newWidth = minWidth;
                if (newWidth > maxWidth) newWidth = maxWidth;

                const targetVar = '--' + currentResizer.dataset.target;
                const shell = currentResizer.closest('.app-shell, .inbox-shell');
                if (shell) shell.style.setProperty(targetVar, newWidth + 'px');
                document.documentElement.style.setProperty(targetVar, newWidth + 'px');
            });

            document.addEventListener('mouseup', () => {
                if (isDragging && currentResizer) {
                    const targetVar = '--' + currentResizer.dataset.target;
                    const shell = currentResizer.closest('.app-shell, .inbox-shell');
                    const finalWidth = shell ? shell.style.getPropertyValue(targetVar) : getComputedStyle(document.documentElement).getPropertyValue(targetVar);
                    
                    if(finalWidth) {
                        localStorage.setItem(targetVar, parseInt(finalWidth));
                        document.cookie = `${currentResizer.dataset.target}=${parseInt(finalWidth)}; path=/; max-age=31536000`;
                    }
                    
                    currentResizer.classList.remove('dragging');
                    currentResizer = null;
                    isDragging = false;
                    document.body.style.cursor = '';
                    document.body.style.userSelect = '';
                }
            });
        });

        // Global Confirmation Dialog Logic
        function confirmAction(message, title, confirmText, cancelText, onConfirm) {
            const dialog = document.getElementById('confirm-dialog');
            document.getElementById('confirm-dialog-title').innerText = title || 'Are you sure?';
            document.getElementById('confirm-dialog-message').innerText = message;
            
            const yesBtn = document.getElementById('confirm-dialog-yes');
            const noBtn = document.getElementById('confirm-dialog-no');
            
            yesBtn.innerText = confirmText || 'Yes, delete';
            noBtn.innerText = cancelText || 'No, keep it';
            
            const newYes = yesBtn.cloneNode(true);
            const newNo = noBtn.cloneNode(true);
            yesBtn.parentNode.replaceChild(newYes, yesBtn);
            noBtn.parentNode.replaceChild(newNo, noBtn);
            
            newNo.addEventListener('click', () => dialog.close());
            newYes.addEventListener('click', () => {
                dialog.close();
                onConfirm();
            });
            
            dialog.showModal();
        }

        document.addEventListener('submit', function(e) {
            const form = e.target;
            if (form.hasAttribute('data-confirm')) {
                e.preventDefault();
                const message = form.getAttribute('data-confirm');
                const title = form.getAttribute('data-confirm-title');
                const confirmBtn = form.getAttribute('data-confirm-btn');
                const cancelBtn = form.getAttribute('data-cancel-btn');
                
                confirmAction(message, title, confirmBtn, cancelBtn, () => {
                    form.removeAttribute('data-confirm');
                    form.submit();
                });
            }
        });

        async function setTheme(theme) {
            localStorage.setItem('userThemePref', theme);
            applyTheme();
            document.getElementById('theme-menu').style.display = 'none';
        }

        function addRainEffect() {
            if (document.querySelector('.rain-overlay')) return;
            const overlay = document.createElement('div');
            overlay.className = 'theme-overlay rain-overlay';
            document.body.appendChild(overlay);

            // Add occasional lightning
            const lightning = document.createElement('div');
            lightning.className = 'theme-overlay lightning-overlay';
            document.body.appendChild(lightning);
        }

        function addCloudEffect() {
            if (document.querySelector('.cloud-overlay')) return;
            const overlay = document.createElement('div');
            overlay.className = 'theme-overlay cloud-overlay';
            document.body.appendChild(overlay);
        }

        function addSunEffect() {
            if (document.querySelector('.sun-overlay')) return;
            const overlay = document.createElement('div');
            overlay.className = 'theme-overlay sun-overlay';
            document.body.appendChild(overlay);
        }

        async function fetchLiveWeather() {
            try {
                let weatherClass = sessionStorage.getItem('weatherClass');
                if (!weatherClass) {
                    const locRes = await fetch('https://ipapi.co/json/');
                    if (!locRes.ok) throw new Error('Location fetch failed');
                    const locData = await locRes.json();
                    if(!locData.latitude || !locData.longitude) return;

                    const weatherRes = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${locData.latitude}&longitude=${locData.longitude}&current_weather=true`);
                    if (!weatherRes.ok) throw new Error('Weather fetch failed');
                    const weatherData = await weatherRes.json();
                    const code = weatherData.current_weather.weathercode;
                    const isDay = weatherData.current_weather.is_day;

                    if (isDay === 0) weatherClass = 'weather-night';
                    else if (code <= 3) weatherClass = (code === 0) ? 'weather-sunny' : 'weather-cloudy';
                    else if (code >= 51 && code <= 67) weatherClass = 'weather-rainy';
                    else if ((code >= 71 && code <= 77) || code === 85 || code === 86) weatherClass = 'weather-snowy';
                    else if (code >= 80 && code <= 82) weatherClass = 'weather-rainy';
                    else if (code >= 95) weatherClass = 'weather-rainy';
                    else weatherClass = 'weather-cloudy';

                    sessionStorage.setItem('weatherClass', weatherClass);
                }

                if (weatherClass && weatherClass !== 'weather-default') {
                    document.body.classList.add(weatherClass);
                    if (weatherClass === 'weather-rainy') { addCloudEffect(); addRainEffect(); }
                    else if (weatherClass === 'weather-cloudy' || weatherClass === 'weather-snowy') addCloudEffect();
                    else if (weatherClass === 'weather-sunny') addSunEffect();
                }
            } catch (err) {
                console.error('Could not load weather data for dynamic background:', err);
                sessionStorage.setItem('weatherClass', 'weather-default');
            }
        }

        async function applyTheme() {
            // Remove existing classes & overlays
            document.body.className = document.body.className.replace(/weather-\S+/g, '').trim();
            document.querySelectorAll('.theme-overlay').forEach(e => e.remove());

            const theme = localStorage.getItem('userThemePref') || 'live';

            if (theme === 'live') {
                await fetchLiveWeather();
            } else if (theme === 'weather-default') {
                document.body.classList.add('weather-default');
            } else {
                document.body.classList.add(theme);
                if (theme === 'weather-rainy') { addCloudEffect(); addRainEffect(); }
                else if (theme === 'weather-cloudy' || theme === 'weather-snowy') addCloudEffect();
                else if (theme === 'weather-sunny') addSunEffect();
            }
        }
        
        applyTheme();
    </script>
</body>

</html>