/* DW Spectrum Gate Control — Main Stylesheet */

:root {
    --primary: #1a73e8;
    --primary-dark: #1557b0;
    --primary-light: #e8f0fe;
    --success: #34a853;
    --success-light: #e6f4ea;
    --danger: #ea4335;
    --danger-light: #fce8e6;
    --warning: #fbbc04;
    --warning-light: #fef7e0;
    --gray-50: #f8f9fa;
    --gray-100: #f1f3f4;
    --gray-200: #e8eaed;
    --gray-300: #dadce0;
    --gray-400: #bdc1c6;
    --gray-500: #9aa0a6;
    --gray-600: #80868b;
    --gray-700: #5f6368;
    --gray-800: #3c4043;
    --gray-900: #202124;
    --bg: #f8f9fa;
    --card-bg: #ffffff;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.1);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.12);
    --shadow-lg: 0 4px 16px rgba(0,0,0,0.15);
    --radius: 8px;
    --radius-lg: 12px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
    background: var(--bg);
    color: var(--gray-900);
    line-height: 1.5;
    min-height: 100vh;
}

/* --- Layout --- */

.app-container {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 240px;
    background: var(--gray-900);
    color: white;
    padding: 20px 0;
    position: fixed;
    height: 100vh;
    overflow-y: auto;
    z-index: 100;
}

.sidebar-brand {
    padding: 0 20px 20px;
    border-bottom: 1px solid var(--gray-700);
    margin-bottom: 10px;
}

.sidebar-brand h2 {
    font-size: 16px;
    font-weight: 600;
    color: white;
}

.sidebar-brand small {
    color: var(--gray-500);
    font-size: 11px;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px;
    color: var(--gray-400);
    text-decoration: none;
    font-size: 14px;
    transition: all 0.15s;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
    background: rgba(255,255,255,0.08);
    color: white;
}

.sidebar-nav .section-title {
    padding: 15px 20px 5px;
    font-size: 11px;
    text-transform: uppercase;
    color: var(--gray-600);
    letter-spacing: 0.5px;
}

.main-content {
    flex: 1;
    margin-left: 240px;
    padding: 20px 30px;
    padding-bottom: 80px;
}

/* --- Top Bar --- */

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.top-bar h1 {
    font-size: 24px;
    font-weight: 600;
    color: var(--gray-900);
}

.top-bar-actions {
    display: flex;
    gap: 10px;
    align-items: center;
}

/* --- Status Bar (sticky bottom) --- */

.status-bar {
    position: fixed;
    bottom: 0;
    left: 240px;
    right: 0;
    background: var(--gray-900);
    color: white;
    padding: 8px 30px;
    display: flex;
    gap: 20px;
    align-items: center;
    font-size: 13px;
    z-index: 50;
}

.status-bar .status-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
}

.status-dot.green { background: var(--success); }
.status-dot.red { background: var(--danger); }
.status-dot.gray { background: var(--gray-500); }

/* --- Buttons --- */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
}

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

.btn-primary {
    background: var(--primary);
    color: white;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }

.btn-success {
    background: var(--success);
    color: white;
}
.btn-success:hover:not(:disabled) { background: #2d8f47; }

.btn-danger {
    background: var(--danger);
    color: white;
}
.btn-danger:hover:not(:disabled) { background: #d33426; }

.btn-outline {
    background: transparent;
    border: 1px solid var(--gray-300);
    color: var(--gray-700);
}
.btn-outline:hover:not(:disabled) { background: var(--gray-100); }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 12px 24px; font-size: 16px; }

.btn-gate {
    background: var(--success);
    color: white;
    font-size: 14px;
    font-weight: 700;
    padding: 10px 20px;
    border-radius: var(--radius);
    width: 100%;
    justify-content: center;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.btn-gate:hover:not(:disabled) { background: #2d8f47; box-shadow: var(--shadow-md); }

.btn-gate.triggering {
    background: var(--warning);
    color: var(--gray-900);
}

.btn-gate.triggered {
    background: var(--primary);
}

/* --- Cards --- */

.card {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 20px;
    margin-bottom: 16px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.card-header h3 {
    font-size: 16px;
    font-weight: 600;
}

/* --- Gate Tile Grid --- */

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

.gate-tile {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: box-shadow 0.15s;
    border: 2px solid transparent;
    position: relative;
}

.gate-tile:hover { box-shadow: var(--shadow-md); }
.gate-tile.selected { border-color: var(--primary); }

.gate-tile.offline {
    opacity: 0.7;
}

.tile-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.tile-name {
    font-size: 16px;
    font-weight: 600;
    color: var(--gray-900);
    word-break: break-word;
}

.tile-subtitle {
    font-size: 12px;
    color: var(--gray-600);
    margin-top: 2px;
}

.tile-meta {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    font-size: 12px;
    color: var(--gray-600);
}

.tile-meta span {
    display: flex;
    align-items: center;
    gap: 4px;
}

.tile-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
}

.favorite-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 18px;
    color: var(--gray-400);
    padding: 2px;
}

.favorite-btn.active { color: var(--warning); }

.tile-footer {
    margin-top: auto;
}

.last-triggered {
    font-size: 11px;
    color: var(--gray-500);
    margin-bottom: 6px;
}

/* Tile color coding */
.gate-tile[data-color="red"] { border-left: 4px solid var(--danger); }
.gate-tile[data-color="green"] { border-left: 4px solid var(--success); }
.gate-tile[data-color="blue"] { border-left: 4px solid var(--primary); }
.gate-tile[data-color="yellow"] { border-left: 4px solid var(--warning); }
.gate-tile[data-color="purple"] { border-left: 4px solid #9c27b0; }

/* --- Server Group (grid view grouping) --- */

.tile-grid-grouped {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.server-group {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.server-group-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: var(--gray-900);
    color: white;
    font-size: 14px;
    font-weight: 600;
}

.server-group-count {
    background: rgba(255,255,255,0.15);
    color: var(--gray-300);
    font-size: 11px;
    font-weight: 500;
    padding: 1px 8px;
    border-radius: 10px;
    margin-left: auto;
}

.server-group .tile-grid {
    padding: 16px;
}

/* --- Section Headers --- */

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.section-header h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--gray-800);
}

.section-divider {
    margin: 30px 0 20px;
}

/* --- Filters / Search --- */

.filter-bar {
    display: flex;
    gap: 10px;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.search-input {
    flex: 1;
    min-width: 200px;
    padding: 8px 14px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s;
}

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

select.filter-select {
    padding: 8px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 14px;
    background: white;
    outline: none;
    cursor: pointer;
}

/* --- Tables --- */

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

.data-table th,
.data-table td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid var(--gray-200);
    font-size: 14px;
}

.data-table th {
    font-weight: 600;
    color: var(--gray-700);
    background: var(--gray-50);
    position: sticky;
    top: 0;
}

.data-table tr:hover { background: var(--gray-50); }

.data-table .actions {
    display: flex;
    gap: 6px;
}

/* --- Forms --- */

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 4px;
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    border-color: var(--primary);
}

.form-row {
    display: flex;
    gap: 16px;
}

.form-row .form-group { flex: 1; }

/* --- Modals --- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 24px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal h3 {
    font-size: 18px;
    margin-bottom: 16px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
}

/* --- Toast Notifications --- */

.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    font-size: 14px;
    animation: slideIn 0.3s ease;
    max-width: 400px;
}

.toast.success { background: var(--success); color: white; }
.toast.error { background: var(--danger); color: white; }
.toast.info { background: var(--primary); color: white; }
.toast.warning { background: var(--warning); color: var(--gray-900); }

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

/* --- Loading / Spinner --- */

.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.spinner.dark {
    border-color: rgba(0,0,0,0.1);
    border-top-color: var(--primary);
}

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

.loading-overlay {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 60px;
    color: var(--gray-500);
}

/* --- Discovery Progress --- */

.discovery-progress {
    margin-top: 20px;
}

.discovery-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid var(--gray-200);
    font-size: 14px;
}

.discovery-item .name { flex: 1; font-weight: 500; }
.discovery-item .device-count { color: var(--gray-600); }
.discovery-item .status-icon { font-size: 16px; }

/* --- Badges --- */

.badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
}

.badge-success { background: var(--success-light); color: var(--success); }
.badge-danger { background: var(--danger-light); color: var(--danger); }
.badge-warning { background: var(--warning-light); color: #b06000; }
.badge-info { background: var(--primary-light); color: var(--primary); }

/* --- Login Page --- */

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--gray-900) 0%, var(--gray-800) 100%);
}

.login-card {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 40px;
    width: 100%;
    max-width: 400px;
}

.login-card h1 {
    font-size: 24px;
    text-align: center;
    margin-bottom: 8px;
}

.login-card p {
    text-align: center;
    color: var(--gray-600);
    margin-bottom: 24px;
    font-size: 14px;
}

.login-error {
    background: var(--danger-light);
    color: var(--danger);
    padding: 10px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 13px;
    display: none;
}

/* --- Pagination --- */

.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-top: 20px;
}

.pagination button {
    padding: 6px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    background: white;
    cursor: pointer;
    font-size: 13px;
}

.pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination button.active { background: var(--primary); color: white; border-color: var(--primary); }
.pagination .page-info { font-size: 13px; color: var(--gray-600); }

/* --- Empty State --- */

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--gray-500);
}

.empty-state h3 {
    font-size: 18px;
    margin-bottom: 8px;
    color: var(--gray-700);
}

/* --- View Toggle --- */

.view-toggle {
    display: flex;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    overflow: hidden;
}

.view-toggle button {
    padding: 6px 14px;
    border: none;
    background: white;
    cursor: pointer;
    font-size: 13px;
    color: var(--gray-600);
}

.view-toggle button.active {
    background: var(--primary);
    color: white;
}

/* --- List View (compact rows) --- */

.list-view {
    display: flex;
    flex-direction: column;
    gap: 0;
}

.list-view .list-header-row {
    display: flex;
    align-items: center;
    padding: 8px 16px;
    font-size: 11px;
    font-weight: 600;
    color: var(--gray-600);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    background: var(--gray-50);
    border-radius: var(--radius) var(--radius) 0 0;
    border-bottom: 2px solid var(--gray-200);
}

.list-view .gate-tile {
    flex-direction: row;
    align-items: center;
    gap: 0;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    border: none;
    border-bottom: 1px solid var(--gray-200);
    background: var(--card-bg);
}

.list-view .gate-tile:last-child {
    border-radius: 0 0 var(--radius) var(--radius);
}

.list-view .gate-tile:hover {
    background: var(--gray-50);
    box-shadow: none;
}

.list-view .gate-tile.selected {
    background: var(--primary-light);
    border-color: var(--gray-200);
}

.list-view .gate-tile[data-color="red"],
.list-view .gate-tile[data-color="green"],
.list-view .gate-tile[data-color="blue"],
.list-view .gate-tile[data-color="yellow"],
.list-view .gate-tile[data-color="purple"] {
    border-left: none;
}

.list-view .list-col {
    padding: 10px 12px;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

.list-view .list-col-drag {
    width: 32px;
    flex-shrink: 0;
    justify-content: center;
    cursor: grab;
    color: var(--gray-400);
    font-size: 14px;
}

.list-view .list-col-drag:active { cursor: grabbing; }

.list-view .list-col-fav {
    width: 36px;
    flex-shrink: 0;
    justify-content: center;
}

.list-view .list-col-check {
    width: 32px;
    flex-shrink: 0;
    justify-content: center;
}

.list-view .list-col-name {
    flex: 2;
    min-width: 120px;
    font-weight: 500;
    color: var(--gray-900);
}

.list-view .list-col-name .list-device-sub {
    font-size: 11px;
    font-weight: 400;
    color: var(--gray-500);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.list-view .list-col-server {
    flex: 1.5;
    min-width: 100px;
    color: var(--gray-700);
}

.list-view .list-col-location {
    flex: 1.2;
    min-width: 90px;
    color: var(--gray-600);
}

.list-view .list-col-location .location-text {
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    transition: background 0.15s;
}

.list-view .list-col-location .location-text:hover {
    background: var(--primary-light);
    color: var(--primary);
}

.list-view .list-col-location .location-empty {
    color: var(--gray-400);
    font-style: italic;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
}

.list-view .list-col-location .location-empty:hover {
    background: var(--gray-100);
    color: var(--gray-600);
}

.list-view .list-col-status {
    width: 80px;
    flex-shrink: 0;
}

.list-view .list-col-action {
    width: 130px;
    flex-shrink: 0;
    padding-right: 16px;
}

.list-view .list-col-action .btn-gate {
    padding: 6px 14px;
    font-size: 12px;
    width: 100%;
}

.list-view .tile-header,
.list-view .tile-meta,
.list-view .tile-footer,
.list-view .tile-subtitle,
.list-view .last-triggered {
    display: none;
}

/* Server group headers in list view */
.list-view .location-group-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    color: white;
    background: var(--gray-800);
    border-bottom: 1px solid var(--gray-200);
    letter-spacing: 0.3px;
}

.list-view .location-group-header span:not(.status-dot) {
    color: var(--gray-400);
    font-weight: 400;
    font-size: 11px;
    margin-left: auto;
}

/* --- Responsive --- */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -240px;
        transition: left 0.2s;
    }
    .sidebar.open { left: 0; }

    .main-content {
        margin-left: 0;
        padding: 16px;
    }

    .status-bar { left: 0; }

    .tile-grid {
        grid-template-columns: 1fr;
    }

    .filter-bar {
        flex-direction: column;
    }

    .form-row {
        flex-direction: column;
    }

    .top-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .top-bar-actions {
        flex-wrap: wrap;
    }

    .list-view .list-col-server,
    .list-view .list-col-location,
    .list-view .list-header-row {
        display: none;
    }

    .list-view .list-col-name {
        flex: 1;
    }
}

/* --- Checkbox for bulk select --- */

.tile-checkbox {
    position: absolute;
    top: 12px;
    left: 12px;
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* --- Confirmation dialog --- */

.confirm-dialog {
    text-align: center;
}

.confirm-dialog .gate-name {
    font-size: 20px;
    font-weight: 700;
    color: var(--gray-900);
    margin: 16px 0;
}

.confirm-dialog .warning {
    color: var(--gray-600);
    font-size: 14px;
    margin-bottom: 20px;
}

/* --- Drag handle --- */

.drag-handle {
    cursor: grab;
    color: var(--gray-400);
    font-size: 16px;
    padding: 4px;
}

.drag-handle:active { cursor: grabbing; }

/* --- Gridstack Overrides --- */

.grid-stack {
    min-height: 100px;
}

.grid-stack-item-content {
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.grid-stack-item-content:hover {
    box-shadow: var(--shadow-md);
}

.grid-stack-item.ui-draggable-dragging .grid-stack-item-content {
    box-shadow: var(--shadow-lg);
    opacity: 0.9;
}

.grid-stack-placeholder > .placeholder-content {
    border: 2px dashed var(--primary) !important;
    border-radius: var(--radius-lg) !important;
    background: var(--primary-light) !important;
}

/* Server card within Gridstack item */

.server-card {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.server-card .server-group-header {
    cursor: grab;
    user-select: none;
    flex-shrink: 0;
}

.server-card .server-group-header:active {
    cursor: grabbing;
}

.server-card-tiles {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 12px;
    padding: 12px;
    overflow-y: auto;
    flex: 1;
}

/* Make tiles inside gridstack cards slightly smaller */
.server-card-tiles .gate-tile {
    margin: 0;
}

/* Tile header actions row */
.tile-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

/* --- Visibility Toggle Button --- */

.visibility-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 14px;
    padding: 2px 4px;
    border-radius: 4px;
    opacity: 0.6;
    transition: opacity 0.15s, background 0.15s;
    line-height: 1;
}

.visibility-btn:hover {
    opacity: 1;
    background: var(--gray-100);
}

.visibility-btn.is-hidden {
    opacity: 0.4;
    color: var(--danger);
}

.visibility-btn.is-hidden:hover {
    opacity: 0.8;
    background: var(--danger-light);
}

/* --- Hidden Device Styling --- */

.gate-tile.hidden-device {
    opacity: 0.4;
    position: relative;
}

.gate-tile.hidden-device .tile-name {
    text-decoration: line-through;
    color: var(--gray-500);
}

.gate-tile.hidden-device::after {
    content: 'HIDDEN';
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 9px;
    font-weight: 700;
    color: var(--danger);
    background: var(--danger-light);
    padding: 1px 6px;
    border-radius: 4px;
    letter-spacing: 0.5px;
}

/* List view visibility column */
.list-view .list-col-vis {
    width: 36px;
    flex-shrink: 0;
    justify-content: center;
}

/* Gridstack resize handle styling */
.grid-stack > .grid-stack-item > .ui-resizable-se {
    width: 20px;
    height: 20px;
    background: var(--gray-400);
    border-radius: 0 0 var(--radius-lg) 0;
    opacity: 0;
    transition: opacity 0.15s;
}

.grid-stack > .grid-stack-item:hover > .ui-resizable-se {
    opacity: 0.5;
}

.grid-stack > .grid-stack-item > .ui-resizable-se:hover {
    opacity: 1;
}
