/* =========================================================
   Barcode Studio · 條碼工作室
   設計風格：輕快穩重 · 純色簡潔
   佈局：行動優先，桌面自動放大
   ========================================================= */

:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --surface-3: #e2e8f0;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --text: #0f172a;
  --text-2: #334155;
  --text-3: #64748b;
  --text-muted: #94a3b8;

  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --primary-text: #ffffff;

  --accent: #0891b2;
  --accent-2: #d946ef;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --danger-hover: #b91c1c;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.12);
  --shadow-xl: 0 24px 64px rgba(15, 23, 42, 0.16);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 999px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Noto Sans TC",
          "PingFang TC", "Microsoft JhengHei", "Helvetica Neue", sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", Consolas, monospace;

  --header-h: 56px;
  --tab-h: 64px;
  --max-w: 720px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #1e293b;
  --surface-3: #334155;
  --border: #334155;
  --border-strong: #475569;

  --text: #f8fafc;
  --text-2: #cbd5e1;
  --text-3: #94a3b8;
  --text-muted: #64748b;

  --primary: #60a5fa;
  --primary-hover: #93c5fd;
  --primary-soft: #1e3a5f;
  --primary-text: #0f172a;

  --accent: #22d3ee;
  --accent-2: #f472b6;
  --success: #4ade80;
  --warning: #fbbf24;
  --danger: #f87171;
  --danger-hover: #ef4444;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
  --shadow-xl: 0 24px 64px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  background-attachment: fixed;
}

/* ========== App 容器 ========== */
#app, .help-page, .detail-page {
  max-width: var(--max-w);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ========== Offline Bar ========== */
.offline-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--warning);
  color: #0f172a;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
  white-space: nowrap;
  animation: fadeIn 0.25s ease;
}

.offline-badge[hidden] { display: none; }

/* ========== Header ========== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--header-h);
}

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

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  display: grid;
  place-items: center;
  background: var(--primary);
  color: white;
}

.brand-text h1 {
  font-size: 17px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 11px;
  color: var(--text-3);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.header-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--text-2);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  text-decoration: none;
  flex-shrink: 0;
}

.icon-btn:hover, .icon-btn:active {
  background: var(--surface-3);
  color: var(--text);
}

.icon-sun { display: none; }
.icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

/* ========== Tab Bar ========== */
.tab-bar {
  position: sticky;
  top: calc(var(--header-h) + var(--safe-top));
  z-index: 90;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 6px;
  gap: 4px;
}

.tab-btn {
  position: relative;
  border: none;
  background: transparent;
  color: var(--text-3);
  padding: 10px 8px;
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.2s;
}

.tab-btn:hover { background: var(--surface-3); }

.tab-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
}

.tab-btn .badge {
  position: absolute;
  top: 4px;
  right: calc(50% - 24px);
  background: var(--accent-2);
  color: white;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--radius-full);
  display: grid;
  place-items: center;
}

/* ========== Main ========== */
.app-main {
  flex: 1;
  padding: 16px 14px calc(var(--safe-bottom) + 24px + var(--tab-h));
}

.tab-panel { display: none; animation: fadeIn 0.25s ease; }
.tab-panel.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ========== Card ========== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
}

/* ========== Form Fields ========== */
.field {
  margin-bottom: 16px;
}

.field:last-child { margin-bottom: 0; }

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 8px;
  letter-spacing: -0.005em;
}

.optional {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 12px;
}

.field input[type="text"],
.field input[type="search"],
.field select,
.field textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 12px 14px;
  border-radius: var(--radius-md);
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
  -webkit-appearance: none;
  appearance: none;
}

.field textarea {
  resize: vertical;
  min-height: 80px;
  font-family: var(--font-mono);
  font-size: 14px;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 15%, transparent);
}

.field-hint {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 6px;
  min-height: 16px;
}

.field-hint.error { color: var(--danger); }
.field-hint.ok { color: var(--success); }

.field input[type="color"] {
  width: 100%;
  height: 44px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  cursor: pointer;
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
}

/* ========== Type Grid ========== */
.type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
}

.type-chip {
  position: relative;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 10px 6px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
}

.type-chip:hover {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.type-chip.active {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.type-chip .chip-icon {
  display: block;
  margin: 0 auto 4px;
  width: 24px;
  height: 24px;
  opacity: 0.8;
}

.type-chip .chip-icon.active { opacity: 1; }

.type-chip .chip-cat {
  display: block;
  font-size: 9px;
  font-weight: 500;
  opacity: 0.6;
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ========== Preview ========== */
.preview-wrap {
  margin: 16px 0;
}

.preview-card {
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  text-align: center;
}

.preview-area {
  background: white;
  border-radius: var(--radius-md);
  padding: 24px 16px;
  min-height: 200px;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.preview-empty {
  color: var(--text-muted);
  text-align: center;
}

.preview-empty svg { color: var(--text-muted); opacity: 0.5; margin: 0 auto 12px; display: block; }
.preview-empty p { margin: 0; font-size: 13px; }

.preview-content svg,
.preview-content canvas,
.preview-content img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
}

.preview-large svg,
.preview-large canvas,
.preview-large img {
  width: 100%;
  max-width: 360px;
}

.preview-caption {
  margin-top: 12px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-2);
  word-break: break-all;
  background: var(--surface-3);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
}

/* ========== Advanced ========== */
.advanced {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
}

.advanced summary {
  padding: 12px 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  user-select: none;
}

.advanced summary:hover { color: var(--primary); }

.advanced-body {
  padding: 0 14px 14px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.advanced-body .field { margin: 0; }
.advanced-body .field label { font-size: 11px; margin-bottom: 4px; }

/* ========== Buttons ========== */
.actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.btn {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 16px;
  border: none;
  border-radius: var(--radius-md);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  min-height: 48px;
}

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

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

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

.btn-secondary {
  background: var(--surface-3);
  color: var(--text);
  border: 1.5px solid var(--border);
}

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

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

.btn-ghost:hover { background: var(--surface-3); }

.btn-sm {
  padding: 8px 12px;
  min-height: 36px;
  font-size: 13px;
  flex: 0 0 auto;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-danger:hover { background: var(--danger-hover); }

/* ========== History ========== */
.history-toolbar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  position: sticky;
  top: calc(var(--header-h) + var(--tab-h) + var(--safe-top));
  z-index: 80;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-wrap svg {
  position: absolute;
  left: 12px;
  color: var(--text-3);
  pointer-events: none;
}

.search-wrap input {
  width: 100%;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 12px 12px 12px 38px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-family: inherit;
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 15%, transparent);
}

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

.filter-wrap select {
  flex: 1;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-3) 50%),
    linear-gradient(135deg, var(--text-3) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 28px;
}

.history-list {
  display: grid;
  gap: 12px;
}

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px;
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: 12px;
  align-items: center;
  transition: box-shadow 0.15s;
}

.history-item:hover {
  box-shadow: var(--shadow-md);
}

.history-item:active {
  transform: scale(0.98);
}

.history-item .thumb {
  background: white;
  border-radius: var(--radius-sm);
  padding: 6px;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  overflow: hidden;
}

.history-item .thumb svg,
.history-item .thumb canvas,
.history-item .thumb img {
  max-width: 100%;
  max-height: 100%;
}

.history-item .info { min-width: 0; }

.history-item .info-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
  font-family: var(--font-mono);
}

.history-item .info-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  align-items: center;
  font-size: 11px;
  color: var(--text-3);
}

.history-item .info-meta .type-badge {
  background: var(--primary-soft);
  color: var(--primary);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.history-item .info-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.history-item .tag {
  background: var(--surface-3);
  color: var(--text-2);
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 500;
}

.history-item .topic {
  color: var(--accent);
  font-weight: 600;
}

.history-item .actions-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history-item .icon-btn {
  width: 32px;
  height: 32px;
}

.history-item .icon-btn.delete { color: var(--danger); }
.history-item .icon-btn.delete:hover { background: color-mix(in srgb, var(--danger) 15%, transparent); }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state svg {
  opacity: 0.4;
  margin: 0 auto 16px;
  display: block;
}

.empty-state h3 {
  font-size: 16px;
  margin: 0 0 6px;
  color: var(--text-2);
}

.empty-state p { font-size: 13px; margin: 0; }

/* ========== Modal ========== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: end center;
  align-items: safe-end center;
  padding: 0;
}

.modal[hidden] { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  animation: fadeIn 0.2s ease;
}

.modal-card {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  width: 100%;
  max-width: var(--max-w);
  max-height: 90vh;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s ease;
  padding-bottom: var(--safe-bottom);
}

.modal-card-lg { max-width: var(--max-w); }

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

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

.modal-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

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

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.view-meta {
  margin-top: 12px;
  font-size: 13px;
  color: var(--text-2);
}

.view-meta .meta-row {
  display: flex;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.view-meta .meta-label {
  flex: 0 0 80px;
  color: var(--text-3);
  font-weight: 500;
}

.view-meta .meta-value {
  flex: 1;
  word-break: break-all;
  font-family: var(--font-mono);
  font-size: 13px;
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 80px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 2000;
  animation: toastIn 0.25s ease;
  max-width: calc(100% - 32px);
}

.toast[hidden] { display: none; }

.toast.success { background: var(--success); color: white; }
.toast.error { background: var(--danger); color: white; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ========== Install Banner ========== */
.install-banner {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 16px);
  left: 16px;
  right: 16px;
  max-width: calc(var(--max-w) - 32px);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 20px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: white;
  border: none;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  z-index: 500;
  animation: toastIn 0.3s ease;
}

.install-banner[hidden] { display: none; }

.install-banner:hover {
  background: var(--primary-hover);
}

/* ========== Help Page ========== */
.help-page .app-main { padding-bottom: calc(var(--safe-bottom) + 24px); }

.help-card h2 {
  font-size: 18px;
  margin: 0 0 12px;
}

.install-step {
  display: grid;
  grid-template-columns: 32px 1fr;
  gap: 12px;
  margin-bottom: 16px;
  align-items: start;
}

.install-step .step-num {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: white;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
}

.install-step strong {
  display: block;
  font-size: 14px;
  color: var(--text);
  margin-bottom: 4px;
}

.install-step p {
  margin: 0;
  font-size: 13px;
  color: var(--text-2);
  line-height: 1.5;
}

.type-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.type-list li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-2);
}

.type-list li:last-child { border-bottom: none; }

.type-list strong { color: var(--text); }

.help-page .card h2:first-child { margin-top: 0; }
.help-page .card h3 {
  font-size: 15px;
  margin: 18px 0 8px;
  color: var(--primary);
}
.help-page .card p,
.help-page .card ol,
.help-page .card ul { font-size: 14px; line-height: 1.7; color: var(--text-2); }
.help-page .card ol { padding-left: 22px; }
.help-page .card li { margin-bottom: 6px; }

/* ========== Responsive ========== */
@media (min-width: 600px) {
  :root { --header-h: 60px; }
  .app-main { padding: 20px 16px calc(var(--safe-bottom) + 28px + var(--tab-h)); }
  .type-grid { grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); }
  .actions { gap: 12px; }
  .btn { padding: 14px 20px; font-size: 16px; }
  .modal { place-items: center; padding: 20px; }
  .modal-card { border-radius: var(--radius-xl); max-height: 88vh; }
  .modal { align-items: center; }
}

@media (min-width: 720px) {
  .app-main { padding: 24px 20px calc(var(--safe-bottom) + 32px + var(--tab-h)); }
  .card { padding: 22px; }
}

@supports (height: 100dvh) {
  html, body, #app, .help-page, .detail-page {
    min-height: 100dvh;
  }
}

.field input,
.field textarea,
.field select { font-size: 16px; }
@media (min-width: 600px) {
  .field input,
  .field textarea,
  .field select { font-size: 15px; }
}
