:root {
  --bg: #f5f6f8; --card: #fff; --fg: #1b1f24; --muted: #667085; --line: #e3e6ea;
  --accent: #2563eb; --accent-fg: #fff; --live: #16a34a; --die: #dc2626;
  --radius: 12px; --pad: 20px; --tap: 36px; --field-font: 13px;
  font-family: system-ui, "Noto Sans Thai", sans-serif;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
[data-theme="dark"] {
  --bg: #0f1216; --card: #171b21; --fg: #e6e8eb; --muted: #98a2b3; --line: #2a3038;
  --accent: #3b82f6;
}
* { box-sizing: border-box; }
html { scrollbar-gutter: stable; }
body { margin: 0; background: var(--bg); color: var(--fg); line-height: 1.5; overflow-wrap: anywhere; }
code { font-size: .92em; word-break: break-all; }

/* Header: brand | nav | theme — nav becomes its own scrollable row on small screens */
header {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px 16px;
  padding: 10px max(var(--pad), env(safe-area-inset-right)) 10px max(var(--pad), env(safe-area-inset-left));
  background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
header .brand { font-weight: 700; font-size: 18px; color: var(--fg); text-decoration: none; white-space: nowrap; }
header nav { display: flex; gap: 4px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
header nav::-webkit-scrollbar { display: none; }
header nav a { color: var(--muted); text-decoration: none; padding: 6px 12px; border-radius: 8px; white-space: nowrap; min-height: var(--tap); display: inline-flex; align-items: center; }
header nav a.active, header nav a:hover { background: var(--bg); color: var(--fg); }
#theme { min-width: var(--tap); padding: 6px 10px; }

main { max-width: 1200px; margin: 0 auto; padding: var(--pad); padding-bottom: calc(var(--pad) + env(safe-area-inset-bottom)); }
h1 { font-size: clamp(19px, 2.4vw + 10px, 24px); margin: 0 0 4px; line-height: 1.3; }
.sub { color: var(--muted); margin: 0 0 18px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }

textarea, input, select { width: 100%; font: var(--field-font)/1.5 ui-monospace, Menlo, monospace; color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-height: var(--tap); }
textarea { min-height: 220px; resize: vertical; }
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; margin: 0; accent-color: var(--accent); vertical-align: middle; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; min-height: var(--tap); cursor: pointer; }
button { font: inherit; border: 1px solid var(--line); background: var(--card); color: var(--fg); padding: 7px 14px; border-radius: 8px; cursor: pointer; min-height: var(--tap); touch-action: manipulation; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button:disabled { opacity: .5; cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.opts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr)); gap: 10px; margin: 12px 0; }
.opts .opt-check { display: flex; align-items: flex-end; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.tabs button { white-space: nowrap; }
.tabs button.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.count { font-size: 12px; color: var(--muted); }
.progress { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; margin: 12px 0 4px; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.live { color: var(--live); } .die { color: var(--die); }
.note { font-size: 12px; color: var(--muted); margin-top: 12px; }
.home-card { text-decoration: none; color: inherit; transition: border-color .15s; }
.home-card:hover { border-color: var(--accent); }

/* Touch devices: bigger targets everywhere */
@media (pointer: coarse) { :root { --tap: 44px; } }

/* Tablet & below */
@media (max-width: 860px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
}

/* Smaller screens: one swipeable tool row and fields that do not trigger iOS zoom */
@media (max-width: 900px) {
  :root { --field-font: 16px; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; scroll-snap-type: x proximity; scrollbar-width: none;
          margin: 0 calc(-1 * var(--pad)) 12px; padding: 2px var(--pad);
          mask-image: linear-gradient(90deg, transparent 0, #000 var(--pad), #000 calc(100% - 24px), transparent); }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { flex: none; scroll-snap-align: start; }
}

/* Phones */
@media (max-width: 640px) {
  :root { --pad: 12px; --tap: 44px; }
  header { grid-template-columns: 1fr auto; padding-top: 8px; padding-bottom: 6px; position: static; }
  header nav { grid-column: 1 / -1; grid-row: 2; margin: 0 -4px; }
  header .brand { font-size: 17px; }
  .card { padding: 12px; }
  textarea { min-height: 160px; }
  .sub { font-size: 14px; margin-bottom: 12px; }
  /* Action buttons: full-width 2-col grid */
  .actions { display: grid; grid-template-columns: 1fr 1fr; }
  .actions .primary { grid-column: 1 / -1; }
  .actions button { width: 100%; }
  .actions #swap { grid-column: 1 / -1; order: 1; } /* long label → own full row, below copy/clear */
  .check-controls { display: grid; grid-template-columns: 1fr 1fr; }
  .check-controls .primary, .check-controls label.inline:last-child { grid-column: 1 / -1; }
  .check-controls button { width: 100%; }
  .card .row > button { flex: 1 1 auto; }
}

/* Very large screens: roomier input areas */
@media (min-width: 1440px) { main { max-width: 1320px; } textarea { min-height: 300px; } }

/* Browser-only TOTP */
.authenticator-card { max-width: 760px; margin: 0 auto; }
.authenticator-card #secrets { min-height: 140px; }
.authenticator-card .authenticator-actions { margin: 12px 0 18px; }
.authenticator-card .totp-output { min-height: 74px; font-size: clamp(24px, 6vw, 36px); font-weight: 700; letter-spacing: .16em; text-align: center; }
.authenticator-card .remaining { min-height: 1.5em; text-align: center; font-variant-numeric: tabular-nums; }
.authenticator-card #line-errors { color: var(--die); font-size: 13px; padding-left: 24px; }
.authenticator-card #line-errors:empty { display: none; }
.authenticator-card #status { min-height: 1.5em; }
.security-note { max-width: 760px; margin: 14px auto 0; }
