:root {
  --bg: #EEF2EF;
  --surface: #FFFFFF;
  --surface-2: #F6F8F7;
  --ink: #15211B;
  --ink-2: #5B6B62;
  --line: #D5DDD8;
  --accent: #0E8A5A;
  --accent-ink: #FFFFFF;
  --accent-soft: #DCF0E5;
  --warn: #9A5B00;
  --err: #B42318;
  --bar-bg: rgba(238, 242, 239, 0.86);
  --round: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, "Segoe UI", sans-serif;
  --body: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  --tabbar-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #141A17;
    --surface-2: #1C2420;
    --ink: #EDF3EF;
    --ink-2: #93A39A;
    --line: #27312C;
    --accent: #3CCB8B;
    --accent-ink: #03140B;
    --accent-soft: #11301F;
    --warn: #F0B35A;
    --err: #FF8A80;
    --bar-bg: rgba(0, 0, 0, 0.82);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; scroll-padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); scroll-padding-top: 70px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.4 var(--body);
  min-height: 100vh;
  overscroll-behavior-y: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 28px);
}

/* ---------- Headers ---------- */
.large-header { padding: calc(env(safe-area-inset-top) + 18px) 4px 8px; }
.large-header.row { display: flex; align-items: flex-end; justify-content: space-between; }
.large-header h1 {
  font: 700 34px/1.1 var(--round);
  letter-spacing: -0.02em;
  margin: 0;
}
.large-header .sub { margin: 6px 0 0; color: var(--ink-2); font-size: 16px; }
.group-title { font: 650 20px/1.2 var(--round); margin: 26px 4px 10px; letter-spacing: -0.01em; }

/* ---------- Tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.tile {
  background: var(--surface);
  border-radius: 18px;
  padding: 16px 10px 13px;
  display: flex; flex-direction: column; align-items: center; gap: 9px;
  text-align: center;
  transition: transform .12s ease;
}
.tile:active { transform: scale(.96); }
.tile-icon { width: 30px; height: 30px; color: var(--accent); stroke-width: 1.8; }
.tile-name { font: 600 14.5px/1.2 var(--round); }

.tiles-bar { grid-template-columns: repeat(2, 1fr); }
.tile-bar { align-items: stretch; text-align: left; padding: 12px 12px 13px; gap: 4px; }
.tile-code {
  width: 100%; height: 46px;
  background: #FFFFFF;
  border-radius: 8px;
  padding: 6px 4px;
  object-fit: fill;
  margin-bottom: 6px;
  box-shadow: inset 0 0 0 1px var(--line);
}
.tile-bar .tile-name { font-size: 15.5px; }
.tile-note { font-size: 12.5px; color: var(--ink-2); line-height: 1.25; }

/* ---------- Generator nav ---------- */
.nav {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  margin: 0 -16px; padding: calc(env(safe-area-inset-top) + 6px) 8px 8px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(1.6) blur(18px); backdrop-filter: saturate(1.6) blur(18px);
}
.nav-back { display: inline-flex; align-items: center; gap: 0; color: var(--accent); font-size: 17px; padding: 8px 6px; justify-self: start; }
.nav-back svg { width: 26px; height: 26px; stroke-width: 2.2; }
.nav-title { font: 650 17px/1.2 var(--round); margin: 0; text-align: center; }

/* ---------- Preview ---------- */
.preview-wrap { margin-top: 8px; }
.preview {
  background: #FFFFFF;
  border-radius: 24px;
  min-height: 200px;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  box-shadow: 0 0 0 1px var(--line);
}
.preview canvas.is-qr { width: min(100%, 260px); height: auto; image-rendering: pixelated; }
.preview canvas.is-bar { width: 100%; max-width: 520px; height: auto; }
.preview-empty { color: #6B7A72; margin: 0; text-align: center; font-size: 15px; max-width: 220px; }
.status { min-height: 22px; margin: 8px 6px 4px; font-size: 14px; color: var(--ink-2); }
.status.warn { color: var(--warn); }
.status.err { color: var(--err); }

/* ---------- Form ---------- */
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 10px; margin-top: 14px; }
.field { grid-column: span 2; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.field.half { grid-column: span 1; }
.field > span { font-size: 14px; font-weight: 600; color: var(--ink-2); padding-left: 4px; }
.field small { font-size: 13px; color: var(--ink-2); padding-left: 4px; }
input[type=text], input[type=url], input[type=tel], input[type=email], input[type=datetime-local], textarea, select {
  width: 100%;
  font: 17px/1.35 var(--body);   /* 17px stops iPhone from zooming in */
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 13px;
  -webkit-appearance: none; appearance: none;
  min-height: 48px;
  min-width: 0; max-width: 100%;
}
input[type=datetime-local] { display: block; text-align: left; }
textarea { resize: vertical; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235B6B62' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M7 10l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 20px;
  padding-right: 40px;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.field.check {
  flex-direction: row; align-items: center; justify-content: space-between;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px 10px 13px; min-height: 48px;
}
.field.check > span { color: var(--ink); font-size: 16px; font-weight: 400; padding: 0; }
.switch {
  -webkit-appearance: none; appearance: none;
  width: 51px; height: 31px; border-radius: 31px; background: var(--line);
  position: relative; flex: none; margin: 0; cursor: pointer; transition: background .2s;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #FFFFFF; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s;
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(20px); }

/* ---------- Options ---------- */
.options { margin-top: 16px; background: var(--surface); border-radius: 16px; border: 1px solid var(--line); }
.options summary {
  list-style: none; cursor: pointer; padding: 14px 16px; font-weight: 600; font-size: 16px;
  display: flex; justify-content: space-between; align-items: center;
}
.options summary::-webkit-details-marker { display: none; }
.options summary::after { content: ""; width: 9px; height: 9px; border-right: 2px solid var(--ink-2); border-bottom: 2px solid var(--ink-2); transform: rotate(45deg); margin: -4px 4px 0 0; transition: transform .2s; }
.options[open] summary::after { transform: rotate(-135deg); margin-top: 4px; }
#options-body { padding: 2px 16px 16px; display: flex; flex-direction: column; gap: 16px; }
.opt { display: flex; flex-direction: column; gap: 8px; }
.opt.field.check { flex-direction: row; background: var(--surface-2); border: 0; }
.opt-label { font-size: 14px; font-weight: 600; color: var(--ink-2); }
.opt small { font-size: 13px; color: var(--ink-2); }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { padding: 8px 4px; border-radius: 8px; font-size: 14.5px; font-weight: 500; }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 650; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.swatch {
  width: 36px; height: 36px; border-radius: 50%; background: var(--sw);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.18);
  position: relative;
}
.swatch.on::after { content: ""; position: absolute; inset: -5px; border-radius: 50%; border: 2.5px solid var(--accent); }
.swatch.custom {
  background: conic-gradient(#E5484D, #F5D90A, #46A758, #0091FF, #8E4EC6, #E5484D);
  overflow: hidden; cursor: pointer; display: block;
}
.swatch.custom input { opacity: 0; width: 100%; height: 100%; border: 0; padding: 0; cursor: pointer; }
.link-btn { color: var(--accent); font-size: 15px; font-weight: 500; align-self: flex-start; padding: 4px 0; }

/* ---------- Buttons ---------- */
.actions { display: grid; grid-template-columns: 1.15fr 1fr; gap: 10px; margin-top: 6px; }
.actions .btn { font-size: 16px; padding: 0 12px; }
.actions .btn svg { width: 20px; height: 20px; stroke-width: 2; }
.btn {
  min-height: 52px; border-radius: 14px; font-size: 17px; font-weight: 600; padding: 0 18px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:disabled { opacity: .4; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-secondary { background: var(--accent-soft); color: var(--accent); }
.btn-ghost { background: var(--surface); color: var(--accent); border: 1px solid var(--line); min-height: 48px; font-size: 16px; }
.field-btn { grid-column: span 2; }
.btn:not(:disabled):active { filter: brightness(.92); }

/* ---------- History ---------- */
.history { list-style: none; margin: 14px 0 0; padding: 0; background: var(--surface); border-radius: 16px; overflow: hidden; }
.history:empty { display: none; }
.h-item { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.h-item:last-child { border-bottom: 0; }
.h-open { flex: 1; display: flex; align-items: center; gap: 12px; padding: 10px 6px 10px 12px; text-align: left; min-width: 0; }
.h-thumb { width: 56px; height: 56px; border-radius: 10px; background: #FFFFFF; display: flex; align-items: center; justify-content: center; flex: none; box-shadow: inset 0 0 0 1px var(--line); overflow: hidden; }
.h-thumb canvas.is-qr { width: 48px; height: 48px; image-rendering: pixelated; }
.h-thumb canvas.is-bar { width: 50px; height: 30px; }
.h-text { display: flex; flex-direction: column; min-width: 0; }
.h-text strong { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-text span { font-size: 13.5px; color: var(--ink-2); }
.h-del { padding: 14px; color: var(--ink-2); }
.h-del svg { width: 22px; height: 22px; }
.empty { text-align: center; padding: 48px 20px; color: var(--ink-2); }
.empty p { max-width: 280px; margin: 0 auto 18px; }

/* ---------- Labels ---------- */
.lbl-preview-card { margin-top: 14px; background: var(--surface); border-radius: 24px; padding: 14px; box-shadow: 0 0 0 1px var(--line); }
.lbl-previews { display: flex; flex-direction: column; gap: 10px; align-items: center; min-height: 90px; justify-content: center; }
.lbl-canvas { width: 100%; height: auto; background: #FFFFFF; border-radius: 6px; outline: 1.5px dashed #9AA5A0; outline-offset: 0; }
.lbl-preview-card .preview-empty { color: var(--ink-2); }
.lbl-png { margin: 10px 4px 0; }
.seg.seg-2 { grid-template-columns: 1fr 1fr; }
.seg.seg-grid { row-gap: 3px; }
.lbl-group { grid-column: span 2; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 10px; }
.lbl-note { grid-column: span 2; margin: -2px 4px 0; font-size: 13px; color: var(--ink-2); }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center; gap: 0;
  padding: 6px 0 calc(env(safe-area-inset-bottom) + 4px);
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(1.6) blur(18px); backdrop-filter: saturate(1.6) blur(18px);
  border-top: 0.5px solid var(--line);
}
.tab { flex: 0 1 140px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0; color: var(--ink-2); font-size: 11px; font-weight: 500; }
.tab svg { width: 26px; height: 26px; }
.tab.active { color: var(--accent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 18px);
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 15px; font-weight: 500;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 560px) {
  .tiles { grid-template-columns: repeat(5, 1fr); }
  .tiles-bar { grid-template-columns: repeat(3, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
