:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-subtle: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #059669;
  --success-soft: #ecfdf5;
  --warning: #d97706;
  --warning-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 6px 20px rgba(15, 23, 42, 0.07);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans Thai', 'Inter', system-ui, -apple-system, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.app-shell {
  max-width: 1380px;
  margin: 0 auto;
  padding: 20px 24px 48px;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  color: #ffffff;
  display: grid;
  place-items: center;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.25);
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand p {
  margin: 2px 0 0;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--text-muted);
  box-shadow: var(--shadow-sm);
}

.pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
}

/* Main Grid */
.main-grid {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 22px;
  align-items: start;
}

@media (max-width: 960px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
}

/* Cards & Controls */
.settings-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
}

.card-title {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Controls */
.control-group {
  margin-bottom: 12px;
}

.control-group:last-child {
  margin-bottom: 0;
}

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

.control-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.control-label label {
  margin-bottom: 0;
}

.value-badge {
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--primary-soft);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 999px;
}

input[type='range'] {
  width: 100%;
  accent-color: var(--primary);
  cursor: pointer;
}

select,
input[type='password'],
input[type='text'] {
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.15s ease;
}

select:focus,
input[type='password']:focus,
input[type='text']:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.hint {
  display: block;
  font-size: 0.75rem;
  color: var(--text-subtle);
  margin-top: 3px;
}

.row-inline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.row-inline label {
  margin-bottom: 0;
}

.color-picks {
  display: flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1.5px solid var(--border-strong);
  cursor: pointer;
  padding: 0;
}

.swatch.active {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

#bgColorPicker {
  width: 28px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: none;
  cursor: pointer;
}

/* Format Chips */
.format-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  font-size: 0.75rem;
  font-weight: 500;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* Workspace Panel */
.workspace-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dropzone {
  background: var(--surface);
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 34px 24px;
  text-align: center;
  cursor: pointer;
  transition: all 0.18s ease;
  box-shadow: var(--shadow-sm);
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.drag-over {
  border-color: var(--primary);
  background: var(--primary-soft);
  outline: none;
}

.drop-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 12px;
  border-radius: 16px;
  background: var(--primary-soft);
  color: var(--primary);
  display: grid;
  place-items: center;
}

.dropzone h2 {
  margin: 0 0 6px;
  font-size: 1.12rem;
  font-weight: 700;
}

.dropzone p {
  margin: 0 0 16px;
  font-size: 0.87rem;
  color: var(--text-muted);
}

.drop-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.paste-hint {
  font-size: 0.8rem;
  color: var(--text-subtle);
}

kbd {
  background: var(--surface-muted);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.75rem;
  font-family: inherit;
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s ease;
}

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

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8rem;
}

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

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

.btn-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}

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

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

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

/* Queue Header */
.queue-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  box-shadow: var(--shadow-sm);
}

.queue-stats {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.progress-summary {
  font-size: 0.83rem;
  color: var(--text-muted);
}

.queue-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.empty-state {
  text-align: center;
  padding: 28px 16px;
  color: var(--text-subtle);
  font-size: 0.9rem;
}

/* Results Grid */
.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 14px;
}

.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.result-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.thumb-box {
  position: relative;
  height: 170px;
  background: #e2e8f0;
  display: grid;
  place-items: center;
  overflow: hidden;
  cursor: pointer;
}

.thumb-box img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: repeating-conic-gradient(#f1f5f9 0% 25%, #e2e8f0 0% 50%) 50% / 16px 16px;
}

.thumb-placeholder {
  font-size: 0.82rem;
  color: var(--text-muted);
  padding: 12px;
  text-align: center;
}

.status-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: rgba(15, 23, 42, 0.78);
  color: #ffffff;
}

.status-badge.done {
  background: var(--success);
}

.status-badge.working {
  background: var(--primary);
}

.status-badge.error {
  background: var(--danger);
}

.card-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.file-name {
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-meta {
  font-size: 0.76rem;
  color: var(--text-subtle);
}

.error-msg {
  font-size: 0.76rem;
  color: var(--danger);
  background: var(--danger-soft);
  padding: 6px 8px;
  border-radius: 6px;
}

.card-actions {
  display: flex;
  gap: 6px;
  margin-top: auto;
  padding-top: 6px;
}

.card-actions .btn {
  flex: 1;
  padding: 6px 10px;
  font-size: 0.78rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.65);
  backdrop-filter: blur(3px);
}

.modal-dialog {
  position: relative;
  width: min(1120px, 100%);
  max-height: 90vh;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.modal-header h3 {
  margin: 0;
  font-size: 1rem;
}

.modal-header small {
  color: var(--text-subtle);
  font-size: 0.78rem;
}

.modal-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-close {
  border: none;
  background: var(--surface-muted);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
}

.icon-close:hover {
  background: var(--border);
  color: var(--text);
}

.modal-body {
  display: grid;
  grid-template-columns: 1fr;
  overflow: hidden;
  flex: 1;
}

.modal-image-pane {
  background: #0f172a;
  padding: 16px;
  overflow: auto;
  display: grid;
  place-items: center;
  max-height: 75vh;
}

.modal-image-pane img {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: 6px;
  background: #ffffff;
}

/* Offscreen Staging for Office Rendering */
#offscreenStage {
  position: fixed;
  left: -10000px;
  top: 0;
  width: 900px;
  pointer-events: none;
  z-index: -1;
}
