:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --border: #e3e8f0;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 4px 14px rgba(0, 0, 0, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; }
.brand .logo { font-size: 24px; }

.nav { display: flex; gap: 4px; }
.nav a {
  padding: 8px 16px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
}
.nav a.active, .nav a:hover { background: #eef2ff; color: var(--primary); text-decoration: none; }

.wrap { max-width: 1180px; margin: 24px auto; padding: 0 20px; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  align-items: start;
  grid-template-areas:
    "input preview"
    "settings preview";
}
.layout > .card { margin-bottom: 0; }
.input-card { grid-area: input; }
.preview-card { grid-area: preview; }
.settings-card { grid-area: settings; }
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "input" "preview" "settings"; }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card h2 { margin: 0 0 14px; font-size: 16px; }
.card h3 { margin: 0 0 10px; font-size: 14px; color: var(--muted); font-weight: 600; }

.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.tabs button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--muted);
  transition: all 0.15s;
}
.tabs button:hover { border-color: var(--primary); color: var(--primary); }
.tabs button.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--muted); }
.field .req { color: #dc2626; }

input[type="text"], input[type="url"], input[type="tel"], input[type="number"], input[type="email"],
textarea, select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, textarea:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
textarea { resize: vertical; min-height: 80px; }

.row { display: flex; gap: 12px; }
.row > .field { flex: 1; }

.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}
.btn:hover { background: var(--primary-dark); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-outline { background: #fff; color: var(--primary); border: 1px solid var(--primary); }
.btn-outline:hover { background: #eef2ff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

/* Preview */
.preview-card { position: sticky; top: 20px; align-self: start; }
@media (max-width: 900px) { .preview-card { position: static; } }

.preview-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
#qrcanvas {
  width: 100% !important;
  max-width: 400px;
  height: auto !important;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
}

.dl-btns { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

.setting-section { border-top: 1px solid var(--border); padding-top: 16px; margin-top: 16px; }
.setting-section:first-child { border-top: none; margin-top: 0; padding-top: 0; }

.settings-card { margin-top: 0; }
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}
@media (max-width: 1100px) { .settings-grid { grid-template-columns: 1fr; } }
.settings-grid .setting-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8fafc;
  padding: 16px;
  margin: 0;
}
.settings-grid .span-all { grid-column: 1 / -1; }
.settings-grid .frame-grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
.settings-grid .span-all .row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.settings-grid .span-all .row > .field { flex: 1 1 180px; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.active { background: #eef2ff; border-color: var(--primary); color: var(--primary); font-weight: 600; }

.color-row { display: flex; flex-wrap: wrap; gap: 12px; }
.color-item { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.color-item input[type="color"] {
  width: 46px; height: 34px; padding: 2px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; cursor: pointer;
}

.switch { display: flex; align-items: center; gap: 10px; font-size: 14px; cursor: pointer; }
.switch input { width: auto; }

.frame-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 10px; }
.frame-item {
  border: 2px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  background: #fff;
  transition: border-color 0.15s;
}
.frame-item img, .frame-item .frame-none { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; }
.frame-item .frame-none { display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); background: #fafafa; }
.frame-item:hover { border-color: var(--primary); }
.frame-item.active { border-color: var(--primary); box-shadow: 0 0 0 2px #bfdbfe; }
.frame-item .tag { position: absolute; top: 4px; left: 4px; font-size: 10px; background: rgba(0,0,0,0.55); color: #fff; padding: 1px 6px; border-radius: 999px; }

.radio-line { display: flex; flex-wrap: wrap; gap: 14px; font-size: 14px; align-items: center; }
.radio-line label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; color: var(--text); }

/* Scanner page */
.scan-box { text-align: center; }
#scanner-video {
  width: 100%;
  max-width: 480px;
  border-radius: 12px;
  background: #000;
}
.scan-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 16px 0; }
.result-box {
  max-width: 480px;
  margin: 16px auto;
  padding: 16px;
  border: 1px dashed var(--primary);
  border-radius: 10px;
  background: #f0f6ff;
  word-break: break-all;
  display: none;
  text-align: left;
}
.result-box.show { display: block; }
.result-box .res-label { font-size: 12px; color: var(--primary); font-weight: 700; margin-bottom: 4px; }
.result-box .res-type { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.result-box .res-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
#history { list-style: none; padding: 0; margin: 0; text-align: left; }
#history li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px;
}
#history li:last-child { border-bottom: none; }
#history .htext { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }

.status { font-size: 13px; color: var(--muted); margin-top: 10px; min-height: 20px; }

.toast {
  position: fixed; left: 50%; bottom: 30px; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 20px; border-radius: 8px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 0.25s; z-index: 99;
}
.toast.show { opacity: 0.92; }

footer { text-align: center; color: var(--muted); font-size: 13px; padding: 24px 16px 40px; }
