/* ── Variables ────────────────────────────────────────────────────────────── */
:root {
  --bg:          #f0f2f5;
  --surface:     #ffffff;
  --text:        #111827;
  --text-muted:  #6b7280;
  --border:      rgba(0,0,0,0.07);
  --accent:      #2563eb;
  --accent-dark: #1d4ed8;
  --danger:      #dc2626;
  --radius:      16px;
  --shadow:      0 2px 8px rgba(0,0,0,0.07);
  --shadow-lg:   0 8px 24px rgba(0,0,0,0.10);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:         #0f172a;
    --surface:    #1e293b;
    --text:       #f1f5f9;
    --text-muted: #94a3b8;
    --border:     rgba(255,255,255,0.07);
    --shadow:     0 2px 8px rgba(0,0,0,0.3);
    --shadow-lg:  0 8px 24px rgba(0,0,0,0.4);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overscroll-behavior: none;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
.header {
  padding: 20px 20px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.4px;
}

/* ── Card list ────────────────────────────────────────────────────────────── */
.card-list {
  list-style: none;
  padding: 16px 16px max(88px, calc(env(safe-area-inset-bottom) + 88px));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform .15s, opacity .15s;
  user-select: none;
}

.card-item:active {
  transform: scale(0.98);
  opacity: .85;
}

.card-avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  flex-shrink: 0;
}

.card-info {
  flex: 1;
  min-width: 0;
}

.card-name {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-format {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.card-chevron {
  color: var(--text-muted);
  font-size: 20px;
  font-weight: 300;
  flex-shrink: 0;
}

/* ── Empty state ──────────────────────────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 80px 40px max(120px, calc(env(safe-area-inset-bottom) + 120px));
  text-align: center;
  gap: 10px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 8px;
  line-height: 1;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
}

.empty-text {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.6;
  max-width: 280px;
}

/* ── FAB ──────────────────────────────────────────────────────────────────── */
.fab {
  position: fixed;
  bottom: max(24px, env(safe-area-inset-bottom, 24px));
  left: 50%;
  transform: translateX(-50%);
  width: min(calc(100vw - 32px), 448px);
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 20px rgba(37,99,235,.45);
  transition: background .15s, transform .15s;
  z-index: 20;
}

.fab:active {
  background: var(--accent-dark);
  transform: translateX(-50%) scale(.98);
}


/* ── Add card view ────────────────────────────────────────────────────────── */
.add-view {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.back-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.btn-back {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 0;
  display: flex;
  align-items: center;
  gap: 4px;
}

.back-header-title {
  font-size: 17px;
  font-weight: 600;
}

.add-body {
  flex: 1;
  padding: 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding-bottom: max(28px, env(safe-area-inset-bottom, 28px));
}

.code-preview {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px 20px 20px;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  box-shadow: var(--shadow);
}

.code-preview canvas {
  max-width: 100%;
  border-radius: 4px;
}

.preview-placeholder {
  font-size: 14px;
  color: var(--text-muted);
  text-align: center;
}

.preview-error {
  font-size: 13px;
  color: var(--danger);
  text-align: center;
  line-height: 1.4;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
}

.field input {
  background: var(--surface);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 16px;
  color: var(--text);
  outline: none;
  box-shadow: var(--shadow);
  transition: border-color .15s;
  width: 100%;
}

.field input::placeholder { color: var(--text-muted); }
.field input:focus        { border-color: var(--accent); }

.field select {
  background: var(--surface);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 14px 40px 14px 16px;
  font-size: 16px;
  color: var(--text);
  outline: none;
  box-shadow: var(--shadow);
  transition: border-color .15s;
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  cursor: pointer;
}

.field select:focus { border-color: var(--accent); }

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: background .15s, transform .15s;
}

.btn-primary:active   { background: var(--accent-dark); transform: scale(.98); }
.btn-primary:disabled { background: var(--text-muted); cursor: default; transform: none; }

/* ── Detail view ──────────────────────────────────────────────────────────── */
.detail-view {
  position: fixed;
  inset: 0;
  background: var(--surface);
  z-index: 100;
  display: flex;
  flex-direction: column;
}

.detail-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top, 12px));
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.detail-name {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-icon {
  width: 44px;
  height: 44px;
  background: none;
  border: none;
  border-radius: 10px;
  font-size: 20px;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}

.btn-icon:active       { background: var(--border); }
.btn-icon.is-danger    { color: var(--danger); }

.detail-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 20px;
  overflow: hidden;
}

.detail-canvas-wrap canvas {
  max-width: 100%;
  max-height: 50vh;
  display: block;
  border-radius: 4px;
}

.detail-code-text {
  font-family: ui-monospace, 'Cascadia Code', monospace;
  font-size: 15px;
  color: var(--text-muted);
  letter-spacing: .06em;
  word-break: break-all;
  text-align: center;
}

/* ── Delete confirm sheet ─────────────────────────────────────────────────── */
.delete-sheet {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px max(24px, env(safe-area-inset-bottom, 24px));
  box-shadow: 0 -4px 24px rgba(0,0,0,.12);
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: slide-up .2s ease-out;
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.delete-sheet p {
  text-align: center;
  font-size: 16px;
  font-weight: 600;
}

.delete-sheet-actions {
  display: flex;
  gap: 10px;
}

.btn-danger {
  flex: 1;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 15px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}

.btn-danger:active { opacity: .8; }

.btn-secondary {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: none;
  border-radius: 12px;
  padding: 15px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}

.btn-secondary:active { opacity: .7; }

/* ── Toast ────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: max(100px, calc(env(safe-area-inset-bottom) + 100px));
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text);
  color: var(--surface);
  padding: 10px 22px;
  border-radius: 100px;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  z-index: 200;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
