/* ==========================================================================
   EPC Tag Writer — handheld-first UI (v0.2, tabbed)
   Target: HKRUR-C72E / Chainway C72, Android 11, Chrome 94, 720x1280.
   Constraints kept on purpose:
     - no dvh / svh units, no :has(), no container queries (Chrome 94)
     - no external fonts or assets: the page must work from a USB stick
   ========================================================================== */

:root {
  --bg: #f3f4f9;
  --surface: #ffffff;
  --surface-2: #fafbfd;
  --border: #e3e6ee;
  --border-strong: #cfd4e0;
  --text: #14161f;
  --muted: #616b82;
  --faint: #98a1b3;
  --accent: #4f46e5;
  --accent-ink: #3730a3;
  --accent-soft: #eef0ff;
  --green: #087f5b;
  --green-soft: #e8f8f1;
  --amber: #a15c07;
  --amber-soft: #fff6e5;
  --red: #b42318;
  --red-soft: #fdecea;
  --blue: #1257a8;
  --blue-soft: #eaf2fd;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
    "Microsoft JhengHei", "PingFang TC", sans-serif;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 22, 31, 0.05), 0 10px 28px -20px rgba(20, 22, 31, 0.35);
  --top-h: 56px;
  --tab-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tab-h) + var(--safe-b));
}

h1, h2, h3, h4 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 17px; }
h2 { font-size: 15.5px; }
p { margin: 0; }
code, .mono { font-family: var(--mono); }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.tiny { font-size: 12px; color: var(--faint); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--top-h);
  padding: 0 12px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 9px; flex: 1 1 auto; min-width: 0; }
.logo {
  flex: none;
  width: 32px; height: 32px;
  border-radius: 9px;
  background: linear-gradient(150deg, #6366f1, #4338ca);
  color: #fff;
  display: grid; place-items: center;
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.brand-txt { min-width: 0; }
.brand-txt .sub { font-size: 10.5px; color: var(--faint); letter-spacing: 0.02em; white-space: nowrap; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  max-width: 46vw;
  min-width: 0;
}
.pill:active { background: #eef0ff; }
.pill #conn-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.on { background: var(--green); box-shadow: 0 0 0 3px rgba(8, 127, 91, 0.15); }
.dot.busy { background: var(--amber); }
.dot.err { background: var(--red); }

.icon-btn {
  flex: none;
  width: 38px; height: 38px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 16px;
  color: var(--muted);
  display: grid; place-items: center;
}

/* ---------- panels ---------- */
.app {
  padding: 12px 12px 8px;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - var(--top-h) - var(--tab-h) - var(--safe-b));
}
.tabpanel { display: none; }
.tabpanel.active { display: flex; flex-direction: column; flex: 1 1 auto; }
#panel-list.active, #panel-log.active { display: block; }
#steps-block[hidden] { display: none; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
}
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(180deg, #5b54ea, #4338ca);
  border-color: #3c34b4;
  color: #fff;
  box-shadow: 0 12px 24px -16px rgba(67, 56, 202, 0.95);
}
.btn.xl { min-height: 62px; font-size: 17.5px; border-radius: 15px; width: 100%; }
.btn.sm { min-height: 40px; font-size: 13.5px; padding: 0 12px; border-radius: 10px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent-ink); }
.btn.danger { color: var(--red); border-color: #f3c3bd; background: var(--red-soft); }
.btn.icon { width: 52px; padding: 0; font-size: 16px; }
.btn:disabled { opacity: 0.45; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.link-btn {
  border: none;
  background: none;
  color: var(--accent-ink);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 2px;
}

/* ---------- write: empty ---------- */
.empty-state { text-align: center; padding: 46px 16px 40px; margin: auto 0; }
.empty-ico {
  width: 64px; height: 64px;
  margin: 0 auto 16px;
  border-radius: 18px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  display: grid; place-items: center;
}
.empty-ico svg { width: 32px; height: 32px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linejoin: round; }

.progress-card {
  max-width: 320px;
  margin: 0 auto 18px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  text-align: left;
}
.pg-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; font-size: 12.5px; color: var(--muted); }
.pg-top b { color: var(--text); font-family: var(--mono); }
.pg-bar { height: 8px; border-radius: 999px; background: #eceef5; margin-top: 9px; overflow: hidden; }
.pg-bar i { display: block; height: 100%; width: 0; border-radius: 999px; background: linear-gradient(90deg, #6366f1, #4338ca); transition: width 0.35s ease; }
.empty-actions { display: flex; gap: 8px; max-width: 320px; margin: 0 auto; }
.empty-actions .btn { flex: 1 1 0; min-height: 52px; }
.empty-title { font-size: 17px; font-weight: 700; }
.empty-sub { font-size: 13.5px; color: var(--muted); margin: 6px 0 20px; }

/* ---------- write: target ---------- */
#target-card { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
#target-card[hidden] { display: none; }
.target-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.target-id { min-width: 0; }
.target-code { font-family: var(--mono); font-size: 23px; font-weight: 700; letter-spacing: -0.02em; word-break: break-all; }
.target-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.hex-box {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #101322;
  color: #dfe3ff;
  font-family: var(--mono);
  font-size: 13px;
  word-break: break-all;
  line-height: 1.5;
}
.hex-box .lbl { display: block; font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: #8b93c9; margin-bottom: 4px; }
.hex-meta { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 8px; }
.hex-meta b { color: var(--text); font-family: var(--mono); font-weight: 600; }

.badge {
  flex: none;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge.todo { color: var(--muted); background: #f1f3f7; border-color: #e4e7ef; }
.badge.written { color: var(--green); background: var(--green-soft); border-color: #b7e6d3; }
.badge.uncertain { color: var(--amber); background: var(--amber-soft); border-color: #f5d9ab; }
.badge.failed { color: var(--red); background: var(--red-soft); border-color: #f5c7c2; }
.badge.writing { color: var(--blue); background: var(--blue-soft); border-color: #c3daf7; }
.badge.dup { color: var(--amber); background: var(--amber-soft); border-color: #f5d9ab; }

.notice {
  display: flex;
  gap: 10px;
  padding: 11px 13px;
  border-radius: 11px;
  font-size: 13px;
  line-height: 1.55;
  margin-top: 10px;
  border: 1px solid;
}
.notice.warn { background: var(--amber-soft); border-color: #f3d8a8; color: #7a4405; }
.notice.info { background: var(--blue-soft); border-color: #c9def7; color: #123f70; }
.notice.ok { background: var(--green-soft); border-color: #b7e6d3; color: #065f46; }
.notice.err { background: var(--red-soft); border-color: #f5c7c2; color: #8a1c12; }
.notice .ico { flex: none; font-weight: 700; }

.scheme-warning { margin: 0 0 12px; }
.scheme-warning[hidden] { display: none; }
.scheme-warning code { font-family: var(--mono); font-size: 11.5px; background: rgba(0,0,0,0.06); padding: 1px 4px; border-radius: 4px; }

.pre-run { list-style: none; margin: 18px 0 0; padding: 0; }
.pre-run li {
  position: relative;
  padding: 0 0 10px 22px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
}
.pre-run li::before {
  content: "";
  position: absolute;
  left: 4px; top: 7px;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #c7c9fb;
}
.pre-run[hidden] { display: none; }

/* ---------- steps ---------- */
.steps-head { display: flex; align-items: center; justify-content: space-between; margin: 18px 0 2px; }
.steps {
  list-style: none;
  margin: 0;
  padding: 0 0 6px;
  max-height: 208px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-mask-image: linear-gradient(180deg, #000 0, #000 calc(100% - 14px), transparent 100%);
  mask-image: linear-gradient(180deg, #000 0, #000 calc(100% - 14px), transparent 100%);
}
.steps.collapsed { display: none; }
.steps li {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: 9px;
  padding: 5px 0;
  position: relative;
}
.steps li:not(:last-child)::before {
  content: "";
  position: absolute;
  left: 11px; top: 26px; bottom: -2px;
  width: 2px;
  background: var(--border);
}
.steps .mk {
  width: 24px; height: 24px;
  border-radius: 7px;
  display: grid; place-items: center;
  font-size: 11px;
  font-family: var(--mono);
  font-weight: 700;
  background: #f1f3f7;
  color: var(--faint);
  border: 1px solid var(--border);
  z-index: 1;
}
.steps li.running .mk { background: var(--blue-soft); color: var(--blue); border-color: #c3daf7; }
.steps li.ok .mk { background: var(--green-soft); color: var(--green); border-color: #b7e6d3; }
.steps li.warn .mk { background: var(--amber-soft); color: var(--amber); border-color: #f3d8a8; }
.steps li.failed .mk { background: var(--red-soft); color: var(--red); border-color: #f5c7c2; }
.steps .lbl { font-size: 13.5px; font-weight: 600; line-height: 1.35; }
.steps .det { font-family: var(--mono); font-size: 11px; color: var(--muted); word-break: break-all; line-height: 1.4; }

/* ---------- result ---------- */
.result { margin-top: 14px; border-radius: 13px; padding: 14px 16px; border: 1px solid; }
.result h3 { font-size: 16px; margin-bottom: 3px; }
.result .msg { font-size: 13.5px; line-height: 1.55; }
.result .kv { font-family: var(--mono); font-size: 11px; margin-top: 8px; color: #4a5163; word-break: break-all; }
.result.ok { background: var(--green-soft); border-color: #b7e6d3; }
.result.warn { background: var(--amber-soft); border-color: #f3d8a8; }
.result.err { background: var(--red-soft); border-color: #f5c7c2; }

/* ---------- action bar (sticky above the tab bar) ---------- */
.action-bar {
  position: sticky;
  bottom: calc(var(--tab-h) + var(--safe-b) + 6px);
  display: grid;
  gap: 8px;
  margin-top: 16px;
  padding: 10px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  box-shadow: 0 -6px 20px -18px rgba(20, 22, 31, 0.5);
}
.action-bar { margin-top: auto; }
.action-row { display: flex; gap: 8px; }
.action-row .btn { flex: 1 1 0; }

/* ---------- list ---------- */
.list-head { position: sticky; top: var(--top-h); z-index: 20; background: var(--bg); padding: 2px 0 8px; }
.search-row { display: flex; gap: 8px; }
#qr-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 50px;
  padding: 0 14px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 15px;
}
#qr-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
  min-height: 34px;
}
.chip.active { background: var(--accent-soft); border-color: #c7c9fb; color: var(--accent-ink); }
.chip .n { font-family: var(--mono); font-size: 11.5px; opacity: 0.75; margin-left: 4px; }
.list-count { font-size: 12px; color: var(--faint); margin-top: 8px; }

.code-list { list-style: none; margin: 0; padding: 0; }
.code-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 64px;
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 7px;
}
.code-item:active { background: var(--accent-soft); }
.code-item.sel { border-color: #c7c9fb; background: var(--accent-soft); }
.code-item .txt { min-width: 0; flex: 1 1 auto; }
.code-item .c { font-family: var(--mono); font-size: 15px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.code-item .e { font-family: var(--mono); font-size: 10.5px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty-list { padding: 34px 8px; text-align: center; color: var(--faint); font-size: 14px; }

/* ---------- log ---------- */
.log-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.log {
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.7;
  background: #0f1220;
  color: #c8cdf0;
  border-radius: 12px;
  padding: 12px;
  min-height: 60vh;
  overflow-wrap: break-word;
}
.log div { white-space: pre-wrap; }
.log .t { color: #6f79b4; }
.log .ok { color: #7ee2b8; }
.log .warn { color: #f4c774; }
.log .err { color: #ff9b90; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tab-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}
.tab {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: none;
  color: var(--faint);
  font-size: 11px;
  font-weight: 600;
  padding: 0;
}
.tab svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent-ink); }
.tab.active::before {
  content: "";
  position: absolute;
  top: 0; left: 50%;
  width: 34px; height: 3px;
  margin-left: -17px;
  border-radius: 0 0 3px 3px;
  background: var(--accent);
}
.tab em {
  position: absolute;
  top: 6px;
  left: 50%;
  margin-left: 9px;
  font-style: normal;
  font-family: var(--mono);
  font-size: 9.5px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  border-radius: 999px;
  padding: 1px 5px;
  min-width: 17px;
  text-align: center;
}

/* ---------- dialogs ---------- */
dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  width: min(560px, calc(100vw - 20px));
  max-height: 88vh;
  box-shadow: 0 30px 70px -30px rgba(10, 12, 20, 0.55);
  color: var(--text);
  background: var(--surface);
}
dialog::backdrop { background: rgba(15, 18, 32, 0.45); }
.dlg-head { padding: 16px 18px 4px; }
.dlg-body { padding: 4px 18px 16px; overflow-y: auto; max-height: calc(88vh - 130px); }
.dlg-foot {
  padding: 12px 18px calc(12px + var(--safe-b));
  display: flex; gap: 8px; justify-content: flex-end;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
}
.section-title { font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--faint); font-weight: 700; margin: 18px 0 8px; }
.row { display: flex; gap: 8px; }
#bridge-url {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 46px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 11px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 13.5px;
}
.diag-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 8px; }
.hint code { font-family: var(--mono); font-size: 11.5px; background: #eef0f6; padding: 1px 4px; border-radius: 4px; }

.field { margin: 12px 0; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.field .sub, .switch .sub { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.field input[type="text"], .field input[type="number"], .field select {
  width: 100%;
  min-height: 46px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 14px;
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.range-row { display: flex; align-items: center; gap: 12px; }
.range-row output { font-family: var(--mono); font-weight: 700; min-width: 62px; text-align: right; }
.switch { display: flex; align-items: flex-start; gap: 10px; padding: 9px 0; }
.switch input { margin-top: 3px; width: 19px; height: 19px; accent-color: var(--accent); flex: none; }
.switch .lbl { font-size: 13.5px; font-weight: 600; }
.build { font-size: 11px; color: var(--faint); margin-top: 18px; }

.cf-msg .cf-line { position: relative; padding-left: 16px; margin-bottom: 7px; font-size: 14px; line-height: 1.6; }
.cf-msg .cf-line::before { content: "•"; position: absolute; left: 2px; top: 0; color: var(--amber); font-weight: 700; }
.cf-detail { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-top: 10px; word-break: break-all; }

/* ---------- small phones ---------- */
@media (max-width: 380px) {
  .brand-txt .sub { display: none; }
  .target-code { font-size: 20px; }
  .chip { padding: 5px 9px; font-size: 12px; }
}

/* ---------- tablets / desk ---------- */
@media (min-width: 760px) {
  .app { display: block; min-height: 0; max-width: 760px; margin: 0 auto; padding: 16px 16px 8px; }
  .action-bar { position: static; box-shadow: none; background: transparent; border: none; padding: 0; margin-top: 18px; }
  .log { min-height: 40vh; }
}
