:root {
    --bg-primary: #0a0e1a;
    --bg-secondary: #131825;
    --bg-tertiary: #1a2035;
    --bg-hover: #2a3447;
    --text-primary: #e5e7eb;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --accent-primary: #3b82f6;
    --accent-secondary: #8b5cf6;
    --accent-success: #10b981;
    --accent-warning: #f59e0b;
    --accent-danger: #ef4444;
    --border-color: #374151;
    --transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
    --font-display: 'Outfit', sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    scrollbar-width: thin;
    scrollbar-color: var(--bg-tertiary) var(--bg-primary);
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--bg-tertiary); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--bg-hover); }

body {
    font-family: var(--font-display);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
}

/* ── Layout ────────────────────────────────────────────────── */

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

.header {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 2rem;
    padding-bottom: 2rem;
    border-bottom: 1px solid var(--border-color);
}

h1 {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* ── Back Button ───────────────────────────────────────────── */

.back-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-base);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.back-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    transform: translateX(-2px);
}

.back-btn svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ── Cards ─────────────────────────────────────────────────── */

.card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
}

.card-header {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.card-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(59, 130, 246, 0.12);
    border: 1px solid rgba(59, 130, 246, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    flex-shrink: 0;
    margin-top: 0.25rem;
}

.card-icon-warning {
    background: rgba(245, 158, 11, 0.12);
    border-color: rgba(245, 158, 11, 0.25);
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: var(--accent-primary);
}

.card-title-warning {
    color: var(--accent-warning);
    font-size: 1.1rem;
}

.card-subtitle {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.card-description {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-bottom: 1.5rem;
    line-height: 1.5;
}

.card-help {
    display: inline-block;
    margin-top: 0.5rem;
}

.card-help a {
    color: var(--accent-primary);
    text-decoration: none;
    border-bottom: 1px dotted currentColor;
}

.card-help a:hover {
    text-decoration: underline;
}

/* ── Forms ─────────────────────────────────────────────────── */

.form-group {
    margin-bottom: 1.5rem;
}

.form-label {
    display: block;
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-secondary);
}

.form-input {
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: var(--font-display);
    font-size: 1rem;
    transition: all var(--transition-base);
}

.form-input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 0.5rem;
}

/* ── Buttons ───────────────────────────────────────────────── */

.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-base);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-family: var(--font-display);
    font-size: 0.9rem;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 16px rgba(59, 130, 246, 0.3);
}

.btn-small {
    padding: 0.65rem 1rem;
    font-size: 0.85rem;
    white-space: nowrap;
    flex-shrink: 0;
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-hover);
    border-color: var(--accent-primary);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: var(--accent-danger);
    border: 1px solid var(--accent-danger);
    padding: 0.4rem 0.8rem;
    font-size: 0.75rem
}

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

.btn-warning {
    background: rgba(217, 119, 6, 0.1);
    color: #d97706;
    border: 1px solid #d97706;
    padding: 0.4rem 0.8rem;
    font-size: 0.75rem;
}

.btn-warning:hover {
    background: #d97706;
    color: white;
}

.admin-badge {
    display: inline-block;
    font-size: 0.65rem;
    font-weight: 600;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    background: rgba(217, 119, 6, 0.15);
    color: #d97706;
    border: 1px solid rgba(217, 119, 6, 0.4);
    vertical-align: middle;
    margin-left: 0.4rem;
}

/* ── Tables ────────────────────────────────────────────────── */

.table-wrapper {
    overflow-x: auto;
    margin-bottom: 1.5rem;
}

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

.channel-table th,
.channel-table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.channel-table th {
    font-size: 0.8rem;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    font-weight: 600;
}

.channel-table tbody tr:last-child td {
    border-bottom: none;
}

.channel-url-cell {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: var(--text-muted);
    word-break: break-all;
}

/* ── Add Forms ─────────────────────────────────────────────── */

.add-form-inline {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    flex-wrap: wrap;
}

.add-form-inline .form-group {
    flex: 1;
    min-width: 200px;
    margin-bottom: 0;
}

.add-form-inline .btn-small {
    height: 46px;
}

/* ── Admin Panel ───────────────────────────────────────────── */

.admin-section {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 1.75rem;
}

.admin-section:last-of-type {
    margin-bottom: 0;
}

.admin-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.9rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
    gap: 0.6rem;
}

.admin-section-icon {
    font-size: 1rem;
    flex-shrink: 0;
}

.admin-section-info {
    flex: 1;
}

.admin-section-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
}

.admin-section-subtitle {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.admin-section-count {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}

.admin-section-content {
    padding: 0.5rem 0.75rem;
}

.admin-section-divider {
    height: 1px;
    background: var(--border-color);
    margin: 0 0.75rem;
}

.admin-section-form {
    padding: 1rem 1.25rem;
}

.admin-form-label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    margin-bottom: 0.875rem;
}

.admin-user-form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr auto;
    gap: 0.75rem;
    align-items: end;
}

.admin-user-form-grid .form-group {
    margin-bottom: 0;
}

.admin-form-btn {
    white-space: nowrap;
    height: auto;
}

/* ── User List Items ───────────────────────────────────────── */

.user-list-item {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.65rem 0.875rem;
    margin-bottom: 0.4rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
}

.user-list-item:last-child {
    margin-bottom: 0;
}

.user-info {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    flex: 1;
    min-width: 0;
}

.user-name {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-email {
    font-size: 0.775rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    flex-shrink: 0;
    align-items: center;
    justify-content: flex-end;
}

.user-actions .btn {
    padding: 0.35rem 0.65rem;
    font-size: 0.72rem;
    white-space: nowrap;
}

/* ── Role Popup ─────────────────────────────────────────────── */

.role-popup {
    position: fixed;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 0.35rem;
    z-index: 2000;
    box-shadow: 0 8px 32px rgba(0,0,0,0.45);
    min-width: 185px;
}

.role-popup-header {
    font-size: 0.72rem;
    color: var(--text-muted);
    padding: 0.25rem 0.65rem 0.4rem;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 0.3rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.role-popup-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.45rem 0.7rem;
    border: none;
    background: none;
    border-radius: 7px;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--text-primary);
    text-align: left;
    transition: background 0.12s;
    font-family: inherit;
    white-space: nowrap;
}

.role-popup-option:hover:not(.active) {
    background: var(--bg-tertiary);
}

.role-popup-option.active {
    background: var(--accent-primary);
    color: #fff;
    font-weight: 500;
    cursor: default;
}

/* ── Backup Actions ────────────────────────────────────────── */

.backup-action {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    padding: 1rem 0;
}

.backup-action-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.2rem;
}

.backup-action-subtitle {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.backup-warning {
    color: var(--accent-warning);
    font-weight: 600;
}

.mono {
    font-family: var(--font-mono);
}

.file-picker-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-bottom: 0.875rem;
    margin-top: 0.875rem;
}

#restoreFileName {
    font-size: 0.78rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn-restore {
    display: none;
    margin-top: 0.875rem;
    width: 100%;
    background: rgba(239, 68, 68, 0.08);
    border: 1px solid rgba(239, 68, 68, 0.4);
    color: var(--accent-danger);
    padding: 0.65rem 1rem;
    border-radius: 8px;
    font-family: var(--font-display);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition-base);
}

.btn-restore:hover {
    background: rgba(239, 68, 68, 0.15);
}

/* ── Modal ─────────────────────────────────────────────────── */

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(10, 14, 26, 0.9);
    backdrop-filter: blur(8px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s;
}

.modal.active {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.modal-content {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 2rem;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.modal-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0;
}

.modal-highlight {
    color: var(--accent-primary);
}

.modal-close {
    width: 40px;
    height: 40px;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    transition: all var(--transition-base);
}

.modal-close:hover {
    background: var(--accent-danger);
    color: white;
    transform: rotate(90deg);
}

/* ── Device Assignment List ────────────────────────────────── */

.device-assign-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 400px;
    overflow-y: auto;
}

.device-assign-item {
    background: var(--bg-tertiary);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.device-assign-name {
    font-weight: 600;
}

.device-assign-imei {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

/* ── Switch ────────────────────────────────────────────────── */

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

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

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--text-muted);
    transition: 0.4s;
    border-radius: 20px;
}

.slider:before {
    position: absolute;
    content: '';
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: 0.4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--accent-success);
}

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

/* ── Notify user list ──────────────────────────────────────── */

.notify-user-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.15s;
}

.notify-user-item:hover {
    background: var(--bg-tertiary);
}

.notify-select-all {
    font-weight: 600;
    font-size: 0.875rem;
    border-bottom: 1px solid var(--border-color);
    border-radius: 0;
    padding-bottom: 0.6rem;
    margin-bottom: 0.25rem;
    color: var(--accent-primary);
}

.notify-user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.notify-user-name {
    font-weight: 500;
    font-size: 0.875rem;
}

.notify-user-email {
    font-size: 0.775rem;
    color: var(--text-muted);
}

.notify-user-empty {
    padding: 0.75rem;
    color: var(--text-muted);
    font-size: 0.875rem;
    text-align: center;
}

.notify-selected-count {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--accent-primary);
    background: color-mix(in srgb, var(--accent-primary) 15%, transparent);
    padding: 0.1rem 0.5rem;
    border-radius: 999px;
    margin-left: 0.4rem;
    vertical-align: middle;
}

.notify-modal-content {
    max-width: 480px;
}

.notify-modal-body {
    padding: 1.25rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.notify-modal-users {
    display: flex;
    flex-direction: column;
}

.notify-modal-fields {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

@media (min-width: 640px) {
    .notify-modal-content {
        max-width: 780px;
    }

    .notify-modal-body {
        flex-direction: row;
        align-items: stretch;
        gap: 1.5rem;
    }

    .notify-modal-users {
        flex: 0 0 280px;
        max-height: 320px;
    }

    #notifyUserList {
        max-height: none;
    }

    .notify-modal-fields {
        flex: 1;
        justify-content: flex-start;
    }
}

/* ── Toast ─────────────────────────────────────────────────── */

.toast-container {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    z-index: 3000;
}

.toast {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 1rem 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    animation: slideIn 0.3s ease-out;
    min-width: 300px;
}

.toast-icon    { font-size: 1.25rem; flex-shrink: 0; }
.toast-content { flex: 1; min-width: 0; }
.toast-title   { font-weight: 600; margin-bottom: 0.25rem; font-size: 0.9375rem; }
.toast-message { font-size: 0.8125rem; color: var(--text-secondary); }
.toast-success { border-left: 4px solid var(--accent-success); }
.toast-error   { border-left: 4px solid var(--accent-danger); }
.toast-warning { border-left: 4px solid var(--accent-warning); }
.toast-info    { border-left: 4px solid var(--accent-primary); }
.toast-close { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: 0.25rem; margin-left: auto; font-size: 1rem; line-height: 1; flex-shrink: 0; opacity: 0.6; transition: opacity 0.15s; }
.toast-close:hover { opacity: 1; }

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* ── Responsive ────────────────────────────────────────────── */

@media (max-width: 950px) {
    .admin-user-form-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
        align-items: flex-end;
    }

    .admin-user-form-grid .form-group {
        flex: 1;
        min-width: 160px;
        margin-bottom: 0;
    }

    .admin-form-btn {
        align-self: flex-end;
        width: auto;
        flex-shrink: 0;
    }

    .user-list-item {
        flex-direction: column;
        align-items: flex-start;
    }

    .user-info {
        width: 100%;
    }

    .user-actions {
        width: 100%;
    }
}

@media (max-width: 650px) {
    .add-form-inline {
        flex-direction: column;
        align-items: stretch;
    }

    .add-form-inline .form-group {
        flex: 1;
        min-width: unset;
        margin-bottom: 0;
    }

    .add-form-inline .form-group:last-of-type {
        flex: unset;
    }

    .add-form-inline .btn {
        width: auto;
        align-self: flex-end;
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
    }

    .add-form-inline .btn-small {
        height: auto;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .form-actions {
        justify-content: flex-end;
    }

    .form-actions .btn-primary {
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
    }

    .admin-user-form-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
        align-items: flex-end;
    }

    .admin-user-form-grid .form-group {
        flex: 1;
        min-width: 150px;
        margin-bottom: 0;
    }

    .admin-form-btn {
        align-self: flex-end;
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
        width: auto;
        flex-shrink: 0;
    }

    .admin-section-count {
        align-self: flex-start;
    }

    .backup-action {
        flex-direction: column;
        align-items: flex-start;
    }

    .backup-action .btn {
        align-self: flex-end;
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
    }

    .container {
        padding: 0.75rem;
    }

    .card {
        padding: 0.75rem;
        margin-bottom: 1rem;
    }

    .user-actions .btn {
        padding: 0.3rem 0.5rem;
        font-size: 0.65rem;
    }

    .modal-content {
        width: 95%;
        padding: 1.5rem;
    }

    .toast-container {
        bottom: 1rem;
        right: 1rem;
    }

    .toast {
        min-width: 90%;
        font-size: 0.9rem;
    }

    .file-picker-row .btn {
        padding: 0.5rem 1rem;
        font-size: 0.8rem;
    }

    .header {
        gap: 0.75rem;
        margin-bottom: 1.5rem;
        padding-bottom: 1rem;
    }

    h1 {
        font-size: 1.5rem;
    }

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

@media (max-width: 380px) {
    #adminPanel .card-header {
        flex-wrap: wrap;
    }
    #adminPanel #companyMgmtLink {
        flex: 0 0 100%;
    }
}
