:root {
  --bg: #0b1020;
  --bg2: #121a2f;
  --bg3: #1a243c;
  --stroke: #2a3655;
  --text: #e8eefc;
  --muted: #8b9bb8;
  --primary: #4d8dff;
  --primary-dim: #3a6fcc;
  --accent: #a878ff;
  --danger: #ff5c7a;
  --ok: #3dd68c;
  --warn: #ffb020;
  --haos: #40dcc8;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "PingFang SC", "Hiragino Sans", "Noto Sans JP", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
input, textarea {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--stroke);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px #4d8dff33; }
textarea { resize: vertical; min-height: 72px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); border: 0;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-t);
  padding-bottom: var(--safe-b);
  background:
    radial-gradient(900px 360px at 50% -8%, #2a1855 0%, transparent 55%),
    radial-gradient(700px 280px at 100% 20%, #1a2a55 0%, transparent 50%),
    var(--bg);
}

.screen { display: none; flex: 1; flex-direction: column; min-height: 0; }
.screen.active { display: flex; animation: fadeIn 0.22s ease; }

/* LANGUAGE SPLASH */
#lang-screen {
  justify-content: space-between;
  padding: 36px 18px 28px;
}
.lang-hero {
  text-align: center;
  padding-top: min(10vh, 72px);
}
.lang-hero h1 {
  margin: 4px 0 0;
  font-size: 2rem;
  letter-spacing: 0.04em;
  font-weight: 750;
  background: linear-gradient(135deg, #f4f7ff 20%, #c5b0ff 70%, #7aa4ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.lang-hero .lock-logo { margin-bottom: 4px; }
.flag-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  padding-bottom: 8px;
}
.flag-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 14px 6px 12px;
  background: var(--bg2);
  border: 1px solid var(--stroke);
  border-radius: 16px;
  transition: transform 0.12s ease, border-color 0.15s, background 0.15s;
}
.flag-btn:hover { border-color: #4d8dff66; }
.flag-btn:active { transform: scale(0.96); }
.flag-btn.active {
  border-color: var(--primary);
  background: #4d8dff18;
  box-shadow: 0 0 0 3px #4d8dff22;
}
.flag-name {
  font-size: 0.78rem;
  font-weight: 650;
  color: var(--muted);
  letter-spacing: 0.01em;
}
.flag-btn.active .flag-name { color: var(--text); }
.flag-icon {
  width: 52px;
  height: 35px;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 0 0 1px #ffffff22, 0 4px 12px #0005;
  flex-shrink: 0;
}
.flag-icon svg { display: block; width: 100%; height: 100%; }

/* LANG CHIP / POPOVER */
.lang-chip {
  background: var(--bg3);
  border: 1px solid var(--stroke);
  border-radius: 12px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  padding: 0;
  flex-shrink: 0;
}
.lang-chip.sm { width: 40px; height: 40px; }
.lang-chip:active { transform: scale(0.96); }
.lang-chip .chip-flag .flag-icon {
  width: 24px;
  height: 16px;
  border-radius: 3px;
  box-shadow: 0 0 0 1px #ffffff33;
}
#lock-screen { position: relative; }
#lock-lang-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 3;
}
.lang-popover {
  position: fixed;
  z-index: 80;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  min-width: 196px;
  background: var(--bg2);
  border: 1px solid var(--stroke);
  border-radius: 14px;
  padding: 8px;
  box-shadow: 0 16px 40px #000a;
}
.lang-popover[hidden] { display: none; }
.lang-pop-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px 6px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
}
.lang-pop-btn .flag-icon { width: 30px; height: 20px; }
.lang-pop-btn .flag-name { font-size: 0.6rem; font-weight: 650; color: var(--muted); }
.lang-pop-btn.active {
  border-color: var(--primary);
  background: #4d8dff22;
}
.lang-pop-btn.active .flag-name { color: var(--text); }

/* LOCK */
.lock-card {
  margin: auto 16px;
  padding: 28px 20px 24px;
  background: var(--bg2);
  border: 1px solid var(--stroke);
  border-radius: 20px;
  box-shadow: 0 12px 40px #0006;
  text-align: center;
}
.lock-logo { font-size: 3rem; filter: drop-shadow(0 0 12px #a878ff88); margin-bottom: 8px; }
.lock-card h1 { margin: 0 0 4px; font-size: 1.35rem; letter-spacing: 0.02em; }
.subtitle { margin: 0; font-size: 0.78rem; color: var(--muted); }
.lock-card form { margin-top: 20px; text-align: left; }
.hint { font-size: 0.72rem; color: var(--muted); margin: 8px 0 14px; }
.error { color: var(--danger); font-size: 0.85rem; margin: 0 0 12px; }

/* FIELDS */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.field.check {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg3); border: 1px solid var(--stroke);
  border-radius: 12px; padding: 12px 14px;
}
.field.check input { width: auto; accent-color: var(--primary); }
.field.check > span { margin: 0; color: var(--text); font-weight: 500; }
.pw-row { display: flex; gap: 8px; align-items: center; }
.pw-row input { flex: 1; }

/* BUTTONS */
.btn {
  border: 1px solid var(--stroke);
  background: var(--bg3);
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 600;
  transition: transform 0.1s ease, background 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-color: transparent;
  color: #fff;
  width: 100%;
}
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #ff5c7a55; }
.btn.danger.ghost {
  background: transparent;
  color: var(--danger);
  border: 1px solid #ff5c7a55;
}
.btn.danger.ghost:hover {
  background: #ff5c7a18;
}
.icon-btn {
  background: var(--bg3); border: 1px solid var(--stroke);
  border-radius: 12px; width: 40px; height: 40px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 1rem;
}
.icon-btn:active { transform: scale(0.96); }

/* TOPBAR */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px 8px; gap: 8px;
}
.brand { display: flex; gap: 10px; align-items: center; min-width: 0; }
.logo { font-size: 1.6rem; filter: drop-shadow(0 0 8px #a878ff88); }
.brand h1 { margin: 0; font-size: 1.05rem; letter-spacing: 0.02em; }
.top-actions { display: flex; gap: 4px; }

.toolbar {
  display: flex; gap: 8px; padding: 4px 12px 10px; align-items: center;
}
.search-wrap { flex: 1; }
.search-wrap input {
  border-radius: 999px;
  padding-left: 16px;
  background: var(--bg2);
}
.fab-inline {
  width: 44px; height: 44px; padding: 0;
  border-radius: 14px; font-size: 1.4rem; line-height: 1;
  flex-shrink: 0;
}

/* LIST */
.entry-list {
  flex: 1; overflow-y: auto; padding: 0 12px 20px;
  display: flex; flex-direction: column; gap: 10px;
}
.empty {
  text-align: center; color: var(--muted); padding: 40px 20px; font-size: 0.95rem;
}
.card {
  background: var(--bg2);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  padding: 12px 14px;
  animation: fadeIn 0.18s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}
.card-head {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
  margin-bottom: 8px;
}
.card-title {
  margin: 0; font-size: 1rem; font-weight: 700; word-break: break-word;
}
.card-tag {
  display: inline-block; margin-top: 4px;
  font-size: 0.68rem; font-weight: 600;
  color: var(--haos); background: #40dcc822;
  border: 1px solid #40dcc844;
  border-radius: 999px; padding: 2px 8px;
}
.badge-google {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.68rem; font-weight: 600;
  color: var(--warn); background: #ffb02022;
  border: 1px solid #ffb02044;
  border-radius: 999px; padding: 2px 8px; margin-left: 6px;
}
.card-meta {
  font-size: 0.82rem; color: var(--muted);
  margin: 0 0 10px; word-break: break-all;
}
.copy-row {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.copy-btn {
  background: var(--bg3); border: 1px solid var(--stroke);
  border-radius: 10px; padding: 7px 10px;
  font-size: 0.75rem; font-weight: 600; color: var(--text);
}
.copy-btn:active { background: var(--primary-dim); }
.copy-btn:disabled { opacity: 0.35; cursor: default; }
.card-edit {
  background: transparent; border: none; color: var(--muted);
  font-size: 0.85rem; padding: 4px 6px;
}

/* MODAL */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal[hidden] { display: none; }
.modal-backdrop {
  position: absolute; inset: 0; background: #000a;
}
.modal-sheet {
  position: relative; z-index: 1;
  width: 100%; max-width: 520px; max-height: 92dvh;
  overflow-y: auto;
  background: var(--bg2);
  border: 1px solid var(--stroke);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(16px + var(--safe-b));
  animation: sheetUp 0.22s ease;
}
@keyframes sheetUp {
  from { transform: translateY(40px); opacity: 0.6; }
  to { transform: none; opacity: 1; }
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 0 12px;
}
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-actions {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end;
  margin-top: 8px; padding-top: 8px;
}
.modal-actions .btn { width: auto; min-width: 88px; }
.modal-actions .btn.danger { margin-right: auto; }

/* TOAST */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%);
  background: var(--bg3); border: 1px solid var(--stroke);
  color: var(--text); padding: 10px 18px; border-radius: 999px;
  font-size: 0.85rem; font-weight: 600; z-index: 100;
  box-shadow: 0 8px 24px #0008;
  white-space: nowrap;
}
.toast[hidden] { display: none; }

@media (min-width: 560px) {
  .modal { align-items: center; }
  .modal-sheet {
    border-radius: 20px;
    margin: 16px;
    max-height: 85vh;
  }
}

/* Auth modes / Firebase */
.firebase-banner {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #ffb02022;
  border: 1px solid #ffb02055;
  color: #ffd58a;
  font-size: 0.78rem;
  line-height: 1.35;
  text-align: left;
}
.warn {
  font-size: 0.72rem;
  color: #ffd58a;
  background: #ffb02018;
  border: 1px solid #ffb02044;
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0 0 12px;
  line-height: 1.4;
  text-align: left;
}
.success {
  color: var(--ok);
  font-size: 0.85rem;
  margin: 0 0 12px;
  text-align: left;
}
.auth-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
  align-items: center;
}
.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 4px 8px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.link-btn:hover { color: #7aa4ff; }
.link-btn[hidden] { display: none !important; }

.rekey-panel {
  margin-top: 4px;
  text-align: left;
}
.rekey-panel[hidden] { display: none !important; }
.rekey-explain {
  font-size: 0.88rem;
  color: #c9d6f0;
  line-height: 1.45;
  margin: 0 0 14px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #4d8dff18;
  border: 1px solid #4d8dff44;
  font-weight: 550;
}
.rekey-choice,
.rekey-old-form {
  display: flex;
  flex-direction: column;
  gap: 0;
}
.rekey-choice[hidden],
.rekey-old-form[hidden] { display: none !important; }
.rekey-panel .btn {
  width: 100%;
  margin-top: 8px;
}
.rekey-panel .btn.danger.ghost {
  margin-top: 10px;
}
.rekey-panel .auth-links {
  margin-top: 12px;
}
