* {
    box-sizing: border-box;
}

:root {
    --bg-app: #f8fafc;
    --bg-panel: #ffffff;
    --bg-hover: #f1f5f9;
    --bg-active: #fef4e8;
    --border: #e2e8f0;
    --border-focus: #fbd3a5;
    --text-main: #1e293b;
    --text-title: #0f172a;
    --text-muted: #64748b;
    --text-light: #475569;
    --primary: #f7971d;
    --primary-hover: #d67d0f;
    --primary-light: #f9ab45;
    --primary-text: #fff;
    --danger-bg: #fee2e2;
    --danger-text: #b91c1c;
    --danger-hover: #fecaca;
    --overlay: rgba(15, 23, 42, 0.5);
    --shadow-color: rgba(0,0,0,0.1);
}

:root.theme-dark {
    --bg-app: #202124;
    --bg-panel: #292a2d;
    --bg-hover: #3c4043;
    --bg-active: rgba(247, 151, 29, 0.15);
    --border: #3c4043;
    --border-focus: #5f6368;
    --text-main: #e8eaed;
    --text-title: #ffffff;
    --text-muted: #9aa0a6;
    --text-light: #bdc1c6;
    --primary-text: #111;
    --danger-bg: #5c1e1e;
    --danger-text: #fecaca;
    --danger-hover: #7f2222;
    --overlay: rgba(0, 0, 0, 0.7);
    --shadow-color: rgba(0,0,0,0.3);
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-app);
    color: var(--text-main);
}

.theme-transition * {
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease !important;
}

/* Animations */
@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
}

.swal-loader-card {
    background: var(--bg-panel) !important;
    color: var(--text-title) !important;
    border: 1px solid var(--border) !important;
    border-radius: 20px !important;
    box-shadow: 0 20px 50px var(--shadow-color) !important;
}

.swal-loader-html {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 8px 0 4px;
}

.swal-loader-text {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-title);
}

.loader {
    width: 16px;
    height: 16px;
    position: relative;
    left: -32px;
    border-radius: 50%;
    color: var(--text-title);
    background: currentColor;
    box-shadow: 32px 0, -32px 0, 64px 0;
}

.loader::after {
    content: '';
    position: absolute;
    left: -32px;
    top: 0;
    width: 16px;
    height: 16px;
    border-radius: 10px;
    background: var(--primary);
    animation: move 3s linear infinite alternate;
}

@keyframes move {
    0%, 5% {
        left: -32px;
        width: 16px;
    }
    15%, 20% {
        left: -32px;
        width: 48px;
    }
    30%, 35% {
        left: 0;
        width: 16px;
    }
    45%, 50% {
        left: 0;
        width: 48px;
    }
    60%, 65% {
        left: 32px;
        width: 16px;
    }
    75%, 80% {
        left: 32px;
        width: 48px;
    }
    95%, 100% {
        left: 64px;
        width: 16px;
    }
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-app);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: var(--primary);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--primary-hover);
}

* {
    scrollbar-width: thin;
    scrollbar-color: var(--primary) var(--bg-app);
}

.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--border);
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
}

button, input, select {
    font: inherit;
}

/* Layout */
.app-shell {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.sidebar-main {
    width: 64px;
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 16px 0;
    z-index: 10;
    transition: width 0.3s ease;
    overflow: hidden;
}

.sidebar-main.expanded {
    width: 200px;
    align-items: flex-start;
    padding: 16px;
}

.brand-icon {
    margin-bottom: 24px;
    width: 100%;
    display: flex;
    justify-content: center;
    overflow: hidden;
}

.sidebar-main.expanded .brand-icon {
    justify-content: flex-start;
    padding-left: 8px;
}

.brand-icon img {
    max-height: 40px;
    max-width: 100%;
    object-fit: contain;
    transition: all 0.3s ease;
    filter: invert(1);
}

:root.theme-dark .brand-icon img {
    filter: invert(0);
}
.brand-icon .logo-expanded {
    display: none;
}
.brand-icon .logo-minimized {
    display: block;
}
.sidebar-main.expanded .brand-icon .logo-expanded {
    display: block;
}
.sidebar-main.expanded .brand-icon .logo-minimized {
    display: none;
}
/* Hide text fallback if image is present */
.brand-icon img ~ .fallback-logo {
    display: none;
}

.brand-badge {
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    background: var(--primary);
    color: var(--primary-text);
    font-weight: bold;
    font-size: 14px;
    flex-shrink: 0;
}

.nav-main, .nav-bottom {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
    align-items: center;
}

.sidebar-main.expanded .nav-main,
.sidebar-main.expanded .nav-bottom {
    align-items: stretch;
}

.nav-bottom {
    margin-top: auto;
}

.nav-item {
    background: transparent;
    border: none;
    color: var(--text-muted);
    width: 44px;
    height: 44px;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    overflow: hidden;
    padding: 0;
}

.sidebar-main.expanded .nav-item {
    width: 100%;
    justify-content: flex-start;
    padding: 0 10px;
}

.nav-item svg {
    flex-shrink: 0;
}

.nav-label {
    display: none;
    margin-left: 12px;
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
}

.sidebar-main.expanded .nav-label {
    display: inline-block;
}

.nav-item:hover {
    background: var(--bg-hover);
    color: var(--text-title);
}

.nav-item.active {
    background: var(--bg-active);
    color: var(--primary);
}

.sidebar-sub {
    width: 260px;
    background: var(--bg-app);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.sidebar-sub:has(#sub-inbox.active) {
    display: none;
}

.sub-view {
    display: none;
    flex-direction: column;
    height: 100%;
}

.sub-view.active {
    display: flex;
}

.sub-view h2 {
    padding: 20px 16px 10px;
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--text-title);
}

.main-content {
    flex: 1;
    background: var(--bg-panel);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.top-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    height: 64px;
}

.search-bar-container {
    flex: 1;
    max-width: 720px;
    margin-right: 24px;
    position: relative;
}

.global-search {
    background: var(--bg-hover);
    color: var(--text-main);
    border: none;
    border-radius: 24px;
    padding: 12px 20px 12px 44px;
    font-size: 15px;
    width: 100%;
}

.search-icon {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
}

.global-search:focus {
    background: var(--bg-panel);
    border: 1px solid var(--border-focus);
    box-shadow: 0 1px 3px var(--shadow-color);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 16px;
}

.view-content {
    display: none;
    padding: 24px 32px;
    flex: 1;
    overflow-y: auto;
}

.view-content.active {
    display: block;
}

.view-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.view-header h2 {
    margin: 0;
    font-size: 20px;
    color: var(--text-title);
}

/* Panels & Cards */
.panel {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px var(--shadow-color);
}

.content-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 20px;
}

.profile-switch {
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-main.expanded .profile-switch {
    justify-content: flex-start;
    padding-left: 0;
}

.profile-btn {
    background: transparent;
    border: none;
    padding: 0;
    cursor: pointer;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    overflow: hidden;
    transition: transform 0.2s;
}

.profile-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 0 0 2px var(--primary);
}

.profile-avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.profile-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 400px;
    overflow-y: auto;
}

.profile-list-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-app);
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
}

.profile-list-item:hover {
    background: var(--bg-hover);
    border-color: var(--border-focus);
}

.profile-list-item.active {
    background: var(--bg-active);
    border-color: var(--border-focus);
}

.profile-list-item img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
}

.profile-info {
    display: flex;
    flex-direction: column;
}

.profile-info strong {
    font-size: 14px;
    color: var(--text-title);
}

.profile-info span {
    font-size: 12px;
    color: var(--text-muted);
}

/* Forms */
.stack-form {
    display: grid;
    gap: 16px;
}

.field-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 16px;
}

label {
    display: grid;
    gap: 6px;
}

label span {
    color: var(--text-light);
    font-size: 13px;
    font-weight: 500;
}

input, select {
    width: 100%;
    border: 1px solid var(--border-focus);
    border-radius: 8px;
    background: var(--bg-panel);
    color: var(--text-title);
    padding: 10px 12px;
    outline: none;
    transition: border-color 0.2s;
}

input:focus, select:focus {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(247, 151, 29, 0.1);
}

/* Buttons */
.btn {
    border: 0;
    border-radius: 8px;
    padding: 10px 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-primary {
    background: var(--primary);
    color: var(--primary-text);
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--bg-hover);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

.btn-danger {
    background: var(--danger-bg);
    color: var(--danger-text);
}

.btn-danger:hover {
    background: var(--danger-hover);
}

/* Tables */
.table-wrap {
    overflow: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    text-align: left;
    padding: 12px 16px;
    border-bottom: 1px solid var(--bg-hover);
}

th {
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Inbox Sub Sidebar */
.saved-inbox-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 16px;
}

.saved-inbox-item {
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: border-color 0.2s;
}

.saved-inbox-item:hover {
    border-color: var(--text-muted);
}

.saved-inbox-item strong {
    font-size: 14px;
    color: var(--text-title);
    word-break: break-all;
}

.saved-inbox-item small {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 20px;
}

.modal-overlay.hidden {
    display: none;
}

.modal-content {
    width: 100%;
    max-width: 500px;
    background: var(--bg-panel);
    box-shadow: 0 20px 25px -5px var(--shadow-color), 0 10px 10px -5px var(--shadow-color);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-header h3 {
    margin: 0;
    font-size: 18px;
}

.btn-close {
    background: transparent;
    border: none;
    font-size: 24px;
    line-height: 1;
    color: var(--text-muted);
    cursor: pointer;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 24px;
}

/* Inbox Layout inside View */
.inbox-layout {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 20px;
    height: calc(100vh - 160px);
}

.message-list {
    overflow-y: auto;
    padding: 0;
}

.message-item {
    width: 100%;
    padding: 16px;
    border: none;
    border-bottom: 1px solid var(--bg-hover);
    background: transparent;
    text-align: left;
    cursor: pointer;
    border-left: 3px solid transparent;
}

.message-item:hover {
    background: var(--bg-app);
}

.message-item.active {
    background: var(--bg-active);
    border-left-color: var(--primary);
}

.message-item strong {
    display: block;
    color: var(--text-title);
    font-size: 14px;
    margin-bottom: 4px;
}

.message-item span {
    display: block;
    color: var(--text-muted);
    font-size: 12px;
}

.message-detail {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 24px;
}

.empty-state {
    color: var(--text-muted);
    font-size: 14px;
    padding: 20px;
    text-align: center;
}

/* Toast */
.toast {
    position: fixed;
    right: 24px;
    bottom: 24px;
    max-width: 360px;
    padding: 14px 16px;
    border-radius: 10px;
    background: var(--text-main);
    color: var(--bg-panel);
    box-shadow: 0 10px 15px -3px var(--shadow-color);
    z-index: 100;
}

.toast.hidden {
    display: none;
}

.sub-stats {
    display: flex;
    gap: 12px;
    padding: 0 16px 16px;
}

.sub-stat {
    display: flex;
    flex-direction: column;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    flex: 1;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.sub-stat span {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.sub-stat strong {
    font-size: 22px;
    color: var(--primary);
}

/* SweetAlert2 Theme Overrides */
.swal2-popup {
    background: var(--bg-panel) !important;
    color: var(--text-main) !important;
}
.swal2-title, .swal2-html-container {
    color: var(--text-main) !important;
}
.swal2-input {
    background: var(--bg-app) !important;
    color: var(--text-title) !important;
    border-color: var(--border) !important;
}
.swal2-confirm {
    background-color: var(--primary) !important;
    color: var(--primary-text) !important;
}
.swal2-confirm:hover {
    background-color: var(--primary-hover) !important;
}

/* Compose Formatting Buttons */
.format-btn {
    background: transparent;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}
.format-btn:hover {
    background: var(--border);
    color: var(--text-title);
}

.settings-nav {
    list-style: none;
    padding: 0;
    margin: 0;
}

.settings-nav li {
    padding: 12px 16px;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
}

.settings-nav li.active {
    background: var(--bg-active);
    color: var(--primary);
    font-weight: 500;
    border-right: 3px solid var(--primary);
}

/* Responsive Design */
@media (max-width: 1024px) {
    .inbox-layout {
        grid-template-columns: 280px 1fr;
    }
}

@media (max-width: 768px) {
    body, html {
        overflow-x: hidden;
        max-width: 100vw;
    }

    .app-shell {
        flex-direction: column;
        overflow-x: hidden;
        max-width: 100vw;
    }
    
    .sidebar-main {
        width: 100%;
        height: 60px;
        flex-direction: row;
        border-right: none;
        border-top: 1px solid var(--border);
        border-bottom: none;
        padding: 0 16px;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 100;
        justify-content: space-around;
        transition: none;
    }
    
    .sidebar-main.expanded {
        width: 100%;
        height: 60px;
        padding: 0 16px;
    }

    .brand-icon {
        display: none;
    }

    .nav-main {
        flex-direction: row;
        width: 100%;
        gap: 8px;
        justify-content: space-around;
    }

    .nav-item {
        width: 48px;
        height: 48px;
    }

    .nav-bottom {
        display: none;
    }

    .sidebar-sub {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    
    .sidebar-sub:has(#sub-inbox.active) {
        display: flex;
    }

    .inbox-layout {
        grid-template-columns: 1fr;
        height: calc(100vh - 200px);
    }
    
    .message-detail {
        display: none;
    }

    .inbox-layout.mobile-detail-open .message-list-container {
        display: none !important;
    }
    
    .inbox-layout.mobile-detail-open .message-detail {
        display: flex;
    }

    .btn-mobile-back {
        display: flex !important;
    }

    .content-grid {
        grid-template-columns: 1fr;
    }

    .compose-panel {
        width: 100% !important;
        right: 0 !important;
        bottom: 60px !important;
        height: calc(100vh - 60px) !important;
        border-radius: 12px 12px 0 0;
    }
    
    .view-content {
        padding: 16px;
        margin-bottom: 60px;
    }

    .top-header {
        padding: 12px 16px;
    }

    .page-header {
        padding: 16px 16px 0 16px !important;
        flex-direction: column;
        align-items: flex-start !important;
        gap: 12px;
    }

    .page-header > div:last-child {
        width: 100%;
        justify-content: space-between !important;
    }

    .search-bar-container {
        margin-right: 0;
        max-width: 100%;
    }
}
