:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #d1d5db;
  --soft-line: #e5e7eb;
  --accent: #111827;
  --danger: #b42318;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.topbar {
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 0 26px;
  background: rgba(255,255,255,.96);
  border-bottom: 1px solid var(--soft-line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand-line { display: flex; align-items: center; gap: 12px; }
.brand-line strong { display: block; font-size: 15px; }
.brand-mark, .login-mark {
  display: grid;
  place-items: center;
  background: #111827;
  color: white;
  font-weight: 800;
  letter-spacing: -.04em;
}
.brand-mark { width: 36px; height: 36px; border-radius: 9px; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.top-actions form { margin: 0; }
.signed-in { color: var(--muted); font-size: 13px; margin-right: 4px; }
.small { font-size: 12px; display: block; margin-top: 1px; }
.muted { color: var(--muted); }

.app-shell { max-width: 1440px; margin: 0 auto; padding: 24px; }
.workspace { display: grid; grid-template-columns: minmax(420px, .95fr) minmax(420px, 1.05fr); gap: 20px; }
.panel {
  background: var(--panel);
  border: 1px solid var(--soft-line);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(0,0,0,.03);
}
.form-panel, .preview-panel, .history-panel { padding: 22px; }
.history-panel { margin-top: 20px; }
.panel-heading, .dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}
h1, h2, p { margin-top: 0; }
h1 { font-size: 24px; letter-spacing: -.03em; margin-bottom: 0; }
h2 { font-size: 18px; letter-spacing: -.02em; margin-bottom: 0; }
.eyebrow { color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .13em; margin: 0 0 4px; }

button {
  border: 0;
  border-radius: 9px;
  padding: 10px 14px;
  font-weight: 700;
}
.primary { background: var(--accent); color: #fff; }
.secondary { background: #eef0f3; color: var(--ink); }
.ghost { background: transparent; color: var(--ink); padding: 8px 10px; }
.ghost:hover, .secondary:hover { background: #e5e7eb; }
.wide { width: 100%; }
.print-button { width: 100%; margin-top: 18px; min-height: 48px; font-size: 15px; }
.link-button, .danger-link { background: transparent; padding: 0; }
.link-button { color: #374151; margin-top: 5px; }
.danger-link { color: var(--danger); font-size: 13px; }
.icon-button { background: transparent; color: #4b5563; font-size: 26px; line-height: 1; padding: 2px 5px; }

.field { display: grid; gap: 7px; }
.field > span { font-size: 12px; font-weight: 750; color: #4b5563; }
input, select, textarea {
  width: 100%;
  border: 1px solid #cfd4dc;
  background: #fff;
  color: var(--ink);
  border-radius: 9px;
  padding: 11px 12px;
  outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: #6b7280; box-shadow: 0 0 0 3px rgba(17,24,39,.08); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .field:first-child { grid-column: 1 / -1; }
.size-row { display: flex; gap: 10px; align-items: end; margin-bottom: 16px; }
.grow { flex: 1; }
.size-field { width: 112px; }
.suffix-input { position: relative; }
.suffix-input input { padding-right: 42px; }
.suffix-input b { position: absolute; right: 11px; top: 50%; transform: translateY(-50%); font-size: 12px; color: var(--muted); }
.extra-fields { display: grid; gap: 10px; margin-top: 12px; }
.extra-field-row { display: grid; grid-template-columns: .7fr 1.3fr 34px; gap: 8px; }
.extra-field-row button { padding: 0; background: #f3f4f6; color: #6b7280; }
.print-options { display: flex; align-items: end; gap: 15px; margin-top: 18px; border-top: 1px solid var(--soft-line); padding-top: 16px; }
.copies-field { width: 90px; flex: none; }
.print-note { color: var(--muted); font-size: 11px; line-height: 1.45; }

.preview-panel { min-height: 560px; }
.preview-heading { margin-bottom: 12px; }
.preview-stage {
  min-height: 470px;
  border-radius: 10px;
  background: #e6e8eb;
  border: 1px solid #d6d9df;
  display: grid;
  place-items: center;
  padding: 26px;
  overflow: hidden;
}
.label-preview {
  --preview-w: 520px;
  width: min(var(--preview-w), 100%);
  aspect-ratio: var(--label-ratio, 2 / 1);
  background: white;
  color: black;
  border: 1px solid #c6c6c6;
  box-shadow: 0 12px 28px rgba(0,0,0,.13);
  padding: 4.5%;
  display: grid;
  grid-template-rows: 1fr auto auto;
  overflow: hidden;
  font-family: Arial, Helvetica, sans-serif;
}
.label-fields { align-self: start; display: grid; gap: .55em; min-height: 0; }
.label-row { display: grid; grid-template-columns: minmax(0, 28%) 13px minmax(0, 1fr); align-items: baseline; font-weight: 700; font-size: clamp(12px, 2.05vw, 23px); line-height: 1.04; }
.label-key, .label-value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.label-colon { text-align: center; }
.label-divider { border-top: 2px solid #333; margin: 4% 0 3%; }
.label-footer { display: grid; grid-template-columns: 42% 1fr; gap: 5%; align-items: center; min-height: 16%; }
.logo-wrap { height: 100%; min-height: 34px; display: grid; place-items: center start; overflow: hidden; }
.logo-wrap img { max-height: 100%; max-width: 100%; object-fit: contain; object-position: left center; filter: grayscale(1) contrast(1.12); display: none; }
#logoPlaceholder { font-size: clamp(18px, 4.5vw, 38px); font-weight: 900; letter-spacing: -.06em; color: #666; }
.business-block { display: flex; flex-direction: column; font-size: clamp(9px, 1.45vw, 15px); line-height: 1.13; min-width: 0; }
.business-block span { white-space: pre-line; }
.business-block strong:empty, .business-block span:empty { display: none; }
.label-preview.compact { padding: 3.1%; }
.label-preview.compact .label-fields { gap: .25em; }
.label-preview.compact .label-row { font-size: clamp(10px, 1.55vw, 18px); line-height: 1; }
.label-preview.compact .label-divider { margin: 2.1% 0 1.8%; border-top-width: 1.5px; }
.label-preview.compact .label-footer { min-height: 13%; }
.label-preview.compact .business-block { font-size: clamp(7px, 1vw, 12px); }
.label-preview.tall .label-row { font-size: clamp(14px, 2.3vw, 25px); }
.label-preview.tall .business-block { font-size: clamp(10px, 1.6vw, 17px); }

.history-list { display: grid; gap: 8px; }
.history-item {
  display: grid;
  grid-template-columns: 145px minmax(100px, .8fr) minmax(130px, 1fr) minmax(120px, .8fr) 84px 150px;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--soft-line);
  border-radius: 10px;
  padding: 11px 12px;
}
.history-item:hover { border-color: #c8cdd5; }
.history-time { color: var(--muted); font-size: 12px; }
.history-main { min-width: 0; }
.history-main strong, .history-main span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-main span { color: var(--muted); font-size: 12px; margin-top: 2px; }
.history-size { color: var(--muted); font-size: 12px; }
.history-actions { display: flex; justify-content: flex-end; gap: 6px; }
.history-actions button { font-size: 12px; padding: 7px 9px; }
.empty-state { color: var(--muted); padding: 20px 4px; text-align: center; }

.settings-dialog {
  border: 0;
  border-radius: 16px;
  padding: 0;
  width: min(620px, calc(100vw - 30px));
  box-shadow: 0 24px 80px rgba(0,0,0,.28);
}
.settings-dialog::backdrop { background: rgba(17,24,39,.48); }
.settings-dialog form { padding: 24px; display: grid; gap: 15px; }
.dialog-heading { margin-bottom: 3px; }
.settings-size-grid .field:first-child { grid-column: auto; }
.logo-setting-row { display: flex; gap: 8px; }
.logo-setting-row input { flex: 1; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--soft-line); padding-top: 16px; margin-top: 4px; }
.settings-message { min-height: 18px; font-size: 12px; color: var(--muted); }

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  background: #111827;
  color: white;
  padding: 11px 14px;
  border-radius: 9px;
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: .16s ease;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateY(0); }
.alert { border-radius: 9px; padding: 10px 12px; font-size: 13px; margin-bottom: 14px; }
.alert.error { color: #8a1c14; background: #feeceb; border: 1px solid #f6c7c3; }

.login-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: #eef0f3; }
.login-shell { width: min(390px, 100%); }
.login-card { background: white; border: 1px solid var(--soft-line); border-radius: 18px; padding: 30px; box-shadow: 0 20px 60px rgba(0,0,0,.08); }
.login-mark { width: 48px; height: 48px; border-radius: 12px; margin-bottom: 24px; }
.login-card h1 { margin-bottom: 8px; }
.login-form { display: grid; gap: 14px; margin-top: 22px; }
.login-form label { display: grid; gap: 7px; }
.login-form label span { font-size: 12px; font-weight: 750; color: #4b5563; }
.login-foot { margin: 18px 0 0; color: var(--muted); font-size: 11px; text-align: center; }

#printPages { display: none; }

@media (max-width: 980px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-panel { min-height: 0; }
  .preview-stage { min-height: 420px; }
  .history-item { grid-template-columns: 110px 1fr 1fr 120px; }
  .history-item > :nth-child(4), .history-size { display: none; }
}

@media (max-width: 620px) {
  .topbar { padding: 0 14px; }
  .signed-in { display: none; }
  .app-shell { padding: 12px; }
  .form-panel, .preview-panel, .history-panel { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .field:first-child { grid-column: auto; }
  .size-row { flex-wrap: wrap; }
  .size-row .grow { flex-basis: 100%; }
  .size-field { flex: 1; width: auto; }
  .history-item { grid-template-columns: 1fr auto; }
  .history-item > :not(.history-main):not(.history-actions) { display: none; }
  .preview-stage { padding: 12px; min-height: 320px; }
  .print-options { align-items: start; }
}

@media print {
  html, body {
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
  }
  .no-print { display: none !important; }
  #printPages {
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
  }
  .print-label-page {
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    break-after: page;
    page-break-after: always;
  }
  .print-label-page:last-child {
    break-after: auto;
    page-break-after: auto;
  }
  .print-label-page .label-preview {
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    aspect-ratio: auto !important;
    border: 0 !important;
    box-shadow: none !important;
  }
}
