:root {
  --bg: #f8f9fb;
  --bg-card: #ffffff;
  --bg-input: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-text: #ffffff;
  --accent: #10b981;
  --error: #ef4444;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-input: #1e293b;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --border: #334155;
  --primary: #818cf8;
  --primary-hover: #6366f1;
  --primary-text: #0f172a;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background 0.3s, color 0.3s;
}

/* HEADER */
.header {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-inner {
  max-width: 800px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--primary);
}
.logo-text {
  font-size: 1.2rem;
  font-weight: 700;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* BUTTONS */
.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
  border: none;
  padding: 12px 28px;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  white-space: nowrap;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.97); }

.btn-ghost {
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-ghost:hover { background: var(--border); }

.btn-small {
  background: var(--primary);
  color: var(--primary-text);
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-small:hover { background: var(--primary-hover); }

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-icon:hover { background: var(--border); }

.select-mini {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

/* THEME TOGGLE ICONS */
[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }

/* MAIN */
.main {
  flex: 1;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
}

.page { display: none; }
.page.active { display: block; }

/* HOME */
.hero {
  text-align: center;
  padding: 60px 0 40px;
}
.hero h1 {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--primary);
}
.subtitle {
  color: var(--text-secondary);
  font-size: 1.05rem;
  margin-bottom: 32px;
}
.input-group {
  display: flex;
  gap: 10px;
  max-width: 520px;
  margin: 0 auto;
}
.input-wrapper {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
}
.input-wrapper input {
  width: 100%;
  padding: 14px 16px;
  padding-right: min(46vw, 200px);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  background: var(--bg-input);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
.input-wrapper input:focus { border-color: var(--primary); }
.key-input-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 520px;
  margin: 12px auto 0;
  padding: 14px 18px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  transition: border-color 0.2s;
}
.key-input-wrapper:focus-within { border-color: var(--primary); }
.key-icon {
  flex-shrink: 0;
  color: var(--text-secondary);
}
.key-input-wrapper:focus-within .key-icon { color: var(--primary); }
.mailbox-key-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-align: center;
  outline: none;
}
.domain-select {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  background: var(--bg-input);
  cursor: pointer;
  width: clamp(108px, 34vw, 190px);
  min-width: 0;
  text-overflow: ellipsis;
}
.generate-row {
  margin-top: 12px;
  display: flex;
  justify-content: center;
}
.btn-accent {
  background: var(--accent);
  color: #ffffff;
  border: none;
  padding: 10px 24px;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-accent:hover { background: #059669; }
.btn-accent:active { transform: scale(0.97); }
.error-text {
  color: var(--error);
  font-size: 0.875rem;
  margin-top: 10px;
}

/* INBOX */
.inbox-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.inbox-email-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.inbox-email {
  font-size: 1rem;
  font-weight: 600;
  color: var(--primary);
  word-break: break-all;
}
.auto-refresh-panel {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.auto-refresh-note {
  color: var(--text-secondary);
  font-size: 0.8rem;
}
.refresh-countdown {
  color: var(--text);
  font-size: 0.85rem;
  margin-top: 2px;
}
.refresh-countdown strong {
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}
.hourglass {
  width: 30px;
  height: 38px;
  border: 2px solid var(--primary);
  border-radius: 12px;
  position: relative;
  overflow: hidden;
  flex: 0 0 auto;
  animation: hourglass-flip 3s ease-in-out infinite;
}
.hourglass::before,
.hourglass::after {
  content: '';
  position: absolute;
  left: 6px;
  right: 6px;
  height: 1px;
  background: var(--primary);
}
.hourglass::before { top: 8px; }
.hourglass::after { bottom: 8px; }
.hourglass span {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  height: 18px;
  background: linear-gradient(to top, var(--primary), transparent);
  border-radius: 0 0 8px 8px;
  animation: sand-flow 1.5s ease-in-out infinite alternate;
}
@keyframes hourglass-flip {
  0%, 80%, 100% { transform: rotate(0deg); }
  90% { transform: rotate(180deg); }
}
@keyframes sand-flow {
  from { height: 4px; opacity: 0.55; }
  to { height: 18px; opacity: 1; }
}

/* MESSAGE LIST */
.message-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.message-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  cursor: pointer;
  transition: box-shadow 0.2s, border-color 0.2s;
  display: flex;
  align-items: center;
  gap: 14px;
}
.message-card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow);
}
.message-card .mc-from {
  font-size: 0.82rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.message-card .mc-subject {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.message-card .mc-date {
  font-size: 0.8rem;
  color: var(--text-secondary);
}
.message-card .mc-code {
  background: linear-gradient(135deg, var(--primary), var(--primary-hover));
  border-radius: 12px;
  color: white;
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  min-width: 128px;
  padding: 12px 14px;
  text-align: center;
  box-shadow: 0 10px 24px rgba(79, 70, 229, 0.22);
  font-variant-numeric: tabular-nums;
}
.key-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(135deg, #f0fdf4, #dcfce7);
  border: 2px solid #22c55e;
  border-radius: var(--radius-sm);
  padding: 14px 20px;
  margin-bottom: 12px;
  font-size: 0.95rem;
  color: #166534;
}
[data-theme="dark"] .key-banner {
  background: linear-gradient(135deg, #052e16, #14532d);
  border-color: #22c55e;
  color: #86efac;
}
.key-banner .key-icon { flex-shrink: 0; color: #22c55e; }
.key-banner strong {
  font-size: 1.3rem;
  letter-spacing: 0.15em;
  font-variant-numeric: tabular-nums;
  color: #15803d;
  background: rgba(34, 197, 94, 0.12);
  padding: 4px 12px;
  border-radius: 8px;
}
[data-theme="dark"] .key-banner strong { color: #4ade80; background: rgba(34, 197, 94, 0.2); }
.btn-copy-key {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 6px;
  padding: 5px 12px;
  border: 1px solid #22c55e;
  border-radius: 6px;
  background: rgba(34, 197, 94, 0.1);
  color: #15803d;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.btn-copy-key:hover { background: rgba(34, 197, 94, 0.22); }
.btn-copy-key:active { transform: scale(0.95); }
[data-theme="dark"] .btn-copy-key { color: #86efac; border-color: #22c55e; background: rgba(34, 197, 94, 0.15); }
[data-theme="dark"] .btn-copy-key:hover { background: rgba(34, 197, 94, 0.3); }
.message-card .mc-content {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.empty-state {
  text-align: center;
  color: var(--text-secondary);
  padding: 40px 0;
  font-size: 1rem;
}

/* MODAL */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal[hidden] { display: none; }
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
}
.modal-content {
  position: relative;
  background: var(--bg-card);
  border-radius: var(--radius);
  width: 90%;
  max-width: 700px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  padding: 24px;
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.modal-title {
  font-size: 1.15rem;
  font-weight: 700;
  word-break: break-word;
}
.modal-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.modal-body {
  font-size: 0.95rem;
  line-height: 1.6;
  word-break: break-word;
  overflow-x: auto;
}
.modal-body img { max-width: 100%; height: auto; }

/* FOOTER */
.footer {
  text-align: center;
  padding: 16px 20px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
}

/* LOADING */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* WARNINGS */
.inbox-warnings,
.modal-warnings {
  margin-bottom: 12px;
}
.warning-banner {
  background: #fef2f2;
  border: 1px solid var(--error);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  color: #991b1b;
  font-size: 0.9rem;
  line-height: 1.5;
}
[data-theme="dark"] .warning-banner {
  background: #450a0a;
  border-color: #ef4444;
  color: #fca5a5;
}
.warning-banner strong {
  font-weight: 700;
}
.warning-normalized {
  font-family: monospace;
  background: rgba(0,0,0,0.08);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.85rem;
}
[data-theme="dark"] .warning-normalized {
  background: rgba(255,255,255,0.1);
}
.warning-details {
  margin-top: 8px;
  margin-left: 18px;
  font-size: 0.82rem;
  color: inherit;
  opacity: 0.9;
}
.warning-details li {
  margin-bottom: 2px;
}

/* RESPONSIVE */
@media (max-width: 600px) {
  .hero h1 { font-size: 1.8rem; }
  .input-group { flex-direction: column; }
  .input-wrapper input { padding-right: min(54vw, 190px); }
  .domain-select { width: min(48vw, 180px); }
  .btn-primary { width: 100%; }
  .inbox-header { flex-direction: column; align-items: flex-start; }
  .message-card { align-items: flex-start; flex-direction: column; }
  .message-card .mc-code { min-width: 0; width: 100%; }
  .modal-content { width: 95%; padding: 16px; }
}
