:root {
  --bg: #eef0f4;
  --surface: #ffffff;
  --text: #1a1a2e;
  --muted: #6b7280;
  --accent: #2563eb;
  --danger: #dc2626;
  --border: #d1d5db;
  --shadow: 0 2px 10px rgba(0,0,0,.08);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1220;
    --surface: #1a1a2e;
    --text: #eef0f4;
    --muted: #9aa0ac;
    --accent: #60a5fa;
    --danger: #f87171;
    --border: #2e3350;
    --shadow: 0 2px 12px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  padding-top: calc(16px + env(safe-area-inset-top));
  background: var(--surface);
  box-shadow: var(--shadow);
}

.app-header h1 {
  font-size: 1.25rem;
  margin: 0;
  font-weight: 700;
}

.header-actions { display: flex; gap: 6px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.icon-btn:active { background: var(--border); }

main { padding: 16px; padding-bottom: 100px; }

.sync-status {
  margin: 0 4px 12px;
  font-size: .85rem;
  color: var(--muted);
  background: var(--surface);
  padding: 8px 12px;
  border-radius: 10px;
}
.sync-status.error { color: var(--danger); }
.sync-status.ok { color: #16a34a; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: transform .08s ease;
}
.card:active { transform: scale(.97); }

.card-logo {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  object-fit: contain;
  background: #fff;
  padding: 4px;
}
.card-logo.monogram {
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.4rem;
}

.card-name {
  font-weight: 600;
  font-size: .95rem;
  line-height: 1.2;
}

.card-number {
  font-size: .72rem;
  color: var(--muted);
  letter-spacing: .5px;
  word-break: break-all;
}

.card-barcode {
  margin-top: auto;
  background: #fff;
  border-radius: 6px;
  padding: 4px;
}
.card-barcode svg { width: 100%; height: auto; display: block; }
.card-qr { margin: 0 auto; width: 80px; height: 80px; }
.card-qr svg, .card-qr canvas { width: 100%; height: 100%; }

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
  font-size: 1rem;
}

.fab {
  position: fixed;
  right: 22px;
  bottom: calc(22px + env(safe-area-inset-bottom));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 16px rgba(37,99,235,.4);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}
.fab:active { transform: scale(.95); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 40;
}
@media (min-width: 520px) {
  .modal { align-items: center; }
}
.modal-box {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  max-height: 90vh;
  overflow-y: auto;
}
@media (min-width: 520px) {
  .modal-box { border-radius: 20px; }
}
.modal-box h2 { margin: 0 0 16px; font-size: 1.2rem; }

.modal-box label {
  display: block;
  margin-bottom: 12px;
  font-size: .85rem;
  color: var(--muted);
}
.modal-box input[type="text"],
.modal-box input[type="url"],
.modal-box input[type="email"],
.modal-box input[type="password"] {
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}
.modal-box input[type="color"] {
  margin-top: 4px;
  width: 60px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin: 0 0 12px;
  padding: 10px 12px;
}
fieldset legend { font-size: .85rem; color: var(--muted); padding: 0 4px; }
.radio { display: flex; align-items: center; gap: 8px; font-size: .95rem; color: var(--text); margin-bottom: 6px; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.btn-primary, .btn-ghost, .btn-delete {
  border: none;
  border-radius: 10px;
  padding: 11px 16px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: var(--bg); color: var(--text); }
.btn-delete { background: var(--danger); color: #fff; }
.hint { font-size: .85rem; color: var(--muted); margin: 8px 0; }

/* Viewer */
.viewer {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.viewer-top {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
}
.viewer-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 420px;
  text-align: center;
  box-shadow: var(--shadow);
}
.viewer-card .card-logo { width: 64px; height: 64px; margin: 0 auto 12px; }
.viewer-card .card-name { font-size: 1.3rem; margin: 0 0 16px; }
.viewer-barcode { background: #fff; border-radius: 10px; padding: 8px; margin-top: 8px; }
.viewer-barcode svg { width: 100%; height: auto; }
.viewer-qr { margin: 8px auto; width: 200px; height: 200px; }
.viewer-qr svg, .viewer-qr canvas { width: 100%; height: 100%; }
.viewer-number { margin: 16px 0 0; font-size: 1.1rem; letter-spacing: 1px; font-weight: 700; }
.viewer-nav { display: flex; align-items: center; gap: 16px; margin-top: 24px; width: 100%; max-width: 420px; }
.viewer-nav .btn-ghost { flex: 0 0 auto; }
.viewer-nav span { flex: 1; text-align: center; color: var(--muted); }
.btn-delete { margin-top: 16px; width: 100%; max-width: 420px; flex: none; }
