/* ============ CSS Variables & Reset ============ */
:root {
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --bg-card: #1a1a3e;
  --bg-card-hover: #222260;
  --bg-modal: #16163a;
  --border: rgba(255, 255, 255, 0.06);
  --border-active: rgba(120, 100, 255, 0.3);
  --text-primary: #e8e8f0;
  --text-secondary: #9090b8;
  --text-muted: #606088;
  --accent: #7c5cff;
  --accent-hover: #9078ff;
  --accent-glow: rgba(124, 92, 255, 0.25);
  --success: #34d399;
  --success-bg: rgba(52, 211, 153, 0.1);
  --danger: #f87171;
  --danger-bg: rgba(248, 113, 113, 0.1);
  --warning: #fbbf24;
  --warning-bg: rgba(251, 191, 36, 0.1);
  --extract: #38bdf8;
  --extract-bg: rgba(56, 189, 248, 0.1);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 48px rgba(0, 0, 0, 0.5);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

html {
  font-size: 16px;
  overflow-x: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* ============ Scrollbar ============ */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.1); border-radius: 2px; }

/* ============ Toast ============ */
.toast {
  position: fixed;
  top: calc(20px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%) translateY(-120%);
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  z-index: 10000;
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
  backdrop-filter: blur(20px);
}

.toast.show {
  transform: translateX(-50%) translateY(0);
}

.toast.success { border-color: var(--success); background: linear-gradient(135deg, rgba(52, 211, 153, 0.15), var(--bg-card)); }
.toast.error { border-color: var(--danger); background: linear-gradient(135deg, rgba(248, 113, 113, 0.15), var(--bg-card)); }
.toast.info { border-color: var(--extract); background: linear-gradient(135deg, rgba(56, 189, 248, 0.15), var(--bg-card)); }

/* ============ Header ============ */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(10, 10, 26, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  max-width: 600px;
  margin: 0 auto;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon { font-size: 24px; }

.logo-text {
  font-size: 18px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--extract));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  background: var(--accent-glow);
  border: 1px solid rgba(124, 92, 255, 0.2);
  border-radius: 20px;
  color: var(--accent-hover);
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: rgba(255, 255, 255, 0.05);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-icon:active { background: rgba(255, 255, 255, 0.1); transform: scale(0.92); }

.hidden { display: none !important; }

/* ============ Action Bar ============ */
.action-bar {
  padding: 16px 16px 8px;
  max-width: 600px;
  margin: 0 auto;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}

.action-btn::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity 0.3s;
}

.action-btn:active {
  transform: scale(0.95);
}

.action-product::before { background: linear-gradient(135deg, rgba(52, 211, 153, 0.08), transparent); }
.action-product:active::before { opacity: 1; }
.action-product { border-color: rgba(52, 211, 153, 0.15); }

.action-code::before { background: linear-gradient(135deg, rgba(124, 92, 255, 0.08), transparent); }
.action-code:active::before { opacity: 1; }
.action-code { border-color: rgba(124, 92, 255, 0.15); }

.action-extract::before { background: linear-gradient(135deg, rgba(56, 189, 248, 0.08), transparent); }
.action-extract:active::before { opacity: 1; }
.action-extract { border-color: rgba(56, 189, 248, 0.15); }

.action-icon { font-size: 24px; }

/* ============ Tabs ============ */
.tabs {
  display: flex;
  padding: 8px 16px 0;
  max-width: 600px;
  margin: 0 auto;
  gap: 4px;
}

.tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.25s;
}

.tab.active {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}

.tab-count {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

.tab.active .tab-count {
  background: var(--accent-glow);
  color: var(--accent-hover);
}

/* ============ Panels ============ */
.panel {
  display: none;
  padding: 12px 16px 100px;
  max-width: 600px;
  margin: 0 auto;
  animation: fadeIn 0.3s ease;
}

.panel.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============ List Items ============ */
.list { display: flex; flex-direction: column; gap: 8px; }

.list-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}

.list-item:active { background: var(--bg-card-hover); }

.list-item.used {
  opacity: 0.45;
  border-color: rgba(255, 255, 255, 0.03);
}

.list-item.used::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: repeating-linear-gradient(
    -45deg,
    transparent,
    transparent 8px,
    rgba(255, 255, 255, 0.01) 8px,
    rgba(255, 255, 255, 0.01) 16px
  );
  pointer-events: none;
}

.item-content {
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-primary);
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

.item-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.item-meta {
  font-size: 11px;
  color: var(--text-muted);
}

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

.item-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 6px;
}

.status-available {
  background: var(--success-bg);
  color: var(--success);
}

.status-used {
  background: var(--danger-bg);
  color: var(--danger);
}

.btn-take {
  padding: 6px 14px;
  background: linear-gradient(135deg, var(--accent), #6c4cef);
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-take:active { transform: scale(0.94); opacity: 0.9; }

.btn-delete {
  padding: 6px 10px;
  background: var(--danger-bg);
  border: 1px solid rgba(248, 113, 113, 0.15);
  border-radius: var(--radius-sm);
  color: var(--danger);
  font-size: 12px;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-delete:active { transform: scale(0.94); }

/* ============ Code List ============ */
.codes-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.code-stats {
  font-size: 13px;
  color: var(--text-secondary);
}

.code-stats strong {
  color: var(--text-primary);
  font-weight: 600;
}

.code-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
}

.code-text {
  font-size: 14px;
  font-weight: 600;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  letter-spacing: 1px;
}

.code-item.used .code-text {
  text-decoration: line-through;
}

/* ============ Empty State ============ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.4; }

.empty-state p {
  color: var(--text-muted);
  font-size: 15px;
  font-weight: 500;
}

/* ============ Modal ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  width: 100%;
  max-width: 500px;
  max-height: 85vh;
  background: var(--bg-modal);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  overflow-y: auto;
}

.modal-overlay.active .modal {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 12px;
  position: sticky;
  top: 0;
  background: var(--bg-modal);
  z-index: 1;
}

.modal-header h3 {
  font-size: 18px;
  font-weight: 700;
}

.modal-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
}

.modal-body {
  padding: 0 20px;
}

.modal-hint {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 14px;
  line-height: 1.5;
}

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 16px 20px;
  padding-bottom: calc(16px + var(--safe-bottom));
  position: sticky;
  bottom: 0;
  background: var(--bg-modal);
}

/* ============ Inputs ============ */
.input-textarea {
  width: 100%;
  padding: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  line-height: 1.6;
  resize: vertical;
  transition: border-color 0.2s;
}

.input-textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.input-text {
  width: 100%;
  padding: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 15px;
  font-family: var(--font);
  transition: border-color 0.2s;
}

.input-text:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* ============ Buttons ============ */
.btn {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.2s;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #6c4cef);
  color: white;
  box-shadow: 0 4px 16px rgba(124, 92, 255, 0.3);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-secondary);
}

.btn-sm {
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-sm:active { transform: scale(0.94); }

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(248, 113, 113, 0.15);
}

/* ============ Section Divider ============ */
.section-divider {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.section-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ============ Skeleton Loading ============ */
@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

.skeleton {
  background: linear-gradient(90deg, var(--bg-card) 25%, var(--bg-card-hover) 50%, var(--bg-card) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius);
  height: 80px;
  margin-bottom: 8px;
}

/* ============ Pulse animation for extract button ============ */
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(56, 189, 248, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(56, 189, 248, 0); }
}

.action-extract { animation: pulse 2.5s infinite; }

/* ============ Responsive Touch ============ */
@media (hover: hover) {
  .list-item:hover { background: var(--bg-card-hover); border-color: var(--border-active); }
  .action-btn:hover::before { opacity: 1; }
  .btn-icon:hover { background: rgba(255, 255, 255, 0.1); color: var(--text-primary); }
}

/* ============ Landscape Adjustments ============ */
@media (min-width: 600px) {
  .modal {
    border-radius: var(--radius-lg);
    border-bottom: 1px solid var(--border);
    max-height: 80vh;
  }

  .modal-overlay {
    align-items: center;
  }
  
  .modal-footer {
    padding-bottom: 16px;
  }
}
