/* ========================================================
   AirSync Next - Fluid Glass 3 (mobile-first, responsive)
   Phone ≤720px · Tablet 721–1079px · Desktop ≥1080px
   ======================================================== */

:root {
  --accent: #0a6cff;
  --accent-strong: #0057d6;
  --accent-soft: rgba(10, 108, 255, 0.12);
  --ok: #1f9d55;
  --ok-soft: rgba(31, 157, 85, 0.12);
  --warn: #c77700;
  --warn-soft: rgba(255, 159, 10, 0.14);
  --danger: #e5332a;
  --danger-soft: rgba(229, 51, 42, 0.1);

  --bg: #f4f5f8;
  --bg-glow-1: rgba(10, 108, 255, 0.10);
  --bg-glow-2: rgba(90, 200, 250, 0.12);
  --surface: rgba(255, 255, 255, 0.78);
  --surface-solid: #ffffff;
  --surface-2: rgba(242, 243, 247, 0.9);
  --surface-3: #e9ebf0;
  --border: rgba(15, 23, 42, 0.08);
  --border-strong: rgba(15, 23, 42, 0.14);
  --text: #15171c;
  --text-2: #5d6370;
  --text-3: #676d79;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px -8px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 24px 64px -16px rgba(15, 23, 42, 0.35);
  --scrim: rgba(10, 12, 16, 0.45);

  --radius: 20px;
  --radius-sm: 12px;
  --radius-xs: 8px;
  --tap: 44px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --topbar-h: 60px;
  --bottombar-h: 0px;

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #3d8bff;
    --accent-strong: #66a3ff;
    --accent-soft: rgba(61, 139, 255, 0.18);
    --ok: #3ccf7a;
    --ok-soft: rgba(60, 207, 122, 0.15);
    --warn: #ffb340;
    --warn-soft: rgba(255, 179, 64, 0.14);
    --danger: #ff5a52;
    --danger-soft: rgba(255, 90, 82, 0.14);
    --bg: #0b0c0f;
    --bg-glow-1: rgba(61, 139, 255, 0.16);
    --bg-glow-2: rgba(90, 200, 250, 0.08);
    --surface: rgba(28, 30, 36, 0.78);
    --surface-solid: #1a1c21;
    --surface-2: rgba(40, 43, 51, 0.85);
    --surface-3: #2b2e36;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.16);
    --text: #f2f3f5;
    --text-2: #a8adb8;
    --text-3: #878c97;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px -8px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 24px 64px -16px rgba(0, 0, 0, 0.7);
    --scrim: rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --accent: #3d8bff;
  --accent-strong: #66a3ff;
  --accent-soft: rgba(61, 139, 255, 0.18);
  --ok: #3ccf7a;
  --ok-soft: rgba(60, 207, 122, 0.15);
  --warn: #ffb340;
  --warn-soft: rgba(255, 179, 64, 0.14);
  --danger: #ff5a52;
  --danger-soft: rgba(255, 90, 82, 0.14);
  --bg: #0b0c0f;
  --bg-glow-1: rgba(61, 139, 255, 0.16);
  --bg-glow-2: rgba(90, 200, 250, 0.08);
  --surface: rgba(28, 30, 36, 0.78);
  --surface-solid: #1a1c21;
  --surface-2: rgba(40, 43, 51, 0.85);
  --surface-3: #2b2e36;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #f2f3f5;
  --text-2: #a8adb8;
  --text-3: #878c97;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px -8px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 24px 64px -16px rgba(0, 0, 0, 0.7);
  --scrim: rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ================= Base ================= */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; overscroll-behavior-y: none; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(60vmax 60vmax at -10% -10%, var(--bg-glow-1), transparent 60%),
    radial-gradient(60vmax 60vmax at 110% 110%, var(--bg-glow-2), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  overflow-x: hidden;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: calc(var(--bottombar-h) + var(--safe-bottom) + 24px);
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; }
button:disabled { cursor: not-allowed; opacity: 0.5; }
/* Controls fire immediately and never select their label on long-press */
button, label.btn, label.chip-btn, .bar-btn, [role="button"], [role="tab"], summary {
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
a { color: var(--accent); }
code { font-family: var(--mono); font-size: 0.92em; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon.xs { width: 13px; height: 13px; }
.icon.sm { width: 16px; height: 16px; }
.icon.xl { width: 36px; height: 36px; stroke-width: 1.6; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-2); }
.small { font-size: 12.5px; }
.center { text-align: center; justify-content: center; }
.spacer { flex: 1; }

/* ================= Buttons ================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-weight: 600; font-size: 14px; white-space: nowrap;
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.danger { background: var(--danger); border-color: transparent; color: #fff; }
.btn.danger-ghost { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { display: flex; width: 100%; }
.btn.lg { min-height: 48px; font-size: 15px; padding: 0 18px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: var(--tap); height: var(--tap);
  border-radius: 999px;
  color: var(--text);
  transition: background 0.15s, transform 0.12s var(--ease);
}
.icon-btn:active { background: var(--surface-3); }
.icon-btn.danger { color: var(--danger); }

.text-btn { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); font-weight: 600; font-size: 13px; min-height: 32px; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 12px;
  border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 13px; font-weight: 600; cursor: pointer;
  max-width: 100%;
  transition: transform 0.12s var(--ease), background 0.15s;
}
.chip-btn:active { transform: scale(0.97); }
.chip-btn.mono span { font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ================= Top bar ================= */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) max(16px, var(--safe-right)) 0 max(16px, var(--safe-left));
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, #0a6cff, #33b5ff); color: #fff;
  box-shadow: 0 6px 16px -6px rgba(10, 108, 255, 0.6);
}
.brand-mark .icon { width: 20px; height: 20px; stroke-width: 2.4; }
.brand-name { font-weight: 800; font-size: 17px; letter-spacing: -0.02em; white-space: nowrap; }
.brand-name em { font-style: normal; font-weight: 600; color: var(--text-2); }

.status-pill {
  justify-self: center;
  display: inline-flex; align-items: center; gap: 8px;
  max-width: 100%; min-width: 0; height: 36px; padding: 0 14px;
  border-radius: 999px;
  background: #15171c; color: #fff;
  font-size: 13px; font-weight: 600;
  box-shadow: 0 6px 18px -8px rgba(0, 0, 0, 0.5);
  transition: background 0.3s;
}
.status-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #ff9f0a; flex: none; }
.status-pill[data-state="idle"] .status-dot { background: #ff9f0a; animation: pulse 2s infinite; }
.status-pill[data-state="busy"] .status-dot { background: #5ac8fa; animation: pulse 1s infinite; }
.status-pill[data-state="connected"] .status-dot { background: #34c759; }
.status-pill[data-state="offline"] .status-dot { background: #ff453a; }
.status-progress { width: 56px; height: 4px; border-radius: 4px; background: rgba(255, 255, 255, 0.2); overflow: hidden; flex: none; }
.status-progress span { display: block; height: 100%; width: 0; background: #5ac8fa; transition: width 0.25s; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.topbar-actions { display: flex; align-items: center; gap: 2px; }

/* ================= Notice ================= */
.notice {
  display: flex; gap: 12px; align-items: flex-start;
  margin: 12px 16px 0; padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.notice.warn { background: var(--warn-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent); }
.notice.warn .icon { color: var(--warn); margin-top: 1px; }
.notice strong { display: block; margin-bottom: 2px; }
.notice.small { margin: 0 0 12px; }

/* ================= Layout ================= */
.layout {
  display: grid; gap: 16px;
  width: 100%; max-width: 1400px;
  margin: 0 auto; padding: 16px;
}
.side { display: grid; gap: 16px; align-content: start; min-width: 0; }
.library { min-width: 0; }

.card {
  position: relative;
  background: var(--surface);
  backdrop-filter: blur(24px) saturate(160%);
  -webkit-backdrop-filter: blur(24px) saturate(160%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  min-width: 0;
}

.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.card-head-text { min-width: 0; flex: 1; }
.card-head h2 { font-size: 17px; font-weight: 700; letter-spacing: -0.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-head p { font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.ok-text { color: var(--ok) !important; font-weight: 600; }
.route-badge { margin-left: 6px; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 700; background: var(--ok-soft); color: var(--ok); }
.route-badge.relay { background: var(--accent-soft); color: var(--accent); }
.setting.info strong#turnInfo { font-family: var(--font); font-size: 13px; }

.device-badge {
  display: grid; place-items: center; flex: none;
  width: 44px; height: 44px; border-radius: 14px;
  background: var(--accent-soft); color: var(--accent);
}
.device-badge.ok { background: var(--ok-soft); color: var(--ok); }
.device-badge.lg { width: 64px; height: 64px; border-radius: 20px; }
.device-badge.lg .icon { width: 30px; height: 30px; }

.field-label { display: block; font-size: 12px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.hint { font-size: 12.5px; color: var(--text-2); margin: 12px 0; }
.hint strong { color: var(--text); }

/* ================= Segmented ================= */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  padding: 3px; gap: 3px;
  background: var(--surface-3); border-radius: 12px;
  margin-bottom: 16px;
}
.seg-btn {
  min-height: 36px; padding: 0 10px; border-radius: 9px;
  font-size: 13.5px; font-weight: 600; color: var(--text-2);
  transition: background 0.2s, color 0.2s;
}
.seg-btn[aria-selected="true"], .seg-btn.active { background: var(--surface-solid); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.segmented.small { margin: 0; }
.segmented.small .seg-btn { min-height: 30px; font-size: 12.5px; }

/* ================= Host panel ================= */
.host-grid { display: grid; grid-template-columns: 132px 1fr; gap: 16px; align-items: center; }
.qr-tile {
  width: 132px; height: 132px; padding: 8px;
  background: #fff; border-radius: 16px; border: 1px solid var(--border);
  display: grid; place-items: center; cursor: zoom-in;
}
.qr-tile canvas, .qr-tile img { width: 100% !important; height: 100% !important; image-rendering: pixelated; }
.qr-tile:empty::after { content: ""; width: 24px; height: 24px; border-radius: 50%; border: 3px solid var(--surface-3); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }

.code-tile { min-width: 0; }
.code-display {
  display: block; width: 100%; text-align: left;
  font-family: var(--mono); font-size: clamp(28px, 7vw, 38px); font-weight: 700; letter-spacing: 0.06em;
  color: var(--text); line-height: 1.15; white-space: nowrap;
}
.code-meta { display: flex; align-items: center; gap: 12px; margin-top: 6px; }
.ttl-ring { font-family: var(--mono); font-size: 12.5px; color: var(--text-2); padding: 2px 8px; border-radius: 999px; background: var(--surface-3); }

/* ================= Join panel ================= */
.divider { display: flex; align-items: center; gap: 12px; margin: 16px 0; color: var(--text-3); font-size: 12px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border-strong); }

.code-input { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.code-input input {
  width: 100%; min-width: 0; max-width: 52px; height: 56px;
  text-align: center; font-family: var(--mono); font-size: 24px; font-weight: 700;
  background: var(--surface-solid); border: 1.5px solid var(--border-strong); border-radius: 12px;
  outline: none; transition: border-color 0.15s, box-shadow 0.15s;
}
.code-input input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.code-input.error input { border-color: var(--danger); animation: shake 0.35s; }
.code-sep { width: 10px; height: 2px; background: var(--text-3); flex: none; border-radius: 2px; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

.advanced { margin-top: 16px; }
.advanced summary { cursor: pointer; font-size: 13px; color: var(--text-2); font-weight: 600; min-height: 32px; display: flex; align-items: center; }
.inline-form { display: flex; gap: 8px; margin-top: 8px; }
.inline-form input {
  flex: 1; min-width: 0; height: 42px; padding: 0 14px;
  border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface-solid);
  font-size: 16px; outline: none;
}
.inline-form input:focus { border-color: var(--accent); }

/* ================= Pending / SAS ================= */
.pending-wrap { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 8px; padding: 8px 0; }
.pending-wrap h2 { font-size: 17px; }
.pending-wrap p { font-size: 13px; color: var(--text-2); }
.pending-wrap .btn { margin-top: 8px; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--surface-3); border-top-color: var(--accent); animation: spin 0.8s linear infinite; margin-bottom: 4px; }
@keyframes spin { to { transform: rotate(360deg); } }

.sas-box {
  display: flex; flex-direction: column; align-items: center;
  padding: 14px; margin: 8px 0; width: 100%;
  border-radius: 16px; background: var(--ok-soft);
}
.sas-box .field-label { margin-bottom: 2px; }
.sas-code { font-family: var(--mono); font-size: 32px; letter-spacing: 0.08em; color: var(--ok); line-height: 1.2; }
.sas-words { font-size: 12.5px; color: var(--text-2); font-weight: 600; }
.sas-inline {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px; border-radius: 12px; background: var(--ok-soft); font-size: 13px; color: var(--text-2);
}
.sas-inline strong { font-family: var(--mono); font-size: 17px; color: var(--ok); letter-spacing: 0.06em; }

/* ================= Composer ================= */
.composer-tabs { display: flex; gap: 4px; margin-bottom: 12px; overflow-x: auto; scrollbar-width: none; }
.composer-tabs::-webkit-scrollbar { display: none; }
.tab-btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 14px; border-radius: 999px;
  font-size: 13.5px; font-weight: 600; color: var(--text-2); white-space: nowrap;
}
.tab-btn:active { transform: scale(0.97); }
.tab-btn.active { background: var(--accent); color: #fff; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

.textarea {
  display: block; width: 100%; min-height: 110px; max-height: 40vh; resize: vertical;
  padding: 12px 14px; border-radius: 14px;
  background: var(--surface-solid); border: 1px solid var(--border-strong);
  font-size: 16px; line-height: 1.55; outline: none;
}
.textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.composer-foot { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.composer-foot .muted { margin-left: auto; }
kbd { display: inline-block; padding: 0 5px; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 5px; font-family: var(--mono); font-size: 11px; line-height: 1.5; }

.typing { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ok); font-weight: 600; min-width: 0; }
.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: pulse 1s infinite; }
.typing-dots i:nth-child(2) { animation-delay: 0.15s; }
.typing-dots i:nth-child(3) { animation-delay: 0.3s; }

.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
  padding: 26px 16px; border-radius: 16px;
  border: 2px dashed var(--border-strong); background: var(--surface-2);
  color: var(--text-2); cursor: pointer; transition: border-color 0.2s, background 0.2s;
  margin-bottom: 12px;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone strong { color: var(--text); font-size: 14.5px; }
.pick-btn { flex: 1 1 100%; cursor: pointer; }
.chip-row .chip-btn { min-height: 40px; }

.voice-panel { position: relative; display: grid; gap: 12px; }
.voice-close { display: none; position: absolute; top: -6px; right: -6px; }
.voice-canvas { width: 100%; height: 72px; border-radius: 14px; background: var(--surface-2); }
.voice-row { display: flex; align-items: center; gap: 14px; }
.record-btn {
  width: 56px; height: 56px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: var(--danger); color: #fff;
  box-shadow: 0 8px 20px -8px var(--danger);
}
.record-btn.recording { animation: rec 1.2s infinite; border-radius: 16px; }
@keyframes rec { 50% { box-shadow: 0 0 0 10px var(--danger-soft); } }

.composer-lock {
  position: absolute; inset: 0; z-index: 2;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  border-radius: inherit;
  background: color-mix(in srgb, var(--surface-solid) 70%, transparent);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  color: var(--text-2); font-weight: 600; font-size: 14px;
}
body.is-connected .composer-lock { display: none; }

/* ================= Library ================= */
.library-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.library-head h2 { font-size: 20px; font-weight: 800; letter-spacing: -0.02em; display: flex; align-items: center; gap: 8px; margin-right: auto; }
.count-badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-3); color: var(--text-2); }
.library-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.search-box {
  display: flex; align-items: center; gap: 6px; height: 40px; padding: 0 12px;
  border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-3);
  min-width: 0;
}
.search-box input { border: none; background: none; outline: none; width: 140px; min-width: 0; font-size: 16px; color: var(--text); }

.filter-bar {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  margin: 0 -16px 14px; padding: 2px 16px;
  scroll-snap-type: x proximity;
}
.filter-bar::-webkit-scrollbar { display: none; }
.filter-chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  min-height: 34px; padding: 0 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  font-size: 13px; font-weight: 600; color: var(--text-2); scroll-snap-align: start;
}
.filter-chip span { font-size: 11.5px; color: var(--text-3); }
.filter-chip:active { transform: scale(0.97); }
.filter-chip.active { background: var(--text); color: var(--bg); border-color: transparent; }
.filter-chip.active span { color: inherit; opacity: 0.7; }

.items-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 12px; }

.item-card {
  position: relative; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); overflow: hidden; min-width: 0;
  transition: transform 0.15s, box-shadow 0.15s, outline-color 0.15s;
  outline: 3px solid transparent; outline-offset: -3px;
  animation: cardIn 0.3s var(--ease);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
@keyframes cardIn { from { opacity: 0; transform: translateY(6px) scale(0.98); } }
.item-card.selected { outline-color: var(--accent); }
.item-thumb {
  position: relative; aspect-ratio: 4 / 3; background: var(--surface-3);
  display: grid; place-items: center; overflow: hidden; color: var(--text-3);
  cursor: pointer; width: 100%;
}
.item-thumb img, .item-thumb video { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.item-thumb .play-badge {
  position: absolute; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%;
  background: rgba(0, 0, 0, 0.55); color: #fff; backdrop-filter: blur(6px);
}
.item-thumb .play-badge .icon { fill: #fff; stroke: none; width: 16px; height: 16px; margin-left: 2px; }
.item-thumb .memo-text {
  align-self: stretch; justify-self: stretch; padding: 38px 12px 12px; text-align: left;
  font-size: 13px; line-height: 1.5; color: var(--text); white-space: pre-wrap; word-break: break-word;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical;
  background: var(--surface-solid);
}
.item-thumb .file-ext { position: absolute; bottom: 10px; font-size: 11px; font-weight: 800; letter-spacing: 0.05em; color: var(--text-2); }

.dir-badge {
  position: absolute; top: 8px; left: 8px; z-index: 1;
  display: inline-flex; align-items: center; gap: 3px;
  padding: 2px 8px 2px 6px; border-radius: 999px;
  font-size: 11px; font-weight: 700; color: #fff; background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.dir-badge .icon { width: 11px; height: 11px; stroke-width: 3; }
.select-mark {
  position: absolute; top: 8px; right: 8px; z-index: 1;
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid #fff; background: rgba(0, 0, 0, 0.25);
  display: none; place-items: center; color: #fff;
}
.select-mark .icon { width: 14px; height: 14px; stroke-width: 3; opacity: 0; }
body.select-mode .select-mark { display: grid; }
.item-card.selected .select-mark { background: var(--accent); border-color: var(--accent); }
.item-card.selected .select-mark .icon { opacity: 1; }

.item-progress {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 1;
  padding: 18px 10px 8px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
  color: #fff; font-size: 11.5px; font-weight: 600;
}
.item-progress .bar { height: 4px; border-radius: 4px; background: rgba(255, 255, 255, 0.3); overflow: hidden; margin-top: 4px; }
.item-progress .bar i { display: block; height: 100%; width: 0; background: #fff; transition: width 0.25s; }

.item-info { display: flex; align-items: center; gap: 4px; padding: 8px 4px 8px 12px; min-width: 0; }
.item-text { min-width: 0; flex: 1; }
.item-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-meta { font-size: 11.5px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-meta .st-ok { color: var(--ok); font-weight: 600; }
.item-meta .st-bad { color: var(--danger); font-weight: 600; }
.item-meta .st-busy { color: var(--accent); font-weight: 600; }
.item-actions { display: flex; flex: none; }
.item-act { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; color: var(--text-2); flex: none; }
.item-act:active { background: var(--surface-3); color: var(--text); }
.item-act .icon { width: 17px; height: 17px; }
body.select-mode .item-act { visibility: hidden; }

.empty-state { text-align: center; padding: 48px 20px; color: var(--text-2); }
.empty-icon { display: inline-grid; place-items: center; width: 72px; height: 72px; border-radius: 22px; background: var(--surface-3); color: var(--text-3); margin-bottom: 14px; }
.empty-state h3 { font-size: 16px; color: var(--text); margin-bottom: 6px; }
.empty-state p { font-size: 13px; max-width: 420px; margin: 0 auto; }

/* ================= Bottom bar (phones) ================= */
/* Messages-style composer: the field wraps and grows upward instead of scrolling sideways */
.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: var(--kb-inset, 0px); z-index: 60;
  display: none;
  padding: 8px max(10px, var(--safe-right)) calc(8px + var(--safe-bottom)) max(8px, var(--safe-left));
  background: color-mix(in srgb, var(--surface-solid) 88%, transparent);
  backdrop-filter: blur(20px) saturate(180%); -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-top: 1px solid var(--border);
}
body.kb-open .bottom-bar { padding-bottom: 8px; }
.bar-row { display: flex; align-items: flex-end; gap: 4px; }
.bar-tools { display: flex; align-items: flex-end; gap: 2px; flex: none; }
.bar-btn {
  display: grid; place-items: center; flex: none;
  width: var(--tap); height: var(--tap); border-radius: 50%; color: var(--text-2); cursor: pointer;
  transition: background 0.12s, transform 0.12s var(--ease);
}
.bar-btn:active { background: var(--surface-3); transform: scale(0.94); }
/* Photos, videos and files share one picker, so it gets the accent */
.bar-btn.bar-pick { background: var(--accent-soft); color: var(--accent); }
.bar-btn.bar-pick .icon { width: 22px; height: 22px; }
.bar-btn.bar-pick:active { background: var(--accent); color: #fff; }
.bar-expand { display: none; color: var(--accent); }
.bar-expand .icon { width: 24px; height: 24px; }
body.bar-composing:not(.bar-tools-open) .bar-tools { display: none; }
body.bar-composing:not(.bar-tools-open) .bar-expand { display: grid; }
body.bar-tools-open .bar-expand .icon { transform: rotate(45deg); }
.bar-input {
  flex: 1; min-width: 0; display: flex; align-items: flex-end; gap: 4px;
  min-height: var(--tap); padding: 4px 4px 4px 14px; margin-left: 2px;
  border-radius: 22px; background: var(--surface-3);
  border: 1px solid transparent; transition: border-color 0.15s;
}
.bar-input:focus-within { border-color: var(--accent); }
.bar-input textarea {
  flex: 1; min-width: 0; display: block;
  height: 34px; max-height: min(40dvh, 196px); padding: 6px 0; resize: none;
  border: none; background: none; outline: none;
  font-size: 16px; line-height: 22px;
  overflow-y: auto; overscroll-behavior: contain;
  overflow-wrap: anywhere; word-break: normal;
}
.bar-input textarea::placeholder { color: var(--text-3); }
.bar-send {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent); color: #fff;
  transition: background 0.15s, opacity 0.15s, transform 0.12s var(--ease);
}
.bar-send:active:not(:disabled) { transform: scale(0.9); }
.bar-send:disabled { opacity: 1; background: var(--border-strong); color: var(--surface-solid); cursor: default; }
.bar-send .icon { width: 18px; height: 18px; stroke-width: 2.6; }
.bar-typing {
  display: flex; align-items: center; gap: 6px;
  padding: 0 6px 6px; font-size: 12.5px; font-weight: 600; color: var(--ok);
}
.bar-typing[hidden] { display: none; }
body:not(.is-connected) .bottom-bar .bar-btn, body:not(.is-connected) .bottom-bar .bar-input { opacity: 0.45; pointer-events: none; }

/* ================= Batch bar ================= */
.batch-bar {
  position: fixed; z-index: 70;
  left: 50%; bottom: calc(16px + var(--safe-bottom) + var(--bottombar-h) + var(--kb-inset, 0px));
  transform: translate(-50%, 140%);
  display: flex; align-items: center; gap: 4px;
  width: min(560px, calc(100% - 24px)); padding: 6px 8px;
  border-radius: 999px;
  background: var(--surface-solid); border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-lg);
  transition: transform 0.3s var(--ease); visibility: hidden;
}
body.select-mode .batch-bar { transform: translate(-50%, 0); visibility: visible; }
.batch-count { font-weight: 700; font-size: 14px; white-space: nowrap; padding: 0 4px; }

/* ================= Drop overlay ================= */
.drop-overlay {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center; padding: 24px;
  background: color-mix(in srgb, var(--accent) 18%, var(--scrim));
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
body.dragging .drop-overlay { opacity: 1; }
.drop-target {
  display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center;
  padding: 40px 48px; border-radius: 28px; border: 2px dashed var(--accent);
  background: var(--surface-solid); color: var(--accent); box-shadow: var(--shadow-lg);
}
.drop-target strong { font-size: 20px; color: var(--text); }
.drop-target span { font-size: 13px; color: var(--text-2); }

/* ================= Modals / sheets ================= */
.modal {
  position: fixed; inset: 0; z-index: 300;
  display: grid; place-items: center; padding: 20px;
  background: var(--scrim);
  animation: fadeIn 0.2s;
}
@keyframes fadeIn { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 560px; max-height: calc(100dvh - 40px); overflow: auto; overscroll-behavior: contain;
  padding: 20px; border-radius: 24px;
  background: var(--surface-solid); box-shadow: var(--shadow-lg);
  animation: sheetIn 0.28s var(--ease);
}
.sheet.narrow { max-width: 420px; }
@keyframes sheetIn { from { opacity: 0; transform: translateY(16px) scale(0.98); } }
.sheet h3 { font-size: 18px; font-weight: 700; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: -6px -8px 12px 0; }
.sheet-actions { display: grid; gap: 10px; margin-top: 16px; }
.sheet-actions.two { grid-template-columns: 1fr 1fr; }
#deleteModal .muted { margin-top: 6px; font-size: 14px; }

.approve-head { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 10px; margin-bottom: 8px; }
.approve-head h3 { word-break: break-word; }
.count-chip { min-width: 26px; padding: 1px 7px; border-radius: 999px; background: rgba(255, 255, 255, 0.22); font-size: 12px; font-variant-numeric: tabular-nums; }

.qr-large { width: min(280px, 70vw); aspect-ratio: 1; margin: 4px auto 12px; padding: 12px; background: #fff; border-radius: 20px; display: grid; place-items: center; }
.qr-large canvas, .qr-large img { width: 100% !important; height: 100% !important; image-rendering: pixelated; }
.qr-large:empty::after { content: "接続準備中…"; color: #888; font-size: 13px; }

.scanner { position: relative; aspect-ratio: 1; border-radius: 20px; overflow: hidden; background: #000; margin-bottom: 12px; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-frame { position: absolute; inset: 18%; border: 3px solid rgba(255, 255, 255, 0.85); border-radius: 18px; box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35); }
.scanner.unsupported { display: none; }

.settings-list { display: grid; margin-bottom: 16px; border-radius: 16px; background: var(--surface-2); overflow: hidden; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; padding: 8px 14px; border-bottom: 1px solid var(--border); font-size: 14.5px; }
.setting:last-child { border-bottom: none; }
.setting > span { font-weight: 600; flex: none; }
.setting > span:has(.setting-sub) { flex: 1 1 auto; min-width: 0; }
.setting-sub { display: block; margin-top: 2px; font-size: 12px; font-weight: 400; line-height: 1.4; }
.setting input[type="text"], .setting select {
  min-width: 0; width: 60%; max-width: 240px; height: 36px; padding: 0 10px;
  border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface-solid); font-size: 16px; text-align: right;
}
.setting select { text-align: left; }
.setting.info strong { font-family: var(--mono); font-size: 14px; }

.switch { appearance: none; -webkit-appearance: none; width: 50px; height: 30px; border-radius: 999px; background: var(--surface-3); position: relative; cursor: pointer; transition: background 0.2s; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.2s var(--ease); }
.switch:checked { background: var(--ok); }
.switch:checked::after { transform: translateX(20px); }

.security-info { margin-top: 16px; font-size: 13px; color: var(--text-2); }
.security-info summary { cursor: pointer; display: flex; align-items: center; gap: 6px; font-weight: 600; min-height: 36px; color: var(--text); }
.security-info ul { padding-left: 20px; display: grid; gap: 6px; margin-top: 6px; }

/* Viewer */
.modal.viewer { padding: 0; place-items: stretch; background: rgba(0, 0, 0, 0.92); }
/* Fixed to the window so large photos are scaled down to fit instead of growing the frame */
.viewer-frame { position: relative; width: 100%; height: 100%; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; color: #fff; overflow: hidden; }
.viewer-head { display: flex; align-items: center; gap: 8px; padding: calc(8px + var(--safe-top)) 12px 8px; }
.viewer-head .icon-btn { color: #fff; }
.viewer-head .icon-btn:active { background: rgba(255, 255, 255, 0.16); }
.viewer-title { min-width: 0; flex: 1; display: grid; }
.viewer-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-frame .muted { color: rgba(255, 255, 255, 0.6); }
.viewer-body { min-height: 0; display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); place-items: center; padding: 8px 16px; overflow: auto; overscroll-behavior: contain; touch-action: pan-y pinch-zoom; }
.viewer-body img, .viewer-body video { display: block; max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; border-radius: 8px; }
.viewer-body audio { width: min(480px, 100%); }
.viewer-body pre {
  width: min(760px, 100%); max-height: 100%; overflow: auto;
  padding: 18px; border-radius: 16px; background: #1c1e24; color: #f2f3f5;
  white-space: pre-wrap; word-break: break-word; font-family: var(--font); font-size: 15px; line-height: 1.6;
  -webkit-user-select: text; user-select: text;
}
.viewer-file { display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; padding: 24px; }
.viewer-file .icon { width: 64px; height: 64px; stroke-width: 1.4; }
.viewer-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; padding: 10px 12px calc(12px + var(--safe-bottom)); }
.viewer-actions .btn { background: rgba(255, 255, 255, 0.12); border-color: transparent; color: #fff; }
.viewer-actions .btn.danger-ghost { color: #ff6b63; }
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.12); color: #fff;
}
.viewer-nav.prev { left: 12px; }
.viewer-nav.next { right: 12px; }

/* ================= Toasts ================= */
.toasts {
  position: fixed; z-index: 400; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + var(--safe-bottom) + var(--bottombar-h) + var(--kb-inset, 0px));
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  width: min(440px, calc(100% - 32px)); pointer-events: none;
}
.toast {
  display: flex; align-items: center; gap: 8px;
  padding: 11px 16px; border-radius: 14px;
  background: #15171c; color: #fff; font-size: 13.5px; font-weight: 500;
  box-shadow: var(--shadow-lg); pointer-events: auto;
  animation: toastIn 0.3s var(--ease);
  transition: opacity 0.3s, transform 0.3s;
}
.toast.error { background: #b4231c; }
.toast.ok .icon { color: #34c759; }
.toast.out { opacity: 0; transform: translateY(8px); }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px) scale(0.96); } }

/* ================= Tablet (721–1079) ================= */
@media (min-width: 721px) {
  .layout { padding: 20px 24px; gap: 20px; }
  .side { grid-template-columns: 1fr 1fr; }
  .card { padding: 20px; }
  .items-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; }
  .filter-bar { margin: 0 0 14px; padding: 2px 0; }
  /* Keep the prev/next buttons beside the photo, not on top of it */
  .viewer-body { padding: 12px 76px 16px; }
  .viewer-head { padding-left: 20px; padding-right: 20px; }
}

/* ================= Desktop (≥1080) ================= */
@media (min-width: 1080px) {
  .layout { grid-template-columns: minmax(360px, 400px) 1fr; padding: 24px 32px; gap: 24px; align-items: start; }
  .side { grid-template-columns: 1fr; position: sticky; top: calc(var(--topbar-h) + var(--safe-top) + 24px); }
  .items-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .topbar { padding-left: max(32px, var(--safe-left)); padding-right: max(32px, var(--safe-right)); }
}

/* Tablet landscape: keep side column narrower so the library gets room */
@media (min-width: 1080px) and (max-width: 1279px) {
  .layout { grid-template-columns: 340px 1fr; padding: 20px 24px; }
  .host-grid { grid-template-columns: 112px 1fr; }
  .qr-tile { width: 112px; height: 112px; }
}

/* ================= Phone (≤720) ================= */
@media (max-width: 720px) {
  :root { --topbar-h: 56px; --bottombar-h: calc(var(--bar-h, 61px) - var(--safe-bottom)); }
  body { font-size: 15px; }
  .hide-sm { display: none !important; }
  .topbar { gap: 8px; padding-left: max(12px, var(--safe-left)); padding-right: max(8px, var(--safe-right)); }
  .brand-name em { display: none; }
  .brand-name { font-size: 16px; }
  .status-pill { height: 32px; padding: 0 12px; font-size: 12.5px; }
  .status-progress { width: 36px; }

  .layout { padding: 12px; gap: 12px; }
  .card { padding: 16px; border-radius: 18px; }
  .composer-card { display: none; }
  .bottom-bar { display: block; }

  .host-grid { grid-template-columns: 112px 1fr; gap: 14px; }
  .qr-tile { width: 112px; height: 112px; }

  .library-head h2 { font-size: 18px; }
  .search-box input { width: 90px; }
  .search-box input:focus { width: 140px; }
  .items-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .item-info { padding: 6px 2px 6px 10px; }

  /* Sheets slide up from the bottom on phones */
  .modal:not(.viewer) { place-items: end stretch; padding: 0; }
  .modal:not(.viewer) .sheet {
    max-width: none; border-radius: 24px 24px 0 0;
    max-height: calc(100dvh - 24px - var(--safe-top));
    padding: 20px 18px calc(20px + var(--safe-bottom));
    animation: sheetUp 0.3s var(--ease);
  }
  @keyframes sheetUp { from { transform: translateY(100%); } }

  .viewer-nav { display: none; }
  .viewer-actions .btn { flex: 1 1 40%; }

  .batch-bar { border-radius: 20px; }

  /* Voice recorder opens as a sheet from the bottom bar */
  body.voice-open .composer-card {
    display: block; position: fixed; z-index: 310; left: 0; right: 0; bottom: 0;
    border-radius: 24px 24px 0 0; padding: 20px 18px calc(20px + var(--safe-bottom));
    background: var(--surface-solid); box-shadow: var(--shadow-lg); animation: sheetUp 0.3s var(--ease);
  }
  body.voice-open::after { content: ""; position: fixed; inset: 0; z-index: 305; background: var(--scrim); }
  body.voice-open .composer-tabs, body.voice-open .composer-lock { display: none; }
  body.voice-open .tab-pane { display: none; }
  body.voice-open .tab-pane[data-pane="voice"] { display: block; }
  body.voice-open .voice-close { display: grid; }
}

@media (max-width: 360px) {
  .host-grid { grid-template-columns: 1fr; justify-items: center; text-align: center; }
  .code-display { text-align: center; }
  .code-meta { justify-content: center; }
  .code-input input { height: 48px; font-size: 20px; }
  .status-pill .status-progress { display: none; }
}

/* Hover states only for a mouse or trackpad, so they never stick after a tap */
@media (hover: hover) and (pointer: fine) {
  .btn:hover:not(:disabled) { border-color: var(--border-strong); }
  .btn.primary:hover:not(:disabled) { background: var(--accent-strong); }
  .icon-btn:hover { background: var(--surface-2); }
  .chip-btn:hover { background: color-mix(in srgb, var(--accent) 18%, transparent); }
  .tab-btn:not(.active):hover, .filter-chip:not(.active):hover { color: var(--text); }
  .dropzone:hover { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
  .item-act:hover { background: var(--surface-2); color: var(--text); }
  .viewer-head .icon-btn:hover { background: rgba(255, 255, 255, 0.1); }
  .viewer-nav:hover { background: rgba(255, 255, 255, 0.2); }
  .bar-btn:hover { background: var(--surface-3); }
}
@media (hover: hover) and (pointer: fine) and (min-width: 1080px) {
  .item-card:hover { transform: translateY(-2px); box-shadow: var(--shadow), 0 16px 32px -16px rgba(15, 23, 42, 0.25); }
}

/* Touch devices: no folder picker, bigger hit areas */
@media (hover: none) {
  .hide-touch { display: none !important; }
  .item-act { width: 40px; height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
