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

:root {
  --green:       #63A830;
  --green-dark:  #4d8526;
  --green-light: #eef6e7;
  --black:       #000000;
  --white:       #ffffff;
  --bg:          #f2f2f0;
  --border:      #deded8;
  --border-light:#ededea;
  --text:        #1a1a1a;
  --text-muted:  #666666;
  --text-subtle: #999999;
  --radius:      6px;
  --radius-sm:   4px;
  --shadow:      0 1px 4px rgba(0,0,0,0.10);
  --shadow-md:   0 4px 16px rgba(0,0,0,0.14);
  --header-h:    56px;
}

html { font-size: 14px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* =============================================
   HEADER
   ============================================= */
.app-header {
  height: var(--header-h);
  background: var(--black);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--white);
  font-weight: 600;
  font-size: 15px;
}

.header-logo {
  background: var(--green);
  color: var(--white);
  font-weight: 700;
  font-size: 12px;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.5px;
}

/* =============================================
   MAIN LAYOUT
   ============================================= */
.app-main {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
}

/* =============================================
   MACHINES SECTION
   ============================================= */
.machines-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
}

.section-header {
  padding: 14px 20px;
  background: var(--white);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.section-header h2 {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}

.filters {
  display: flex;
  gap: 8px;
  flex: 1;
  flex-wrap: wrap;
}

.filters select,
.filters input {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: var(--white);
  color: var(--text);
  outline: none;
}
.filters select:focus,
.filters input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 2px rgba(99,168,48,0.15);
}
.filters input { flex: 1; min-width: 160px; }
.filters select { min-width: 150px; }

/* =============================================
   STATE BOXES (loading / error)
   ============================================= */
.state-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex: 1;
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
}

.state-box--error {
  gap: 8px;
}

.state-icon {
  font-size: 40px;
  line-height: 1;
  color: #e67e22;
}

.state-box h3 { color: var(--text); font-size: 16px; }
.state-box p { font-size: 13px; max-width: 480px; line-height: 1.6; }

.cors-help {
  background: #fff8ed;
  border: 1px solid #f0d080;
  border-radius: var(--radius);
  padding: 12px 16px;
  text-align: left;
  max-width: 520px;
  font-size: 12px;
  line-height: 1.6;
}
.cors-help summary { cursor: pointer; font-weight: 600; font-size: 13px; color: #b8700a; }
.cors-help pre {
  margin: 8px 0;
  background: var(--white);
  border: 1px solid var(--border);
  padding: 10px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-size: 11px;
}
.cors-help code {
  background: #f0f0ec;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 12px;
}

/* =============================================
   SPINNER
   ============================================= */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =============================================
   MACHINE GRID
   ============================================= */
.machine-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
  align-content: start;
}

.no-results {
  grid-column: 1 / -1;
  text-align: center;
  padding: 48px;
  color: var(--text-muted);
  font-size: 14px;
}

/* =============================================
   MACHINE CARD
   ============================================= */
.machine-card {
  background: var(--white);
  border: 2px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
  position: relative;
  user-select: none;
}

.machine-card:hover {
  border-color: var(--green);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.machine-card--selected {
  border-color: var(--green);
  background: var(--green-light);
}

.selected-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--green);
  color: var(--white);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  z-index: 1;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.machine-img-wrap {
  width: 100%;
  aspect-ratio: 4/3;
  overflow: hidden;
  background: var(--bg);
}

.machine-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.2s;
}
.machine-card:hover .machine-img-wrap img { transform: scale(1.03); }

.img-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--text-subtle);
  background: #eeeee8;
}

.machine-info {
  padding: 10px 12px 12px;
}

.category-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--green);
  margin-bottom: 4px;
}

.machine-name {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  margin-bottom: 5px;
}

.machine-specs {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  align-items: center;
  margin-bottom: 6px;
}
.spec-sep { opacity: 0.4; }

.machine-price {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

/* =============================================
   SELECTION PANEL
   ============================================= */
.selection-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--white);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.selected-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  text-align: center;
  gap: 8px;
}
.selected-empty p { font-size: 13px; color: var(--text-muted); line-height: 1.6; }
.selected-empty .hint { font-size: 12px; color: var(--text-subtle); }

.selected-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px;
}

/* =============================================
   SELECTED ITEM (in panel)
   ============================================= */
.selected-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-light);
  margin-bottom: 6px;
  background: var(--white);
  cursor: grab;
  transition: background 0.1s, border-color 0.1s;
}
.selected-item:hover { background: #fafaf8; }
.selected-item.dragging { opacity: 0.4; }
.selected-item.drag-over { border-color: var(--green); background: var(--green-light); }

.drag-handle {
  color: var(--border);
  font-size: 16px;
  cursor: grab;
  flex-shrink: 0;
}

.item-num {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-subtle);
  width: 16px;
  text-align: center;
  flex-shrink: 0;
}

.item-thumb {
  width: 40px;
  height: 30px;
  object-fit: cover;
  border-radius: 3px;
  flex-shrink: 0;
}
.item-thumb--empty {
  background: var(--bg);
  border-radius: 3px;
  display: block;
}

.item-name {
  flex: 1;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.item-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-subtle);
  font-size: 14px;
  padding: 2px 4px;
  border-radius: 3px;
  line-height: 1;
  flex-shrink: 0;
  transition: color 0.1s, background 0.1s;
}
.item-remove:hover { color: #c0392b; background: #fdecea; }

/* =============================================
   PANEL FOOTER
   ============================================= */
.panel-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

/* =============================================
   COUNT BADGES
   ============================================= */
.count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  background: var(--bg);
  border-radius: 11px;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}
.count-badge--green {
  background: var(--green);
  color: var(--white);
}

/* =============================================
   BUTTONS
   ============================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 34px;
  padding: 0 14px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  font-family: inherit;
}

.btn-primary {
  background: var(--green);
  color: var(--white);
  border-color: var(--green);
}
.btn-primary:hover { background: var(--green-dark); border-color: var(--green-dark); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }

.btn-secondary {
  background: var(--white);
  color: var(--green);
  border-color: var(--green);
}
.btn-secondary:hover { background: var(--green-light); }

.btn-ghost {
  background: none;
  color: var(--white);
  border-color: rgba(255,255,255,0.3);
}
.btn-ghost:hover { background: rgba(255,255,255,0.1); }

.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn-full { width: 100%; }

/* Ghost in light context */
.modal .btn-ghost,
.panel-footer .btn-ghost {
  color: var(--text-muted);
  border-color: var(--border);
}
.modal .btn-ghost:hover,
.panel-footer .btn-ghost:hover { background: var(--bg); }

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

/* =============================================
   MODALS
   ============================================= */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
  cursor: pointer;
}

.modal-content {
  position: relative;
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 480px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 64px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-content--wide {
  width: 680px;
  max-width: calc(100vw - 32px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h3 { font-size: 15px; font-weight: 600; }

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: var(--text-muted);
  padding: 4px 6px;
  border-radius: 3px;
  line-height: 1;
}
.modal-close:hover { background: var(--bg); color: var(--text); }

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-body--preview {
  padding: 0;
  background: #e8e8e4;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: auto;
}

#preview-iframe {
  width: 640px;
  height: 600px;
  border: none;
  display: block;
  margin: 20px auto;
  background: var(--white);
  box-shadow: var(--shadow-md);
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* =============================================
   FORM ELEMENTS
   ============================================= */
.form-group {
  margin-bottom: 16px;
}
.form-group:last-child { margin-bottom: 0; }

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

.required { color: #c0392b; }

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  background: var(--white);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 2px rgba(99,168,48,0.15);
}

.form-group textarea { resize: vertical; line-height: 1.5; }

.form-group small {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-subtle);
}

/* =============================================
   TOAST
   ============================================= */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  white-space: nowrap;
  background: var(--black);
  color: var(--white);
}
.toast--success { background: var(--green); }
.toast--error { background: #c0392b; }

/* =============================================
   SCROLLBAR STYLING
   ============================================= */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #b0b0a8; }
