:root {
    --bg-body: #f8f9fa;
    --bg-card: #ffffff;
    --text-main: #212529;
    --text-muted: #6c757d;
    --primary-green: #00a25b;
    /* Exact Dhan Green */
    --primary-hover: #008f50;
    --danger-red: #ff3b30;
    /* Kept for Panic button only */
    --loss-color: #8b8b8b;
    /* Soft color for losses */
    --border-color: #e9ecef;
    --font-family: 'Inter', system-ui, -apple-system, sans-serif;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body.light-theme {
    font-family: var(--font-family);
    background-color: var(--bg-body);
    color: var(--text-main);
    overflow-x: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* --- Top Navigation --- */
.top-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: var(--bg-card);
    height: 64px;
    padding: 0 24px;
    border-bottom: 1px solid var(--border-color);
}

.nav-left,
.nav-center,
.nav-right {
    display: flex;
    align-items: center;
}

.logo-container {

    margin-right: 4px;
    display: flex;
    align-items: center;
    height: 70px;
}

.logo-container img {
    height: 60px;
    width: auto;
    object-fit: contain;
}

.logo-container-large {
    margin-bottom: 24px;
    display: flex;
    justify-content: center;
    width: 100%;
}

.logo-container-large img {
    height: 135px;
    width: auto;
    max-width: 100%;
    object-fit: contain;
}

.index-ticker {
    display: flex;
    gap: 8px;
    font-size: 14px;
}

.idx-name {
    font-weight: 500;
}

.idx-val {
    font-weight: 600;
}

.status-ok {
    color: var(--primary-green);
}

.nav-links {
    display: flex;
    list-style: none;
    gap: 32px;
}

.nav-links li {
    font-size: 15px;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    padding: 22px 0;
    position: relative;
    transition: color 0.2s;
}

.nav-links li:hover {
    color: var(--primary-green);
}

.nav-links li.active {
    color: var(--primary-green);
}

.nav-links li.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px;
    background-color: var(--primary-green);
    border-radius: 3px 3px 0 0;
}

.user-profile {
    font-size: 14px;
    font-weight: 500;
    margin-right: 16px;
}

.btn-logout {
    background: transparent;
    border: 1px solid var(--border-color);
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    transition: all 0.2s;
}

.btn-logout:hover {
    background: var(--border-color);
    color: var(--text-main);
}

/* --- Bot Master Toggle --- */
.bot-toggle-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-right: 20px;
    padding: 6px 14px;
    border-radius: 8px;
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    cursor: pointer;
    user-select: none;
    transition: border-color 0.3s;
}

.bot-toggle-wrap.is-on {
    border-color: var(--primary-green);
    background: rgba(0, 162, 91, 0.06);
}

.bot-toggle-label {
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    transition: color 0.3s;
    min-width: 50px;
    text-align: right;
}

.bot-toggle-wrap.is-on .bot-toggle-label {
    color: var(--primary-green);
}

.bot-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    cursor: pointer;
}

.bot-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.bot-slider {
    position: absolute;
    inset: 0;
    background-color: #ccd0d5;
    border-radius: 24px;
    transition: background-color 0.3s;
}

.bot-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
    transition: transform 0.3s cubic-bezier(.4, 0, .2, 1);
}

.bot-switch input:checked+.bot-slider {
    background-color: var(--primary-green);
}

.bot-switch input:checked+.bot-slider::before {
    transform: translateX(20px);
}

/* --- Secondary Ticker Strip --- */
.ticker-strip {
    background-color: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    padding: 8px 24px;
    display: flex;
    gap: 24px;
    font-size: 13px;
    color: var(--text-muted);
}

.ticker-item span {
    font-weight: 600;
    color: var(--text-main);
}

/* --- Main Layout Grid --- */
.main-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Left Watchlist Panel */
.left-panel {
    width: 320px;
    flex-shrink: 0;
    background-color: var(--bg-body);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: relative;
    z-index: 10;
}

.search-box {
    padding: 16px;
    background: var(--bg-card);
}

.search-box input {
    width: 100%;
    padding: 10px 16px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border 0.2s;
}

.search-box input:focus {
    border-color: var(--primary-green);
}

.panel-header {
    padding: 12px 16px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    border-top: 1px solid var(--border-color);
}

.watchlist-items {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-card);
}

.wl-item {
    display: flex;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
}

.wl-item:hover {
    background-color: var(--bg-body);
}

.wl-name {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
}

.wl-exchange {
    font-size: 11px;
    color: var(--text-muted);
}

.source-tag {
    font-size: 9px;
    padding: 1px 5px;
    border-radius: 4px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    display: inline-block;
}
.source-manual {
    background: rgba(155, 89, 182, 0.12);
    color: #9b59b6;
    border: 1px solid rgba(155, 89, 182, 0.2);
}
.source-chartink {
    background: rgba(52, 152, 219, 0.12);
    color: #3498db;
    border: 1px solid rgba(52, 152, 219, 0.2);
}
.wl-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
}


.wl-right {
    text-align: right;
}

.wl-price {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 4px;
}

.wl-change {
    font-size: 12px;
}

.green {
    color: var(--primary-green);
}

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

.loss-soft {
    color: var(--loss-color);
}

/* Content Panel */
.content-panel {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    background-color: var(--bg-body);
}

.view {
    display: none;
}

.view.active-view {
    display: block;
    animation: fadeIn 0.3s;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Cards */
.card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 24px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-sm);
}

.card-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-main);
}

/* Portfolio Card */
.portfolio-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.portfolio-header h2 {
    font-size: 18px;
    font-weight: 500;
}

.portfolio-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 24px;
}

.stat-box {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stat-label {
    font-size: 13px;
    color: var(--text-muted);
}

.stat-val {
    font-size: 18px;
    font-weight: 500;
}

/* Buttons */
.btn {
    padding: 8px 16px;
    border-radius: 4px;
    border: none;
    font-weight: 500;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.2s;
}

.btn-primary {
    background-color: var(--primary-green);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
}

.btn-danger {
    background-color: var(--danger-red);
    color: white;
}

.btn-danger:hover {
    opacity: 0.9;
}

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

.btn-outline {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

.btn-outline:hover {
    background: var(--bg-body);
}

.w-100 {
    width: 100%;
}

/* Table */
.table-responsive {
    overflow-x: auto;
}

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

.data-table th {
    text-align: left;
    padding: 12px;
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
    border-bottom: 1px solid var(--border-color);
}

.data-table td {
    padding: 16px 12px;
    font-size: 14px;
    border-bottom: 1px solid var(--border-color);
}

/* Login Overlay */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(248, 249, 250, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.login-card {
    background-color: var(--bg-card);
    padding: 40px;
    border-radius: 12px;
    width: 100%;
    max-width: 360px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-md);
    text-align: center;
}

.logo-circle.large {
    width: 64px;
    height: 64px;
    font-size: 24px;
    margin: 0 auto 24px auto;
}

.login-card h2 {
    margin-bottom: 24px;
    font-size: 20px;
    font-weight: 600;
}

.form-group {
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
}

.form-label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-main);
    margin-bottom: 6px;
}

.form-text-muted {
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 6px;
    line-height: 1.3;
}

.form-control,
.form-select {
    width: 100%;
    padding: 8px 12px;
    font-size: 13px;
    color: var(--text-main);
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    outline: none;
}

.form-control:focus,
.form-select:focus {
    border-color: var(--primary-green);
    box-shadow: 0 0 0 3px rgba(0, 162, 91, 0.1);
}

.settings-card-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

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

/* Custom Checkbox as Switch */
.custom-switch-wrap {
    display: flex;
    align-items: center;
    gap: 12px;
}

.custom-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
}

.custom-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.custom-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccd0d5;
    transition: .3s;
    border-radius: 20px;
}

.custom-slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.custom-switch input:checked+.custom-slider {
    background-color: var(--primary-green);
}

.custom-switch input:checked+.custom-slider:before {
    transform: translateX(16px);
}

/* Override global form-group input for backward compatibility where classes aren't used yet */
.form-group input:not(.form-control):not([type="checkbox"]) {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
    outline: none;
    transition: border 0.2s, box-shadow 0.2s;
}

.form-group input:not(.form-control):not([type="checkbox"]):focus {
    border-color: var(--primary-green);
    box-shadow: 0 0 0 3px rgba(0, 162, 91, 0.1);
}

/* Spinner for buttons */
.spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(255, 255, 255, .3);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 1s ease-in-out infinite;
    margin-right: 8px;
}

.btn-outline .spinner {
    border: 2px solid rgba(0, 0, 0, .1);
    border-top-color: currentColor;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}


/* --- Responsive Media Queries --- */
@media (max-width: 992px) {
    .nav-center {
        display: none; /* Hide desktop nav */
    }

    .nav-center.mobile-active {
        display: flex;
        position: fixed;
        top: 64px;
        left: 0;
        right: 0;
        bottom: 0;
        background: white;
        z-index: 1000;
        flex-direction: column;
        padding: 40px 20px;
        animation: slideInDown 0.3s ease;
    }

    .nav-links {
        flex-direction: column;
        width: 100%;
        gap: 0;
    }

    .nav-links li {
        width: 100%;
        padding: 20px 0;
        border-bottom: 1px solid var(--border-color);
        font-size: 18px;
    }

    .nav-links li.active::after {
        display: none;
    }

    .mobile-menu-btn {
        display: flex;
        flex-direction: column;
        gap: 5px;
        background: none;
        border: none;
        cursor: pointer;
        padding: 10px;
        margin-right: 15px;
    }

    .mobile-menu-btn span {
        width: 25px;
        height: 2px;
        background: var(--text-main);
        transition: 0.3s;
    }

    .mobile-menu-btn.active span:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); }
    .mobile-menu-btn.active span:nth-child(2) { opacity: 0; }
    .mobile-menu-btn.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }
}

@media (max-width: 768px) {
    .main-layout {
        flex-direction: column;
    }

    .left-panel {
        width: 100%;
        height: 250px; /* Fixed compact height */
        flex-shrink: 0;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .portfolio-stats {
        grid-template-columns: 1fr 1fr;
    }

    .content-panel {
        padding: 16px;
    }

    .ticker-strip {
        padding: 10px 16px;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }

    /* Mobile DataTables Card Layout */
    .dataTables_wrapper .dataTables_filter {
        float: none !important;
        text-align: left !important;
        margin-bottom: 15px !important;
    }
    .dataTables_wrapper .dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
        margin-top: 8px !important;
        padding: 10px !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 6px !important;
        box-sizing: border-box !important;
    }
    
    table.dataTable.nowrap th, table.dataTable.nowrap td {
        white-space: normal !important;
    }
    table.data-table, .data-table thead, .data-table tbody, .data-table th, .data-table td, .data-table tr {
        display: block !important;
        width: 100% !important;
    }
    .data-table thead tr {
        position: absolute !important;
        top: -9999px !important;
        left: -9999px !important;
    }
    .data-table tr {
        border: 1px solid var(--border-color) !important;
        margin-bottom: 15px !important;
        border-radius: 8px !important;
        padding: 10px !important;
        background: var(--bg-card) !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.02) !important;
    }
    .data-table td {
        border: none !important;
        border-bottom: 1px solid var(--border-color) !important;
        position: relative !important;
        padding-left: 50% !important;
        text-align: right !important;
        min-height: 40px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: flex-end !important;
    }
    .data-table td:last-child {
        border-bottom: 0 !important;
    }
    
    /* Ensure DataTables pagination and info stack nicely */
    .dataTables_wrapper .dataTables_info, .dataTables_wrapper .dataTables_paginate {
        float: none !important;
        text-align: center !important;
        margin-top: 10px !important;
    }

    /* Pseudo-element labels for mobile cards */
    .data-table td:before {
        position: absolute;
        left: 10px;
        width: 45%;
        padding-right: 10px;
        white-space: nowrap;
        text-align: left;
        font-weight: 600;
        color: var(--text-muted);
        font-size: 13px;
        display: flex;
        align-items: center;
        height: 100%;
        top: 0;
    }

    /* Live Positions / Orders Table */
    #ordersTable td:nth-child(1):before { content: "Account"; }
    #ordersTable td:nth-child(2):before { content: "Type"; }
    #ordersTable td:nth-child(3):before { content: "Status"; }
    #ordersTable td:nth-child(4):before { content: "Filled/Total"; }
    #ordersTable td:nth-child(5):before { content: "Entry Price"; }
    #ordersTable td:nth-child(6):before { content: "Avg Fill"; }
    #ordersTable td:nth-child(7):before { content: "PnL"; }
    #ordersTable td:nth-child(8):before { content: "Reason"; }
    #ordersTable td:nth-child(9):before { content: "Date"; }
    #ordersTable td:nth-child(10):before { content: "Time"; }

    /* Closed Trades Table */
    #closedOrdersTable td:nth-child(1):before { content: "Account"; }
    #closedOrdersTable td:nth-child(2):before { content: "Type"; }
    #closedOrdersTable td:nth-child(3):before { content: "Status"; }
    #closedOrdersTable td:nth-child(4):before { content: "Filled/Total"; }
    #closedOrdersTable td:nth-child(5):before { content: "Target Price"; }
    #closedOrdersTable td:nth-child(6):before { content: "Actual Fill"; }
    #closedOrdersTable td:nth-child(7):before { content: "PnL"; }
    #closedOrdersTable td:nth-child(8):before { content: "Reason"; }
    #closedOrdersTable td:nth-child(9):before { content: "Date"; }
    #closedOrdersTable td:nth-child(10):before { content: "Time"; }

    /* Accounts Table */
    #accountsTable td:nth-child(1):before { content: "Alias"; }
    #accountsTable td:nth-child(2):before { content: "Client ID"; }
    #accountsTable td:nth-child(3):before { content: "Active"; }
    #accountsTable td:nth-child(4):before { content: "Broker Status"; }
    #accountsTable td:nth-child(5):before { content: "Live Qty"; }
    #accountsTable td:nth-child(6):before { content: "M2M PnL"; }
    #accountsTable td:nth-child(7):before { content: "Daily Trades"; }
    #accountsTable td:nth-child(8):before { content: "Token Expires"; }
    #accountsTable td:nth-child(9):before { content: "Actions"; }
}

@media (max-width: 480px) {
    .portfolio-stats {
        grid-template-columns: 1fr;
    }

    .top-nav {
        padding: 0 16px;
    }

    .logo-container img {
        height: 40px;
    }

    .user-profile {
        display: none;
    }

    .card {
        padding: 20px 16px;
    }

    /* Force linear forms and override inline grid styles */
    form div, .form-group, .grid-2, .grid-3, [style*="grid-template-columns"] {
        display: block !important;
        grid-template-columns: 1fr !important;
        width: 100% !important;
    }

    form label {
        display: block;
        margin-bottom: 8px;
    }

    form input, form select, form textarea {
        margin-bottom: 16px;
    }
    
    /* Fix for flex-based inline styles */
    [style*="display: flex"] {
        flex-direction: column !important;
        align-items: flex-start !important;
    }
    
    /* Exceptions for small icons/buttons that should stay horizontal */
    .bot-toggle-wrap, .nav-left, .nav-right, .logo-container, .ticker-strip {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
    }
}

@keyframes slideInDown {
    from { transform: translateY(-10%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}