:root {
  --bg-app: #0b0f17;
  --bg-card: #151d2a;
  --bg-hover: #1e293b;
  --bg-input: #0e1420;
  --border: #2a374d;
  --border-hover: #3d4f6d;
  --primary: #38bdf8;
  --primary-hover: #7dd3fc;
  --primary-glow: rgba(56, 189, 248, 0.25);
  --success: #34d399;
  --danger: #f87171;
  --text: #cbd5e1;
  --text-bright: #ffffff;
  --text-dim: #94a3b8;
  --radius: 8px;
  --transition: all 0.15s ease;
}

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

button, input, select, textarea, option {
  font-family: inherit;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background-color: var(--bg-app);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 13.5px;
  line-height: 1.4;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}

/* Toàn bộ app vừa trọn 100% viewport laptop (Zero Scrollbar) */
.app-layout {
  width: 100vw;
  height: 100vh;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-sizing: border-box;
  overflow: hidden;
}

/* Topbar: Gọn gàng, thanh lịch, 1 hàng duy nhất */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 14px;
  gap: 12px;
  flex-shrink: 0;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text-bright);
}

.brand-icon {
  font-size: 1.25rem;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
}

.divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 2px;
}

/* Nút tải ảnh / Drop button */
.drop-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-input);
  border: 1px dashed var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 0.88rem;
  color: var(--text-bright);
  font-weight: 500;
  max-width: 220px;
  transition: var(--transition);
}

.drop-btn:hover, .drop-btn.dragover {
  border-color: var(--primary);
  background: rgba(56, 189, 248, 0.1);
}

#fileInput {
  display: none;
}

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-sm {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: var(--transition);
  white-space: nowrap;
}

.btn-ghost {
  background: var(--bg-input);
  border-color: var(--border);
  color: var(--text-bright);
}

.btn-ghost:hover {
  background: var(--bg-hover);
  border-color: var(--border-hover);
}

.btn-action {
  background: var(--primary);
  color: #04101c;
  box-shadow: 0 2px 8px var(--primary-glow);
}

.btn-action:hover:not(:disabled) {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

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

.btn-download {
  background: #059669;
  color: white;
  padding: 4px 10px;
  font-size: 0.82rem;
}

.btn-download:hover:not(:disabled) {
  background: #10b981;
}

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

.btn-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(0, 0, 0, 0.25);
  border-top-color: #04101c;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}

.btn-action.loading .btn-spinner {
  display: inline-block;
}

.btn-action.loading .btn-icon {
  display: none;
}

/* Mode Select: Chuẩn font Inter, nền tối đẹp mắt */
.mode-select-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.88rem;
}

.mode-select-wrap label {
  color: var(--text-dim);
  font-weight: 600;
}

.mode-select-wrap select {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-input);
  color: var(--text-bright);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 28px 5px 10px;
  font-size: 0.88rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  outline: none;
  cursor: pointer;
  min-width: 150px;
  max-width: 220px;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%2338bdf8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  transition: var(--transition);
}

.mode-select-wrap select:hover {
  border-color: var(--border-hover);
}

.mode-select-wrap select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-glow);
}

.mode-select-wrap select option {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: #151d2a;
  color: #f0f6fc;
  padding: 8px 12px;
  font-size: 0.88rem;
  font-weight: 500;
}

/* Toggle Switch */
.toggle-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  font-size: 0.84rem;
  color: var(--text-dim);
}

.toggle-wrap input {
  display: none;
}

.slider {
  width: 28px;
  height: 16px;
  background: var(--border);
  border-radius: 16px;
  position: relative;
  transition: var(--transition);
}

.slider::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  transition: var(--transition);
}

.toggle-wrap input:checked + .slider {
  background: var(--primary);
}

.toggle-wrap input:checked + .slider::after {
  transform: translateX(12px);
}

/* Status Pill */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-dim);
  background: var(--bg-input);
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--text-dim);
}

.status-pill.connected .dot {
  background-color: var(--success);
  box-shadow: 0 0 5px var(--success);
}

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

/* Thanh mô tả thuật toán */
.info-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 0.86rem;
  color: var(--text-bright);
  flex-shrink: 0;
}

.info-tag {
  color: var(--primary);
  font-weight: 700;
  white-space: nowrap;
}

/* Workspace Grid: Chiếm trọn 100% chiều cao còn lại (Side by Side) */
.workspace-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  min-height: 0; /* Rất quan trọng: cho phép container con co giãn trong flexbox */
}

.image-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

.box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(11, 15, 23, 0.6);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.box-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-bright);
}

.pill {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  color: white;
}

.pill-blue { background: #2563eb; }
.pill-green { background: #059669; }

.box-meta {
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* Khung ảnh: Tự động lấp đầy 100% không gian còn lại */
.box-view {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 8px;
  background: #06090e;
  background-image: 
    linear-gradient(45deg, #0e141f 25%, transparent 25%), 
    linear-gradient(-45deg, #0e141f 25%, transparent 25%), 
    linear-gradient(45deg, transparent 75%, #0e141f 75%), 
    linear-gradient(-45deg, transparent 75%, #0e141f 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
}

.preview-img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.empty-icon {
  font-size: 1.8rem;
  opacity: 0.6;
}

.empty-state p {
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--text-bright);
}

.empty-state small {
  font-size: 0.76rem;
}

/* Loading Overlay */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(6, 9, 14, 0.85);
  backdrop-filter: blur(2px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--text-bright);
  z-index: 10;
}

.mini-spinner {
  width: 26px;
  height: 26px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* Box Footer */
.box-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(11, 15, 23, 0.6);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.stats-group {
  display: flex;
  gap: 6px;
}

.stat-item {
  font-size: 0.78rem;
  background: var(--bg-input);
  padding: 2px 7px;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--text-dim);
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: 14px;
  right: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 9999;
}

.toast {
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.82rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
  color: white;
  animation: slideIn 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.toast-success { background: #065f46; border-left: 3px solid #34d399; }
.toast-error { background: #7f1d1d; border-left: 3px solid #f87171; }
.toast-info { background: #1e3a8a; border-left: 3px solid #38bdf8; }

.hidden {
  display: none !important;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* Khi thu nhỏ cửa sổ cực hẹp dưới 700px */
@media (max-width: 700px) {
  html, body { overflow-y: auto; }
  .app-layout { height: auto; min-height: 100vh; overflow-y: auto; }
  .topbar-controls { flex-wrap: wrap; }
  .workspace-grid { grid-template-columns: 1fr; }
  .box-view { height: 280px; }
}
