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

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-surface: #0f3460;
  --bg-rack: #2a2a3e;
  --rail-color: #4a4a5e;
  --slot-border: #3a3a4e;
  --switch-color: #1b4965;
  --switch-accent: #5fa8d3;
  --patch-color: #4a2c40;
  --patch-accent: #d4738a;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b0;
  --port-idle: #555566;
  --port-active: #00e676;
  --port-selected: #ffab00;
  --danger: #ff5252;
  --success: #4caf50;
  --slot-height: 44px;
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

/* ===== RACK CONFIG (in sidebar) ===== */
#rack-config {
  padding: 12px 16px;
  border-bottom: 1px solid var(--slot-border);
}

#rack-config label {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: block;
  margin-bottom: 6px;
}

#rack-config-row {
  display: flex;
  gap: 6px;
}

#rack-size-input {
  flex: 1;
  padding: 5px 8px;
  background: var(--bg-primary);
  border: 1px solid var(--slot-border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 13px;
  text-align: center;
}

#btn-apply-rack-size {
  padding: 5px 14px;
  background: var(--bg-surface);
  border: 1px solid var(--switch-accent);
  border-radius: 4px;
  color: var(--switch-accent);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s;
}

#btn-apply-rack-size:hover {
  background: var(--switch-accent);
  color: var(--bg-primary);
}

/* ===== PROJECT CONTROLS ===== */
#project-controls {
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--slot-border);
}

#project-controls button {
  flex: 1;
  padding: 5px 4px;
  background: var(--bg-primary);
  border: 1px solid var(--slot-border);
  border-radius: 4px;
  color: var(--text-secondary);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s;
}

#project-controls button:hover {
  border-color: var(--switch-accent);
  color: var(--switch-accent);
}

#file-input.hidden {
  display: none;
}

/* ===== MAIN LAYOUT ===== */
#app-container {
  display: grid;
  grid-template-columns: 240px 1fr 260px;
  height: 100vh;
}

/* ===== LEFT SIDEBAR: Device Library ===== */
#device-library {
  background: var(--bg-secondary);
  border-right: 1px solid var(--slot-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#device-library h2 {
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px 8px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#library-tabs {
  display: flex;
  padding: 0 12px;
  gap: 4px;
  margin-bottom: 8px;
}

#library-tabs .tab {
  flex: 1;
  padding: 6px 4px;
  background: transparent;
  border: 1px solid var(--slot-border);
  border-radius: 4px;
  color: var(--text-secondary);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s;
}

#library-tabs .tab:hover {
  border-color: var(--text-secondary);
}

#library-tabs .tab.active {
  background: var(--bg-surface);
  border-color: var(--switch-accent);
  color: var(--switch-accent);
}

/* Brand filter dropdown */
#brand-filter-wrap {
  padding: 0 12px 6px;
}

#brand-filter-wrap.hidden {
  display: none;
}

#brand-filter {
  width: 100%;
  padding: 6px 8px;
  background: var(--bg-primary);
  border: 1px solid var(--slot-border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23a0a0b0'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
}

#brand-filter:hover {
  border-color: var(--switch-accent);
}

#brand-filter:focus {
  outline: none;
  border-color: var(--switch-accent);
  box-shadow: 0 0 0 2px rgba(95, 168, 211, 0.2);
}

/* Port-count group header */
.port-group-header {
  font-size: 10px;
  font-weight: 600;
  color: var(--switch-accent);
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 10px 4px 4px;
  border-bottom: 1px solid rgba(95, 168, 211, 0.2);
  margin-bottom: 4px;
  margin-top: 4px;
}

.port-group-header:first-child {
  margin-top: 0;
  padding-top: 4px;
}

#library-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 12px;
}

/* Library cards */
.library-card {
  padding: 10px 12px;
  margin-bottom: 6px;
  border-radius: 6px;
  cursor: grab;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.library-card:active {
  cursor: grabbing;
}

.library-card.switch-card {
  background: var(--switch-color);
  border-color: rgba(95, 168, 211, 0.3);
}

.library-card.switch-card:hover {
  border-color: var(--switch-accent);
}

.library-card.patch-panel-card {
  background: var(--patch-color);
  border-color: rgba(212, 115, 138, 0.3);
}

.library-card.patch-panel-card:hover {
  border-color: var(--patch-accent);
}

.library-card .card-name {
  font-size: 12px;
  font-weight: 500;
  margin-bottom: 4px;
}

.library-card .card-meta {
  display: flex;
  gap: 8px;
  font-size: 10px;
  color: var(--text-secondary);
}

.library-card .card-meta .badge {
  padding: 1px 6px;
  border-radius: 3px;
  background: rgba(255,255,255,0.1);
}

/* Custom device form */
#custom-device-form {
  padding: 12px 16px;
  border-top: 1px solid var(--slot-border);
}

#custom-device-form.hidden {
  display: none;
}

#custom-device-form h3 {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#custom-device-form input,
#custom-device-form select {
  width: 100%;
  padding: 6px 8px;
  margin-bottom: 6px;
  background: var(--bg-primary);
  border: 1px solid var(--slot-border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 12px;
}

#btn-add-custom {
  width: 100%;
  padding: 6px;
  background: var(--bg-surface);
  border: 1px solid var(--switch-accent);
  border-radius: 4px;
  color: var(--switch-accent);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s;
}

#btn-add-custom:hover {
  background: var(--switch-accent);
  color: var(--bg-primary);
}

/* ===== CENTER: Rack Area ===== */
#rack-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: auto;
  padding: 20px;
  background: var(--bg-primary);
  min-width: 0;
}

#rack-wrapper {
  position: relative;
  display: inline-block;
}

#rack-frame {
  display: flex;
  background: var(--bg-rack);
  border: 2px solid #555;
  border-radius: 4px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

.rack-rail {
  width: 28px;
  background: linear-gradient(90deg, #5a5a6e 0%, #4a4a5e 40%, #3a3a4e 100%);
  border: 1px solid #6a6a7e;
  position: relative;
}

#rack-left-rail {
  border-radius: 3px 0 0 3px;
}

#rack-right-rail {
  border-radius: 0 3px 3px 0;
}

#rack-slots {
  position: relative;
  width: clamp(300px, 40vw, 500px);
  background: var(--bg-rack);
}

.rack-slot {
  height: var(--slot-height);
  border-bottom: 1px solid var(--slot-border);
  position: relative;
}

.rack-slot:last-child {
  border-bottom: none;
}

/* U labels on left rail */
.rail-label {
  position: absolute;
  width: 28px;
  height: var(--slot-height);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  color: var(--text-secondary);
  border-bottom: 1px solid rgba(106, 106, 126, 0.3);
  user-select: none;
}

/* Screw holes on rails */
.screw-hole {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #333;
  border: 1px solid #555;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.5);
}

.rack-rail .screw-hole.top-screw {
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
}

.rack-rail .screw-hole.bottom-screw {
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
}

/* Drop preview */
.slot-preview {
  position: absolute;
  left: 0;
  right: 0;
  pointer-events: none;
  z-index: 5;
  border-radius: 3px;
  transition: opacity 0.15s;
}

.slot-preview.valid {
  background: rgba(76, 175, 80, 0.25);
  border: 2px dashed var(--success);
}

.slot-preview.invalid {
  background: rgba(255, 82, 82, 0.25);
  border: 2px dashed var(--danger);
}

/* ===== DEVICES IN RACK ===== */
.rack-device {
  position: absolute;
  left: 4px;
  right: 4px;
  border-radius: 4px;
  z-index: 10;
  cursor: grab;
  transition: box-shadow 0.2s;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.rack-device:active {
  cursor: grabbing;
}

.rack-device.dragging {
  opacity: 0.4;
}

.rack-device.switch-device {
  background: var(--switch-color);
  border: 1px solid var(--switch-accent);
}

.rack-device.patch-panel-device {
  background: var(--patch-color);
  border: 1px solid var(--patch-accent);
}

.btn-remove-device {
  position: absolute;
  top: 2px;
  right: 4px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.2s;
}

.rack-device:hover .btn-remove-device {
  opacity: 1;
}

.btn-remove-device:hover {
  color: var(--danger);
}

.device-ports {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 2px 6px 4px;
  flex: 1;
  justify-content: center;
}

.port-row {
  display: flex;
  gap: 1px;
  flex-wrap: wrap;
}

.device-port {
  width: 16px;
  height: 12px;
  background: var(--port-idle);
  border: 1px solid #777;
  border-radius: 2px;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s;
  position: relative;
}

.device-port:hover {
  background: #888;
  z-index: 1;
}

.device-port.connected {
  background: var(--port-active);
  border-color: #00c853;
}

.device-port.selected {
  background: var(--port-selected);
  border-color: #ff8f00;
  box-shadow: 0 0 6px var(--port-selected);
  z-index: 2;
}

.device-port .port-tooltip {
  display: none;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 3px;
  white-space: nowrap;
  z-index: 100;
  pointer-events: none;
}

.device-port:hover .port-tooltip {
  display: block;
}

/* ===== SVG OVERLAY ===== */
#connection-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 15;
}

.cable-visible {
  pointer-events: none;
}

/* ===== TRASH ZONE ===== */
#trash-zone {
  margin-top: 16px;
  padding: 16px 32px;
  border: 2px dashed var(--danger);
  border-radius: 8px;
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  opacity: 0.6;
  transition: all 0.2s;
}

#trash-zone.hidden {
  display: none;
}

#trash-zone.drag-over {
  opacity: 1;
  background: rgba(255, 82, 82, 0.15);
}

/* ===== RIGHT PANEL: Connections ===== */
#connections-panel {
  background: var(--bg-secondary);
  border-left: 1px solid var(--slot-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#connections-panel h2 {
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px 8px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#port-status {
  padding: 0 16px 8px;
  font-size: 12px;
  color: var(--port-selected);
  min-height: 22px;
}

#connections-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

#connections-table thead {
  background: rgba(0,0,0,0.2);
}

#connections-table th {
  padding: 6px 8px;
  text-align: left;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--slot-border);
}

#connections-table td {
  padding: 5px 8px;
  border-bottom: 1px solid rgba(58, 58, 78, 0.5);
  vertical-align: middle;
}

#connections-tbody {
  display: block;
  overflow-y: auto;
  max-height: calc(100vh - 200px);
}

#connections-table thead tr,
#connections-tbody tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}

.color-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  vertical-align: middle;
}

.btn-delete-connection {
  background: transparent;
  border: 1px solid rgba(255, 82, 82, 0.4);
  border-radius: 3px;
  color: var(--danger);
  font-size: 10px;
  padding: 2px 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-delete-connection:hover {
  background: var(--danger);
  color: #fff;
}

#connection-stats {
  padding: 8px 16px;
  font-size: 12px;
  color: var(--text-secondary);
  border-top: 1px solid var(--slot-border);
  margin-top: auto;
}

/* ===== MODAL ===== */
#modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

#modal-overlay.hidden {
  display: none;
}

#modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--slot-border);
  border-radius: 8px;
  padding: 20px;
  min-width: 300px;
  max-width: 400px;
}

#modal-title {
  font-size: 15px;
  margin-bottom: 12px;
}

#modal-body {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

#modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

#modal-actions button {
  padding: 6px 16px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  border: 1px solid var(--slot-border);
  transition: all 0.2s;
}

#modal-cancel {
  background: transparent;
  color: var(--text-secondary);
}

#modal-cancel:hover {
  background: rgba(255,255,255,0.05);
}

#modal-confirm {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

#modal-confirm:hover {
  background: #e04040;
}

/* ===== TOAST ===== */
#toast-container {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.toast {
  padding: 8px 20px;
  border-radius: 6px;
  font-size: 12px;
  color: #fff;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
  pointer-events: none;
}

.toast.info {
  background: var(--bg-surface);
  border: 1px solid var(--switch-accent);
}

.toast.success {
  background: #2e7d32;
}

.toast.error {
  background: #c62828;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

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

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--slot-border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--rail-color);
}
