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

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --warning: #ca8a04;
    --bg: #f8fafc;
    --bg-white: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --sidebar-width: 240px;
    --devices-sidebar-width: 220px;
    --focus-ring: rgba(37, 99, 235, 0.1);
    --modal-backdrop: rgba(0, 0, 0, 0.5);
    --active-bg: rgba(37, 99, 235, 0.1);
    --connected-color: #16a34a;
    --disconnected-color: #94a3b8;
}

[data-theme="dark"] {
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #22c55e;
    --warning: #eab308;
    --bg: #0f172a;
    --bg-white: #1e293b;
    --border: #334155;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --focus-ring: rgba(59, 130, 246, 0.2);
    --modal-backdrop: rgba(0, 0, 0, 0.7);
    --active-bg: rgba(59, 130, 246, 0.2);
    --connected-color: #22c55e;
    --disconnected-color: #64748b;
}

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

/* Header */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1.5rem;
    height: 56px;
    background: var(--bg-white);
    border-bottom: 1px solid var(--border);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.125rem;
    color: var(--text);
    text-decoration: none;
}

.logo-icon {
    width: 28px;
    height: 28px;
    border-radius: 5px;
}

.separator {
    color: var(--text-muted);
}

.tool-name {
    color: var(--text-muted);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#user-menu {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

#user-name {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 0.375rem;
    border: none;
    cursor: pointer;
    transition: background 0.15s;
}

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

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

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

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

.btn-secondary:hover:not(:disabled) {
    background: var(--border);
}

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

.btn-danger:hover:not(:disabled) {
    background: var(--danger-hover);
}

.btn-sm {
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
}

.btn-xs {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

/* Inputs */
.input, .select {
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    background: var(--bg-white);
    color: var(--text);
    width: 100%;
}

.input:focus, .select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--focus-ring);
}

/* Main Layout */
.main {
    display: flex;
    height: calc(100vh - 56px);
}

/* Project Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-white);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.project-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;
}

.project-list li {
    padding: 0.75rem 1rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}

.project-list li:hover {
    background: var(--bg);
}

.project-list li.active {
    background: var(--active-bg);
    border-left: 3px solid var(--primary);
}

.project-list li.loading {
    color: var(--text-muted);
    cursor: default;
}

.project-list li.loading:hover {
    background: transparent;
}

.project-item-name {
    font-weight: 500;
    font-size: 0.875rem;
}

.project-item-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* Content */
.content {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
}

.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-state h2 {
    color: var(--text);
    margin-bottom: 0.5rem;
}

.empty-state-sm {
    text-align: center;
    padding: 2rem;
    color: var(--text-muted);
}

/* Project View */
.project-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.project-info h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.project-info p {
    color: var(--text-muted);
}

.project-actions {
    display: flex;
    gap: 0.5rem;
}

/* Department Tabs */
.department-tabs {
    display: flex;
    gap: 0.25rem;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.department-tab {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-muted);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    transition: all 0.15s;
}

.department-tab:hover {
    color: var(--text);
}

.department-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

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

/* Project Content Layout */
.project-content {
    display: flex;
    gap: 1rem;
    flex: 1;
    min-height: 0;
}

/* Devices Sidebar */
.devices-sidebar {
    width: var(--devices-sidebar-width);
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

.devices-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.devices-header h3 {
    font-size: 0.875rem;
    font-weight: 600;
}

.device-sections {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.device-section {
    margin-bottom: 1rem;
}

.device-section h4 {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    padding: 0.25rem 0.5rem;
    margin-bottom: 0.25rem;
}

.device-list {
    list-style: none;
}

.device-list li {
    padding: 0.5rem;
    font-size: 0.8125rem;
    border-radius: 0.25rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: background 0.15s;
}

.device-list li:hover {
    background: var(--bg);
}

.device-list li.active {
    background: var(--active-bg);
}

.device-list li .device-status {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--disconnected-color);
}

.device-list li .device-status.connected {
    background: var(--connected-color);
}

.endpoint-item {
    font-style: italic;
}

/* Main Panel */
.main-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* View Tabs */
.view-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.view-tab {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-muted);
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    cursor: pointer;
    transition: all 0.15s;
}

.view-tab:hover {
    background: var(--bg);
}

.view-tab.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* View Content */
.view-content {
    display: none;
    flex: 1;
    min-height: 0;
}

.view-content.active {
    display: flex;
    flex-direction: column;
}

/* Device Detail View */
.device-detail-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.device-detail-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.device-type-badge {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: 0.25rem;
    text-transform: uppercase;
}

.device-type-badge.source {
    background: #dbeafe;
    color: #1e40af;
}

.device-type-badge.destination {
    background: #fef3c7;
    color: #92400e;
}

[data-theme="dark"] .device-type-badge.source {
    background: rgba(59, 130, 246, 0.2);
    color: #60a5fa;
}

[data-theme="dark"] .device-type-badge.destination {
    background: rgba(251, 191, 36, 0.2);
    color: #fbbf24;
}

.device-detail-actions {
    margin-left: auto;
    display: flex;
    gap: 0.5rem;
}

/* Connections Section */
.connections-section {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.connections-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.connections-header h3 {
    font-size: 0.875rem;
    font-weight: 600;
}

.connections-list {
    list-style: none;
    flex: 1;
    overflow-y: auto;
}

.connection-item {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    gap: 1rem;
}

.connection-item:last-child {
    border-bottom: none;
}

.connection-info {
    flex: 1;
}

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

.connection-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.125rem;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.connection-status.connected {
    color: var(--success);
}

.connection-actions {
    display: flex;
    gap: 0.25rem;
}

.connect-btn {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 0.25rem;
    cursor: pointer;
}

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

.direction-badge {
    display: inline-block;
    padding: 0.125rem 0.375rem;
    font-size: 0.625rem;
    font-weight: 600;
    text-transform: uppercase;
    border-radius: 0.25rem;
    margin-left: 0.25rem;
}

.direction-badge.in {
    background: #dcfce7;
    color: #166534;
}

.direction-badge.out {
    background: #fef3c7;
    color: #92400e;
}

[data-theme="dark"] .direction-badge.in {
    background: rgba(34, 197, 94, 0.2);
    color: #4ade80;
}

[data-theme="dark"] .direction-badge.out {
    background: rgba(251, 191, 36, 0.2);
    color: #fbbf24;
}

/* Cable Toolbar */
.cable-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.filter-group {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.filter-group .input {
    width: 200px;
}

.filter-group .select {
    width: 150px;
}

.filter-group label {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* Cable Table */
.cable-table-container {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
}

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

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

.cable-table th {
    background: var(--bg);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--text-muted);
    position: sticky;
    top: 0;
}

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

.cable-table tbody tr:hover {
    background: var(--bg);
}

.cable-table td {
    font-size: 0.875rem;
}

.cable-actions {
    display: flex;
    gap: 0.5rem;
}

.cable-actions button {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

/* Cable Summary */
.cable-summary {
    display: flex;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--bg);
    border-top: 1px solid var(--border);
    font-size: 0.875rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* Cases View */
.cases-toolbar {
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.cases-container {
    flex: 1;
    overflow-y: auto;
}

.case-section {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

.case-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
}

.case-header h3 {
    font-size: 0.875rem;
    font-weight: 600;
}

.case-header .case-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-left: 0.5rem;
}

.case-header-actions {
    display: flex;
    gap: 0.25rem;
}

.case-cables {
    list-style: none;
    min-height: 50px;
}

.case-cables li {
    display: flex;
    align-items: center;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.8125rem;
    cursor: grab;
}

.case-cables li:last-child {
    border-bottom: none;
}

.case-cables li:hover {
    background: var(--bg);
}

.case-cable-info {
    flex: 1;
}

.case-cable-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.case-empty {
    padding: 1rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.8125rem;
}

/* Pull List View */
.pull-list-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.pull-list-content {
    flex: 1;
    overflow-y: auto;
}

.pull-list-group {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

.pull-list-group-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 0.875rem;
}

.pull-list-group-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: normal;
}

.pull-list-cables {
    list-style: none;
}

.pull-list-cable {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 80px;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.8125rem;
    align-items: center;
}

.pull-list-cable:last-child {
    border-bottom: none;
}

/* Device view: Type, Route, Length */
.pull-list-cable-device {
    grid-template-columns: 120px 1fr 80px;
    gap: 1rem;
}

.pull-cable-type {
    font-weight: 500;
}

.pull-cable-route {
    color: var(--text-muted);
}

.pull-cable-length {
    text-align: right;
}

/* Type view: Length, Route */
.pull-list-cable-type {
    grid-template-columns: 70px 1fr;
    gap: 1rem;
}

.pull-list-cable-type .pull-cable-length {
    text-align: right;
}

/* Case view: Length, Type, Route */
.pull-list-cable-case {
    grid-template-columns: 70px 180px 1fr;
    gap: 1rem;
}

.pull-cable-qty {
    font-weight: 600;
    color: var(--primary);
}

.pull-list-totals {
    display: flex;
    justify-content: space-between;
    padding: 1rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-top: 1rem;
    font-weight: 600;
}

/* Theme Toggle */
.theme-toggle {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    padding: 0.375rem 0.5rem;
    cursor: pointer;
    font-size: 0.875rem;
    line-height: 1;
    transition: background 0.15s;
}

.theme-toggle:hover {
    background: var(--border);
}

.theme-toggle::before {
    content: '☀️';
}

[data-theme="dark"] .theme-toggle::before {
    content: '🌙';
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--modal-backdrop);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: var(--bg-white);
    border-radius: 0.5rem;
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-content.modal-sm {
    max-width: 400px;
}

.modal-content.modal-lg {
    max-width: 600px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
}

.modal-close:hover {
    color: var(--text);
}

/* Forms */
form {
    padding: 1.5rem;
}

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

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.375rem;
}

.form-group label input[type="checkbox"] {
    margin-right: 0.5rem;
}

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

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

.help-text {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* Connection Source Info */
.connection-source-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: var(--bg);
    border-radius: 0.375rem;
    margin-bottom: 1rem;
    font-weight: 500;
}

.connection-arrow {
    color: var(--text-muted);
}

/* Destination Options */
.destination-options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.destination-section h4 {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-bottom: 0.5rem;
}

/* Print Styles */
@media print {
    .header,
    .sidebar,
    .devices-sidebar,
    .view-tabs,
    .pull-list-toolbar,
    .cable-toolbar,
    .project-header .project-actions,
    .department-tabs,
    .btn,
    button,
    #device-view,
    #cable-view,
    #cases-view {
        display: none !important;
    }

    .main {
        height: auto;
        display: block;
    }

    .content {
        padding: 0.25in;
        overflow: visible;
    }

    .project-content {
        display: block;
    }

    .main-panel {
        width: 100%;
    }

    #pull-list-view {
        display: block !important;
    }

    .pull-list-content {
        overflow: visible;
    }

    .pull-list-group {
        break-inside: avoid;
        margin-bottom: 0.15in;
        border: 1px solid #999;
    }

    .pull-list-group-header {
        background: #eee !important;
        padding: 0.05in 0.1in;
        font-size: 10pt;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .pull-list-cable {
        display: grid !important;
        padding: 0.02in 0.1in;
        font-size: 9pt;
        border-bottom: 1px solid #ddd;
        gap: 0.15in !important;
    }

    .pull-list-cable:last-child {
        border-bottom: none;
    }

    .pull-list-cable-device {
        grid-template-columns: 90px 1fr 50px !important;
    }

    .pull-list-cable-type {
        grid-template-columns: 50px 1fr !important;
    }

    .pull-list-cable-case {
        grid-template-columns: 50px 120px 1fr !important;
    }

    .pull-list-cable-type .pull-cable-length {
        text-align: right;
    }

    .pull-cable-type {
        font-weight: 600;
        font-style: italic;
    }

    .pull-cable-qty {
        font-weight: 600;
    }

    .pull-cable-route {
        color: #333;
    }

    .pull-cable-length {
        text-align: right !important;
    }

    .pull-list-totals {
        margin-top: 0.15in;
        padding: 0.05in 0.1in;
        font-size: 10pt;
        border: 1px solid #999;
        background: #eee !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    body {
        background: white;
        color: black;
        font-size: 9pt;
    }

    .project-info h1 {
        font-size: 14pt;
        margin-bottom: 0.1in;
    }

    .project-info p {
        font-size: 10pt;
        margin-bottom: 0.1in;
    }

    .pull-list-group,
    .cable-table-container,
    .case-section {
        border: 1px solid #ccc;
    }
}

/* Responsive */
@media (max-width: 1024px) {
    .project-content {
        flex-direction: column;
    }

    .devices-sidebar {
        width: 100%;
        max-height: 200px;
    }

    .device-sections {
        display: flex;
        gap: 1rem;
    }

    .device-section {
        flex: 1;
        margin-bottom: 0;
    }
}

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

    .sidebar {
        width: 100%;
        height: auto;
        max-height: 200px;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .project-header {
        flex-direction: column;
        gap: 1rem;
    }

    .cable-toolbar,
    .pull-list-toolbar {
        flex-direction: column;
        gap: 0.75rem;
        align-items: stretch;
    }

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

    .filter-group .input,
    .filter-group .select {
        width: 100%;
    }

    .cable-table-container {
        overflow-x: auto;
    }

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

    .destination-options {
        grid-template-columns: 1fr;
    }

    .view-tabs {
        flex-wrap: wrap;
    }

    .pull-list-cable,
    .pull-list-cable-device,
    .pull-list-cable-type,
    .pull-list-cable-case {
        grid-template-columns: 1fr;
        gap: 0.25rem;
    }

    .pull-cable-length,
    .pull-cable-qty {
        text-align: left;
    }
}
