:root {
  color-scheme: light;
  --primary: #246bfd;
  --primary-dark: #1553d5;
  --primary-soft: #eaf1ff;
  --text: #17243b;
  --muted: #67758b;
  --border: #dce3ed;
  --surface: #ffffff;
  --background: #f4f7fb;
  --success: #13865f;
  --danger: #c9364b;
  --shadow: 0 8px 30px rgba(30, 54, 92, 0.08);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei UI", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { background: var(--background); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--text);
  background: var(--background);
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; }
button, label[for] { -webkit-user-select: none; user-select: none; }

button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 3px solid rgba(36, 107, 253, 0.25);
  outline-offset: 2px;
}

.visually-hidden {
  position: fixed !important;
  width: 1px !important;
  height: 1px !important;
  overflow: hidden !important;
  clip: rect(0 0 0 0) !important;
  white-space: nowrap !important;
}

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 74px;
  padding: 10px max(24px, env(safe-area-inset-right)) 10px max(24px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand > span:last-child { display: grid; }
.brand strong { font-size: 18px; line-height: 1.35; }
.brand small { margin-top: 2px; color: var(--muted); font-size: 12px; }
.brand-mark {
  display: grid;
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 12px;
  color: white;
  background: linear-gradient(145deg, #3b7cff, #1554d7);
  box-shadow: 0 7px 18px rgba(36, 107, 253, 0.25);
  font-weight: 800;
}

.top-actions { display: flex; align-items: center; gap: 9px; }
.local-badge { color: var(--success); font-size: 13px; white-space: nowrap; }
.local-badge i { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 17px;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
.button:active { transform: scale(0.98); }
.button:disabled { cursor: not-allowed; opacity: 0.46; transform: none; }
.button-primary { color: #fff; background: var(--primary); box-shadow: 0 4px 12px rgba(36, 107, 253, 0.2); }
.button-primary:hover:not(:disabled) { background: var(--primary-dark); }
.button-secondary { color: var(--primary); border-color: #b9cdf8; background: #fff; }
.button-secondary:hover:not(:disabled) { background: #f5f8ff; }
.button-soft { color: var(--primary-dark); background: var(--primary-soft); }
.button-text { min-height: 36px; padding: 0 9px; color: var(--muted); background: transparent; }
.button-text.danger { color: var(--danger); }
.button-row { display: flex; gap: 8px; margin-top: 13px; }
.button-row .button-soft { flex: 1; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(300px, 348px) minmax(0, 1fr);
  gap: 18px;
  width: min(1540px, 100%);
  min-height: calc(100vh - 74px);
  margin: 0 auto;
  padding: 18px 22px calc(28px + env(safe-area-inset-bottom));
}

.settings-column { min-width: 0; }
.mobile-settings > summary { display: none; }
.settings-content { display: grid; gap: 13px; }
.card, .privacy-card, .preview-card, .queue-section {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 2px 10px rgba(28, 51, 86, 0.035);
}
.card { padding: 17px; }
.card-heading { margin-bottom: 15px; }
.card-heading h2 { margin: 0; font-size: 15px; }
.card-heading p, .privacy-card p { margin: 4px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }

.field-label, .form-grid > label, .split-fields > label {
  display: grid;
  gap: 7px;
  margin: 11px 0 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
.field-label:first-of-type { margin-top: 0; }
input[type="text"], input[type="number"], textarea, select {
  width: 100%;
  min-height: 41px;
  padding: 8px 11px;
  border: 1px solid #cfd8e5;
  border-radius: 9px;
  color: var(--text);
  background: #fff;
}
textarea { resize: vertical; line-height: 1.55; }
input[type="color"] { width: 58px; height: 38px; padding: 3px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
input[type="checkbox"] { width: 19px; height: 19px; accent-color: var(--primary); }
input[type="range"] { width: 100%; min-height: 30px; margin: 0; accent-color: var(--primary); }
output { float: right; color: var(--primary); font-weight: 800; }
.field-hint { margin: 8px 0 0; color: var(--muted); font-size: 11px; line-height: 1.45; }
.form-grid { display: grid; gap: 8px; }
.form-grid > label { margin: 0; }
.split-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.split-fields > label { align-content: end; margin: 0; }
.split-fields .check-field { display: flex; align-items: center; align-self: end; min-height: 41px; margin: 0; }
.number-field { position: relative; display: block; }
.number-field input { padding-right: 32px; }
.number-field em { position: absolute; right: 9px; top: 50%; color: var(--muted); font-style: normal; transform: translateY(-50%); }

.privacy-card { display: flex; gap: 12px; padding: 15px 17px; color: var(--success); background: #f2fbf7; border-color: #c9ebdd; }
.privacy-card > span { display: grid; flex: 0 0 auto; width: 25px; height: 25px; place-items: center; border-radius: 50%; color: #fff; background: var(--success); font-weight: 900; }
.privacy-card strong { font-size: 14px; }

.workspace { display: flex; flex-direction: column; min-width: 0; gap: 14px; }
.drop-zone {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 92px;
  padding: 15px 20px;
  border: 2px dashed #b9c8db;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.65);
  transition: border 150ms ease, background 150ms ease;
}
.drop-zone.dragging { border-color: var(--primary); background: var(--primary-soft); }
.drop-zone > div:nth-child(2) { flex: 1; }
.drop-zone strong { font-size: 14px; }
.drop-zone p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.drop-icon { display: grid; flex: 0 0 auto; width: 43px; height: 43px; place-items: center; border-radius: 50%; color: var(--primary); background: var(--primary-soft); font-size: 27px; }

.queue-section { padding: 12px 14px 14px; }
.section-toolbar { display: flex; align-items: center; justify-content: space-between; min-height: 34px; font-size: 14px; }
.muted { color: var(--muted); font-size: 12px; font-weight: 500; }
.queue-list { display: flex; gap: 9px; overflow-x: auto; padding: 3px 2px 5px; overscroll-behavior-x: contain; scrollbar-width: thin; }
.queue-item {
  position: relative;
  display: grid;
  flex: 0 0 102px;
  height: 82px;
  padding: 0;
  overflow: hidden;
  border: 2px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  background: #e9edf3;
}
.queue-item.active { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(36, 107, 253, 0.13); }
.queue-item img { width: 100%; height: 100%; object-fit: cover; }
.queue-item .file-caption { position: absolute; inset: auto 0 0; overflow: hidden; padding: 12px 5px 4px; color: #fff; background: linear-gradient(transparent, rgba(0, 0, 0, 0.72)); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.remove-image { position: absolute; z-index: 2; top: 3px; right: 3px; display: grid; width: 25px; height: 25px; padding: 0; place-items: center; border: 0; border-radius: 50%; color: #fff; background: rgba(15, 23, 42, 0.72); cursor: pointer; font-size: 17px; }

.preview-card { display: flex; flex: 1; min-height: 570px; overflow: hidden; flex-direction: column; }
.preview-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 18px; min-height: 67px; padding: 11px 16px; border-bottom: 1px solid var(--border); }
.preview-title { display: grid; min-width: 0; }
.preview-title strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.preview-title span { margin-top: 3px; color: var(--muted); font-size: 11px; }
.segmented { display: flex; flex: 0 0 auto; padding: 3px; border-radius: 9px; background: #eef2f7; }
.segmented button { min-height: 32px; padding: 0 12px; border: 0; border-radius: 7px; color: var(--muted); cursor: pointer; background: transparent; font-size: 12px; font-weight: 700; }
.segmented button.active { color: var(--primary); background: #fff; box-shadow: 0 1px 4px rgba(24, 45, 78, 0.13); }

.canvas-stage {
  position: relative;
  display: grid;
  flex: 1;
  min-height: 410px;
  padding: 16px;
  overflow: hidden;
  place-items: center;
  background-color: #e9eef5;
  background-image: linear-gradient(45deg, rgba(255,255,255,.42) 25%, transparent 25%), linear-gradient(-45deg, rgba(255,255,255,.42) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgba(255,255,255,.42) 75%), linear-gradient(-45deg, transparent 75%, rgba(255,255,255,.42) 75%);
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-size: 16px 16px;
}
#previewCanvas { display: block; max-width: 100%; max-height: 68vh; border-radius: 3px; box-shadow: 0 9px 30px rgba(21, 38, 63, 0.18); object-fit: contain; }
.empty-state { display: grid; place-items: center; color: var(--muted); text-align: center; }
.empty-state > span { margin-bottom: 8px; color: #9ba8ba; font-size: 42px; }
.empty-state strong { color: #526176; font-size: 14px; }
.empty-state p { margin: 5px 0 0; font-size: 12px; }
.compare-line { position: absolute; z-index: 2; top: 16px; bottom: 16px; left: 50%; width: 2px; pointer-events: none; background: #fff; box-shadow: 0 0 0 1px rgba(20, 36, 60, .35); }
.compare-line span { position: absolute; top: 12px; left: 50%; padding: 4px 7px; border-radius: 5px; color: #fff; background: rgba(20, 36, 60, .78); font-size: 10px; transform: translateX(-50%); }
.compare-control { display: flex; align-items: center; gap: 9px; min-height: 43px; padding: 4px 18px; border-top: 1px solid var(--border); color: var(--muted); font-size: 11px; }
.compare-control input { flex: 1; }

.preview-actions { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 67px; padding: 11px 16px calc(11px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.preview-actions p { margin: 0; color: var(--muted); font-size: 12px; }
.preview-actions > div { display: flex; gap: 8px; }

.export-dialog {
  width: min(650px, calc(100vw - 28px));
  max-height: min(760px, calc(100dvh - 28px));
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 18px;
  color: var(--text);
  background: #fff;
  box-shadow: 0 24px 80px rgba(15, 30, 53, 0.3);
}
.export-dialog::backdrop { background: rgba(11, 22, 39, 0.58); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
.dialog-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 19px 14px; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; font-size: 18px; }
.dialog-header p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.icon-button { display: grid; width: 38px; height: 38px; padding: 0; place-items: center; border: 0; border-radius: 50%; color: var(--muted); cursor: pointer; background: #f0f3f7; font-size: 25px; }
.progress-wrap { display: flex; align-items: center; gap: 12px; padding: 14px 19px; }
.progress-wrap progress { flex: 1; height: 9px; accent-color: var(--primary); }
.progress-wrap span { color: var(--muted); font-size: 12px; }
.export-results { display: grid; gap: 8px; max-height: 48vh; padding: 2px 19px 15px; overflow: auto; }
.result-item { display: flex; align-items: center; gap: 10px; min-width: 0; padding: 10px 11px; border: 1px solid var(--border); border-radius: 10px; }
.result-status { display: grid; flex: 1; min-width: 0; }
.result-status strong { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.result-status span { margin-top: 3px; color: var(--muted); font-size: 11px; }
.result-actions { display: flex; gap: 4px; }
.result-actions .button { min-height: 35px; padding: 0 10px; font-size: 12px; }
.result-error { color: var(--danger) !important; }
.dialog-footer { display: flex; align-items: center; gap: 9px; padding: 13px 19px calc(13px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: #f8fafc; }
.dialog-footer p { flex: 1; margin: 0; color: var(--muted); font-size: 11px; line-height: 1.45; }
.toast { position: fixed; z-index: 100; right: 18px; bottom: calc(20px + env(safe-area-inset-bottom)); max-width: min(430px, calc(100vw - 36px)); padding: 12px 16px; border-radius: 10px; color: #fff; background: rgba(20, 34, 55, 0.94); box-shadow: var(--shadow); font-size: 13px; }

@media (max-width: 900px) {
  .topbar { min-height: 66px; padding: 8px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left)); }
  .brand small, .local-badge, .top-actions > label { display: none; }
  .brand-mark { width: 38px; height: 38px; }
  .brand strong { font-size: 16px; }
  .top-actions .button { min-height: 40px; padding: 0 13px; }
  .app-shell { display: flex; min-height: calc(100vh - 66px); padding: 12px 12px calc(20px + env(safe-area-inset-bottom)); flex-direction: column; }
  .settings-column { order: 2; }
  .workspace { order: 1; }
  .mobile-settings { border: 1px solid var(--border); border-radius: 14px; background: #fff; }
  .mobile-settings > summary { display: flex; align-items: center; justify-content: space-between; min-height: 52px; padding: 0 16px; cursor: pointer; font-size: 14px; font-weight: 800; }
  .mobile-settings > summary span { color: var(--muted); font-size: 11px; font-weight: 500; }
  .settings-content { padding: 0 10px 10px; }
  .preview-card { min-height: auto; }
  .canvas-stage { min-height: min(54vh, 520px); }
  #previewCanvas { max-height: 54vh; }
}

@media (max-width: 620px) {
  .top-actions #exportAllButton { font-size: 12px; }
  .drop-zone { display: grid; grid-template-columns: auto 1fr; padding: 13px; }
  .drop-zone .button { grid-column: 1 / -1; width: 100%; }
  .drop-zone p { line-height: 1.45; }
  .preview-toolbar { display: grid; gap: 9px; padding: 11px 12px; }
  .segmented { width: 100%; }
  .segmented button { flex: 1; }
  .canvas-stage { min-height: 43vh; padding: 10px; }
  #previewCanvas { max-height: 50vh; }
  .compare-line { top: 10px; bottom: 10px; }
  .preview-actions { align-items: stretch; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); flex-direction: column; }
  .preview-actions > div { display: grid; grid-template-columns: 1fr 1fr; }
  .preview-actions .button { padding: 0 10px; }
  .dialog-footer { align-items: stretch; flex-wrap: wrap; }
  .dialog-footer p { flex-basis: 100%; }
  .dialog-footer .button { flex: 1; padding: 0 10px; }
  .result-item { align-items: stretch; flex-direction: column; }
  .result-actions .button { flex: 1; }
}

@media (display-mode: standalone) and (max-width: 900px) {
  .topbar { padding-top: max(8px, env(safe-area-inset-top)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
