:root {
    --bg: #0f172a; --card: #1e293b; --text: #f8fafc; --muted: #94a3b8;
    --accent: #3b82f6; --accent-hover: #2563eb;
    --success: #10b981; --danger: #ef4444; --error: #ef4444;
    --border: #334155;
}

/* Light theme */
:root[data-theme="light"] {
    --bg: #f8fafc; --card: #ffffff; --text: #1e293b; --muted: #64748b;
    --accent: #3b82f6; --accent-hover: #2563eb;
    --success: #10b981; --danger: #ef4444; --error: #ef4444;
    --border: #e2e8f0;
}

*, *::before, *::after { box-sizing: border-box; }

body {
    background: var(--bg); color: var(--text);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 0;
}

a { text-decoration: none; color: inherit; transition: 0.2s; }

/* Login Page Specifics */
body.login-page {
    display: flex; justify-content: center; align-items: center; height: 100vh;
}
.login-card {
    background: var(--card); padding: 2rem; border-radius: 1rem; border: 1px solid #334155;
    width: 100%; max-width: 320px; text-align: center;
    box-shadow: 0 10px 25px -5px rgba(0,0,0,0.3);
}

/* Dashboard Specifics */
body.dashboard { padding: 20px; font-size: 14px; }

.container { max-width: 1200px; margin: 0 auto; width: 100%; }

/* Header */
.header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid var(--border); }
.logo { font-size: 1.4rem; font-weight: 800; color: var(--accent); letter-spacing: -0.5px; display: flex; align-items: center; gap: 10px; }
.nav { display: flex; gap: 5px; background: var(--card); padding: 4px; border-radius: 10px; border: 1px solid var(--border); }
.nav a { color: var(--muted); padding: 8px 16px; border-radius: 8px; font-weight: 500; }
.nav a.active { background: var(--bg); color: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.2); }
.nav a:hover:not(.active) { color: #fff; }

/* Cards & Toolbar */
.card { background: var(--card); border-radius: 16px; border: 1px solid var(--border); overflow: hidden; margin-bottom: 20px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); }
.toolbar { padding: 15px 20px; display: flex; gap: 15px; border-bottom: 1px solid var(--border); align-items: center; justify-content: space-between; flex-wrap: wrap; background: rgba(255,255,255,0.02); }
.toolbar h3 { margin: 0; font-size: 1.1rem; font-weight: 600; }

/* Inputs & Buttons */
input[type=text], input[type=password] { background: var(--bg); border: 1px solid var(--border); color: #fff; padding: 10px 12px; border-radius: 8px; outline: none; transition: 0.2s; }
input:focus { border-color: var(--accent); }
/* Specific fix for login input margin */
.login-card input { width: 100%; margin: 15px 0; border: 1px solid #475569; }

button, .btn { padding: 9px 18px; border-radius: 8px; border: none; cursor: pointer; font-weight: 600; font-size: 0.9rem; display: inline-flex; align-items: center; gap: 6px; color: #fff; background: var(--accent); transition: 0.2s; white-space: nowrap; }
button:hover, .btn:hover { opacity: 0.9; background: var(--accent-hover); }

/* Button Variants */
.login-card button { width: 100%; padding: 12px; }
.btn-sec { background: var(--border); color: var(--muted); } 
.btn-sec:hover { color: #fff; background: var(--bg); }
.btn-danger { background: rgba(239,68,68,0.2); color: #fca5a5; } 
.btn-danger:hover { background: var(--danger); color: #fff; }

/* Table */
.table-wrap { overflow-x: auto; width: 100%; }
table { width: 100%; border-collapse: collapse; white-space: nowrap; }
th { text-align: left; padding: 12px 20px; color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid var(--border); background: rgba(0,0,0,0.1); }
td { padding: 14px 20px; border-bottom: 1px solid var(--bg); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover { background: rgba(255,255,255,0.02); }
tr.disabled { opacity: 0.5; }

/* Status & Badges */
.c-name { font-weight: 600; font-size: 0.95rem; color: #fff; margin-bottom: 3px; }
.c-ip { font-size: 0.8rem; color: var(--muted); font-family: monospace; }
.status { width: 8px; height: 8px; border-radius: 50%; background: var(--border); display: inline-block; }
.status.online { background: var(--success); box-shadow: 0 0 8px var(--success); }
.badge { background: #4f46e5; color: #fff; font-size: 0.65rem; padding: 2px 6px; border-radius: 4px; margin-left: 6px; vertical-align: middle; font-weight: 600; }

/* Flag Icons */
.flag { 
    display: inline-block; 
    width: 20px; 
    height: 14px; 
    background-size: contain; 
    background-repeat: no-repeat; 
    background-position: center;
    vertical-align: middle;
    margin-right: 4px;
    border-radius: 2px;
}
.flag.ru { background-image: url('russia.svg'); border: none; }
.flag.eu { background-image: url('europe.svg'); border: none; }

.stat-main { font-weight: 600; font-size: 0.9rem; color: var(--accent); }
.stat-sub { font-size: 0.75rem; color: var(--muted); display: flex; gap: 8px; margin-top: 2px; }
.stat-row { display: flex; gap: 8px; align-items: center; font-size: 0.85rem; margin-bottom: 3px; }
.stat-val { color: #fff; font-weight: 500; }
.sort-link { display: inline-flex; align-items: center; gap: 4px; }
.sort-link.active { color: var(--accent); font-weight: 700; }

/* Icons */
.icon-btn { padding: 6px; border-radius: 6px; background: transparent; color: var(--muted); border: none; cursor: pointer; transition: 0.2s; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: var(--bg); color: #fff; }
.icon-btn.danger:hover { color: var(--danger); background: rgba(239,68,68,0.1); }
svg { width: 18px; height: 18px; display: block; }

/* Switch */
.list-item { display: flex; align-items: center; justify-content: space-between; padding: 15px 20px; border-bottom: 1px solid var(--border); }
.switch { position: relative; display: inline-block; width: 40px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background: var(--bg); transition: .3s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background: #fff; transition: .3s; border-radius: 50%; }
input:checked + .slider { background: var(--success); }
input:checked + .slider:before { transform: translateX(18px); }

.logs { background: #000; color: #ccc; padding: 15px; font-family: monospace; font-size: 0.85rem; height: 300px; overflow-y: auto; border-top: 1px solid var(--border); white-space: pre-wrap; }

/* Light theme adjustments */
:root[data-theme="light"] .logs {
    background: #1e1e1e;
    color: #d4d4d4;
}

:root[data-theme="light"] .log-content {
    background: #1e1e1e;
    color: #d4d4d4;
}

:root[data-theme="light"] .login-card {
    box-shadow: 0 10px 25px -5px rgba(0,0,0,0.1);
}

:root[data-theme="light"] input[type=text],
:root[data-theme="light"] input[type=password] {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
}

:root[data-theme="light"] .card {
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05);
}

:root[data-theme="light"] tr:hover {
    background: rgba(0,0,0,0.02);
}

/* Fix contrast for active navigation in light theme */
:root[data-theme="light"] .nav a.active {
    background: var(--accent);
    color: #ffffff;
}

:root[data-theme="light"] .nav a {
    color: var(--muted);
}

:root[data-theme="light"] .nav a:hover:not(.active) {
    color: var(--text);
}

/* Fix contrast for client names in light theme */
:root[data-theme="light"] .c-name {
    color: #0f172a;
}

/* Fix contrast for table text in light theme */
:root[data-theme="light"] td {
    color: var(--text);
}

:root[data-theme="light"] th {
    color: var(--muted);
}

/* Fix contrast for history table in light theme */
:root[data-theme="light"] .history-main-table .client-cell {
    color: #0f172a;
}

:root[data-theme="light"] .history-main-table .month-cell {
    color: var(--accent);
}

/* Fix contrast for stat values in light theme */
:root[data-theme="light"] .stat-val {
    color: #0f172a;
}

/* Fix tab navigation in light theme */
:root[data-theme="light"] .tab-nav a.active {
    background: var(--accent);
    color: #ffffff;
}

:root[data-theme="light"] .tab-nav a {
    color: var(--muted);
}

:root[data-theme="light"] .tab-nav a:hover {
    color: var(--text);
}

/* Fix button hover in light theme */
:root[data-theme="light"] .icon-btn {
    color: #64748b;
}

:root[data-theme="light"] .icon-btn:hover {
    background: #f1f5f9;
    color: #0f172a;
}

:root[data-theme="light"] .icon-btn.danger:hover {
    color: var(--danger);
    background: rgba(239,68,68,0.1);
}

:root[data-theme="light"] .btn-sec {
    background: #f1f5f9;
    color: #475569;
}

:root[data-theme="light"] .btn-sec:hover {
    background: #e2e8f0;
    color: #0f172a;
}

:root[data-theme="light"] .btn-danger {
    background: rgba(239,68,68,0.15);
    color: #dc2626;
}

:root[data-theme="light"] .btn-danger:hover {
    background: var(--danger);
    color: #ffffff;
}

:root[data-theme="light"] button:hover,
:root[data-theme="light"] .btn:hover {
    opacity: 1;
}

/* Fix modal content in light theme */
:root[data-theme="light"] .modal-content h3 {
    color: #0f172a;
}

:root[data-theme="light"] .modal-content p {
    color: var(--text);
}

:root[data-theme="light"] .modal-content b {
    color: #0f172a;
}

:root[data-theme="light"] .modal-content label {
    color: var(--text);
}

:root[data-theme="light"] .modal-content label span {
    color: var(--text);
}

/* Fix file input in light theme */
:root[data-theme="light"] input[type="file"] {
    color: var(--text);
}

:root[data-theme="light"] input[type="file"]::file-selector-button {
    background: var(--accent);
    color: #ffffff;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    margin-right: 10px;
}

:root[data-theme="light"] input[type="file"]::file-selector-button:hover {
    background: var(--accent-hover);
}

/* Fix checkbox in light theme */
:root[data-theme="light"] input[type="checkbox"] {
    accent-color: var(--accent);
}

/* Fix alert warning in light theme */
:root[data-theme="light"] .alert.warn {
    background: rgba(234,179,8,0.15);
    border: 1px solid rgba(234,179,8,0.3);
    color: #92400e;
}

/* Modal & Toast */
.modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); z-index: 99; justify-content: center; align-items: center; backdrop-filter: blur(3px); }
.modal-content { background: var(--card); padding: 30px; border-radius: 16px; border: 1px solid var(--border); text-align: center; position: relative; max-width: 90%; }
.toast { position: fixed; bottom: 20px; right: 20px; background: var(--card); border: 1px solid var(--accent); padding: 15px 25px; border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); transform: translateY(100px); transition: 0.3s; opacity: 0; z-index: 200; border-left: 4px solid var(--accent); }
.toast.show { transform: translateY(0); opacity: 1; }

/* Theme Toggle */
.theme-toggle {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: 0.2s;
}
.theme-toggle:hover {
    background: var(--bg);
}
.theme-toggle svg {
    width: 20px;
    height: 20px;
    color: var(--muted);
    transition: 0.2s;
}
.theme-toggle:hover svg {
    color: var(--text);
}

/* Improved Mobile Responsive */
@media(max-width:768px) {
    body.dashboard { padding: 10px; font-size: 13px; }

    .header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
        margin-bottom: 20px;
        padding-bottom: 15px;
    }

    .logo { font-size: 1.2rem; justify-content: center; }

    .nav {
        width: 100%;
        flex-wrap: wrap;
        justify-content: center;
        padding: 3px;
    }
    .nav a {
        padding: 6px 12px;
        font-size: 0.85rem;
        flex: 1 1 auto;
        text-align: center;
    }

    .toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 12px 15px;
    }
    .toolbar h3 { font-size: 1rem; }

    /* Make buttons stack on mobile */
    .toolbar > div, .srv-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        width: 100%;
    }
    .toolbar button, .toolbar .btn {
        width: 100%;
        justify-content: center;
    }

    /* Table improvements for mobile */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    table {
        min-width: 600px;
        font-size: 0.85rem;
    }
    td, th {
        padding: 10px 8px;
        font-size: 0.8rem;
    }

    .hide-m { display: none; }

    /* Card adjustments */
    .card { border-radius: 12px; margin-bottom: 15px; }

    /* Modal adjustments */
    .modal-content {
        max-width: 95%;
        padding: 20px;
    }

    /* Toast adjustments */
    .toast {
        left: 10px;
        right: 10px;
        bottom: 10px;
    }

    /* Service card stacking */
    .srv-head {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    /* Domain list wrapping */
    .domain-list {
        gap: 6px;
    }
    .domain-tag {
        font-size: 0.8rem;
        padding: 5px 10px;
    }

    /* Login card on mobile */
    .login-card {
        max-width: 100%;
        margin: 0 20px;
    }
}

/* Small mobile devices */
@media(max-width:480px) {
    body.dashboard { padding: 8px; }

    .logo { font-size: 1.1rem; }

    .nav a {
        padding: 5px 8px;
        font-size: 0.8rem;
    }

    .card { border-radius: 10px; }

    .toolbar h3 { font-size: 0.95rem; }

    button, .btn {
        padding: 8px 14px;
        font-size: 0.85rem;
    }

    table {
        min-width: 500px;
        font-size: 0.8rem;
    }

    .c-name { font-size: 0.9rem; }
    .c-ip { font-size: 0.75rem; }
}

/* --- NEW STYLES --- */

/* Tab Navigation (Sub-nav) */
.tab-nav { display: flex; gap: 10px; margin-bottom: 20px; border-bottom: 1px solid var(--border); padding-bottom: 10px; }
.tab-nav a { padding: 8px 16px; border-radius: 6px; color: var(--muted); font-weight: 500; font-size: 0.9rem; }
.tab-nav a:hover { background: var(--card); color: #fff; }
.tab-nav a.active { background: var(--accent); color: #fff; }

/* Log Viewer */
.log-container { 
    background: #000000; 
    border-top: 1px solid var(--border); 
    height: 400px; /* Increased height */
    overflow: hidden; 
    position: relative; 
}

/* Button Disabled State */
button:disabled, .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
    filter: grayscale(100%);
}

.log-content { 
    height: 100%; 
    overflow-y: auto; 
    padding: 10px; 
    font-family: 'Consolas', 'Monaco', monospace; 
    font-size: 0.75rem; 
    line-height: 1.3; 
    color: #e2e8f0; 
    white-space: pre-wrap;
    scroll-behavior: smooth;
}
.log-content::-webkit-scrollbar { width: 6px; }
.log-content::-webkit-scrollbar-thumb { background: #334155; border-radius: 3px; }

/* Grids */
.services-grid { display: flex; flex-direction: column; gap: 25px; }
.history-grid { width: 100%; }

/* Service Card Fixes */
.service-card { width: 100%; display: flex; flex-direction: column; }
.service-card .toolbar { background: rgba(255,255,255,0.03); padding: 12px 20px; }
.srv-head { display: flex; align-items: center; gap: 12px; flex: 1; }
.srv-actions { display: flex; gap: 8px; }

/* History Table Styling */
.history-table-container { background: var(--card); border-radius: 16px; border: 1px solid var(--border); overflow: hidden; }
.history-main-table { width: 100%; border-collapse: collapse; }
.history-main-table th { background: rgba(0,0,0,0.2); color: var(--muted); font-size: 0.8rem; padding: 15px 20px; }
.history-main-table td { padding: 15px 20px; border-bottom: 1px solid var(--border); }
.history-main-table tr:hover { background: rgba(255,255,255,0.01); }
.history-main-table .client-cell { font-weight: 600; color: #fff; width: 250px; }
.history-main-table .month-cell { color: var(--accent); font-family: monospace; font-weight: 600; }

/* Alerts */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 15px; font-size: 0.9rem; line-height: 1.4; }
.alert.warn { background: rgba(234,179,8,0.1); border: 1px solid rgba(234,179,8,0.2); color: #fde047; }

/* Domain List */
.domain-list { display: flex; flex-wrap: wrap; gap: 8px; }
.domain-tag { background: var(--bg); padding: 6px 12px; border-radius: 6px; font-size: 0.9rem; border: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
.add-domain-form { display: flex; gap: 10px; margin-bottom: 15px; }