/* ============================================================
   Tokens
   ============================================================ */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --fg: #0a0a0a;
  --muted: #6f6f6f;
  --subtle: #a3a3a3;
  --border: #ebebeb;
  --border-strong: #dcdcdc;
  --hover: #f3f3f3;
  --primary: #0a0a0a;
  --primary-fg: #ffffff;
  --blue: #006bff;
  --danger: #e5484d;
  --danger-soft: #fdecec;
  --success: #29a383;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.04);
  --shadow-lg: 0 0 0 1px rgb(0 0 0 / 0.06), 0 8px 24px -4px rgb(0 0 0 / 0.1), 0 2px 6px rgb(0 0 0 / 0.04);

  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);

  --radius: 12px;
  --font: "Geist", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "Geist Mono", ui-monospace, "SF Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0a;
    --surface: #111111;
    --surface-2: #171717;
    --fg: #ededed;
    --muted: #a1a1a1;
    --subtle: #6b6b6b;
    --border: #1f1f1f;
    --border-strong: #2e2e2e;
    --hover: #1a1a1a;
    --primary: #ededed;
    --primary-fg: #0a0a0a;
    --blue: #3b8bff;
    --danger: #ff6369;
    --danger-soft: #2a1314;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-lg: 0 0 0 1px rgb(255 255 255 / 0.08), 0 8px 24px -4px rgb(0 0 0 / 0.6);
    color-scheme: dark;
  }
}

/* ============================================================
   Base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.icon {
  width: 16px; height: 16px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.75;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ============================================================
   Layout
   ============================================================ */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 680px; margin: 0 auto; padding: 20px 16px 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -0.01em; }
.logo {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: var(--primary); color: var(--primary-fg);
}
.logo svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.chip {
  font-family: var(--mono); font-size: 12px; color: var(--muted);
  padding: 3px 10px; border: 1px solid var(--border); border-radius: 99px; background: var(--surface);
}

.container { max-width: 680px; margin: 0 auto; padding: 24px 16px 96px; display: grid; gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); color: var(--fg);
  font-size: 13px; font-weight: 500; white-space: nowrap;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
  transition: transform 160ms var(--ease-out), background-color 150ms ease, border-color 150ms ease, opacity 150ms ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn:disabled:active { transform: none; }
.btn-primary { background: var(--primary); color: var(--primary-fg); border-color: var(--primary); }
.btn-icon { width: 36px; padding: 0; }
.btn-block { width: 100%; }
.btn-danger-ghost { color: var(--danger); }

@media (hover: hover) and (pointer: fine) {
  .btn:hover { background: var(--hover); }
  .btn-primary:hover { background: var(--primary); opacity: 0.88; }
  .btn-danger-ghost:hover { background: var(--danger-soft); border-color: var(--danger-soft); }
}

/* ============================================================
   Hero / address
   ============================================================ */
.hero { padding: 20px; }
.hero-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.eyebrow { font-size: 13px; color: var(--muted); }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.live-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 20%, transparent); }
.live[data-state="expired"] .live-dot { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 20%, transparent); }

.address {
  display: block; width: 100%; text-align: left;
  padding: 14px 16px; margin: 0 0 14px;
  font-family: var(--mono); font-size: clamp(16px, 4.2vw, 22px); font-weight: 500; letter-spacing: -0.02em;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  transition: transform 160ms var(--ease-out), border-color 150ms ease;
}
.address:active { transform: scale(0.99); }
@media (hover: hover) and (pointer: fine) {
  .address:hover { border-color: var(--border-strong); }
}

.hero-row { display: flex; align-items: center; gap: 16px; }

/* Copy button: icon crossfade with blur */
.copy { min-width: 108px; }
.swap { position: relative; width: 16px; height: 16px; }
.swap .icon { position: absolute; inset: 0; transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out), filter 200ms var(--ease-out); }
.swap .icon-b { opacity: 0; transform: scale(0.6); filter: blur(4px); }
.copy[data-copied] .icon-a { opacity: 0; transform: scale(0.6); filter: blur(4px); }
.copy[data-copied] .icon-b { opacity: 1; transform: none; filter: none; }

.timer { flex: 1; min-width: 0; }
.timer-text { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.timer-text strong { color: var(--fg); font-family: var(--mono); font-weight: 500; font-variant-numeric: tabular-nums; transition: color 200ms ease; }
.timer-text strong[data-warn] { color: var(--danger); }
.track { height: 4px; border-radius: 99px; background: var(--border); overflow: hidden; }
.bar {
  height: 100%; background: var(--fg); border-radius: inherit;
  transform-origin: left; transform: scaleX(1);
  transition: transform 1s linear, background-color 200ms ease;
}
.bar[data-warn] { background: var(--danger); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }

/* Hold to delete */
.btn-hold { margin-left: auto; overflow: hidden; color: var(--muted); }
.hold-label, .hold-fill { display: inline-flex; align-items: center; gap: 8px; }
.hold-fill {
  position: absolute; inset: 0; justify-content: center;
  background: var(--danger-soft); color: var(--danger);
  clip-path: inset(0 100% 0 0);
  transition: clip-path 200ms var(--ease-out);
}
.btn-hold[data-holding] .hold-fill { clip-path: inset(0 0 0 0); transition: clip-path 2s linear; }
.btn-hold { -webkit-touch-callout: none; }

/* ============================================================
   Popover (origin-aware: scales from its trigger)
   ============================================================ */
.pop-anchor { position: relative; }
.popover {
  position: absolute; top: calc(100% + 8px); left: 0; z-index: 20;
  width: 300px; max-width: calc(100vw - 32px); padding: 14px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  transform-origin: top left;
  opacity: 0; transform: scale(0.96); visibility: hidden; pointer-events: none;
  transition: opacity 120ms ease, transform 120ms var(--ease-out), visibility 0s 120ms;
}
.popover[data-open] {
  opacity: 1; transform: scale(1); visibility: visible; pointer-events: auto;
  transition: opacity 150ms ease, transform 180ms var(--ease-out), visibility 0s;
}
.popover.instant { transition-duration: 0s; }
.pop-title { display: block; font-weight: 500; margin-bottom: 10px; }
.field {
  display: flex; align-items: center; height: 38px;
  border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface-2);
  overflow: hidden; transition: border-color 150ms ease, box-shadow 150ms ease;
}
.field:focus-within { border-color: var(--fg); box-shadow: 0 0 0 3px color-mix(in srgb, var(--fg) 10%, transparent); }
.field input { flex: 1; min-width: 0; height: 100%; padding: 0 10px; border: 0; background: transparent; outline: none; font-family: var(--mono); font-size: 16px; }
.field-suffix { padding: 0 10px; font-family: var(--mono); font-size: 13px; color: var(--muted); white-space: nowrap; }
.pop-hint { margin: 8px 0 12px; font-size: 12px; color: var(--muted); }

/* ============================================================
   Inbox
   ============================================================ */
.inbox { overflow: hidden; }
.inbox-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 14px 20px; border-bottom: 1px solid var(--border);
}
.inbox-head h2 { margin: 0; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.count {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 99px;
  display: inline-grid; place-items: center;
  font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
}
.inbox-tools { display: flex; align-items: center; gap: 10px; }

.btn-icon[data-loading] .icon { animation: spin 600ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; font-size: 13px; color: var(--muted); }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  position: relative; width: 30px; height: 18px; border-radius: 99px;
  background: var(--border-strong); transition: background-color 150ms ease;
}
.switch-thumb {
  position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.2);
  transition: transform 150ms var(--ease-out);
}
.switch input:checked + .switch-track { background: var(--fg); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(12px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--blue); outline-offset: 2px; }
@media (prefers-color-scheme: dark) { .switch-thumb { background: #0a0a0a; } .switch input:not(:checked) + .switch-track .switch-thumb { background: #a1a1a1; } }

.list { list-style: none; margin: 0; padding: 6px; }
.item {
  position: relative; width: 100%;
  display: grid; grid-template-columns: 36px 1fr; gap: 12px; align-items: start;
  padding: 12px 12px 12px 14px; text-align: left;
  background: transparent; border: 0; border-radius: 10px;
  transition: background-color 120ms ease, transform 160ms var(--ease-out);
}
.item:active { transform: scale(0.99); }
@media (hover: hover) and (pointer: fine) {
  .item:hover { background: var(--hover); }
}
.item + .item::before, li + li .item::before {
  content: ""; position: absolute; top: 0; left: 62px; right: 12px; height: 1px; background: var(--border);
}
.item[data-unread]::after {
  content: ""; position: absolute; left: 4px; top: 50%; width: 6px; height: 6px; margin-top: -3px;
  border-radius: 50%; background: var(--blue);
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 13px; font-weight: 600; text-transform: uppercase;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
}
.item-body { min-width: 0; display: grid; gap: 1px; }
.item-top { display: flex; justify-content: space-between; gap: 12px; }
.item-from, .item-subject, .item-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-from { font-weight: 500; }
.item-time { flex: none; font-size: 12px; color: var(--subtle); font-variant-numeric: tabular-nums; }
.item-subject { color: var(--fg); display: flex; align-items: center; gap: 6px; }
.item-subject .icon { width: 13px; height: 13px; color: var(--muted); }
.item-preview { font-size: 13px; color: var(--muted); }
.item:not([data-unread]) .item-from { font-weight: 400; color: var(--muted); }
.item[data-unread] .item-subject { font-weight: 500; }

/* Entrada escalonada solo para mensajes nuevos */
.item[data-enter] { animation: item-in 380ms var(--ease-out) both; animation-delay: var(--delay, 0ms); }
@keyframes item-in {
  from { opacity: 0; transform: translateY(8px); filter: blur(2px); }
}

.empty { padding: 56px 20px 60px; text-align: center; }
.empty[hidden] { display: none; }
.empty-icon {
  position: relative; width: 44px; height: 44px; margin: 0 auto 14px;
  display: grid; place-items: center; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
}
.empty-icon .icon { width: 20px; height: 20px; }
.pulse {
  position: absolute; top: -3px; right: -3px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--blue); border: 2px solid var(--surface);
}
.pulse::after {
  content: ""; position: absolute; inset: -2px; border-radius: 50%; background: var(--blue);
  animation: ping 1.6s var(--ease-out) infinite;
}
@keyframes ping { 0% { transform: scale(1); opacity: 0.5; } 100% { transform: scale(2.4); opacity: 0; } }
.empty p { margin: 0; font-weight: 500; }
.empty small { color: var(--muted); font-size: 13px; }

/* ============================================================
   Drawer (Vaul-like)
   ============================================================ */
.overlay {
  position: fixed; inset: 0; z-index: 30;
  background: rgb(0 0 0 / 0.4);
  opacity: 0; visibility: hidden;
  transition: opacity 500ms var(--ease-drawer), visibility 0s 500ms;
}
.overlay[data-open] { opacity: 1; visibility: visible; transition: opacity 500ms var(--ease-drawer), visibility 0s; }

.drawer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  width: 100%; max-width: 760px; height: 90dvh; margin: 0 auto;
  display: flex; flex-direction: column;
  background: var(--surface); border-radius: 16px 16px 0 0;
  box-shadow: var(--shadow-lg);
  transform: translateY(100%); visibility: hidden;
  transition: transform 500ms var(--ease-drawer), visibility 0s 500ms;
}
.drawer[data-open] { transform: translateY(0); visibility: visible; transition: transform 500ms var(--ease-drawer), visibility 0s; }
.drawer.dragging, .overlay.dragging, .drawer.instant, .overlay.instant { transition: none; }

.drawer-grab { padding: 0 20px 14px; border-bottom: 1px solid var(--border); touch-action: none; cursor: grab; user-select: none; }
.drawer.dragging .drawer-grab { cursor: grabbing; }
.handle { width: 40px; height: 5px; border-radius: 99px; background: var(--border-strong); margin: 10px auto 14px; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.sender { display: flex; align-items: center; gap: 10px; min-width: 0; }
.sender-text { display: grid; min-width: 0; }
.sender-text strong { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sender-text span { font-size: 12px; color: var(--muted); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-actions { display: flex; gap: 8px; flex: none; }
.drawer h3 { margin: 14px 0 6px; font-size: 18px; font-weight: 600; letter-spacing: -0.015em; line-height: 1.35; overflow-wrap: anywhere; user-select: text; cursor: auto; }
.drawer-meta { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.drawer-meta time { font-size: 13px; color: var(--muted); }

.segmented {
  position: relative; display: inline-grid; grid-template-columns: 1fr 1fr;
  padding: 2px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--border);
}
.segmented button {
  position: relative; z-index: 1; height: 26px; padding: 0 12px; border: 0; background: none;
  font-size: 12px; font-weight: 500; color: var(--muted); transition: color 150ms ease;
}
.seg-indicator {
  position: absolute; top: 2px; bottom: 2px; left: 2px; width: calc(50% - 2px);
  border-radius: 7px; background: var(--surface); box-shadow: 0 1px 2px rgb(0 0 0 / 0.08), 0 0 0 1px var(--border);
  transition: transform 220ms var(--ease-in-out);
}
.segmented[data-active="html"] button[data-tab="html"],
.segmented[data-active="text"] button[data-tab="text"] { color: var(--fg); }
.segmented[data-active="text"] .seg-indicator { transform: translateX(100%); }

.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:not(:empty) { margin-top: 12px; }
.attachment {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
  font-size: 12px; color: var(--fg); text-decoration: none;
  transition: transform 160ms var(--ease-out), border-color 150ms ease;
}
.attachment:active { transform: scale(0.97); }
.attachment small { color: var(--muted); }
.attachment .icon { width: 14px; height: 14px; color: var(--muted); }
@media (hover: hover) and (pointer: fine) { .attachment:hover { border-color: var(--border-strong); } }

.drawer-body { flex: 1; min-height: 0; display: flex; }
#vHtml { flex: 1; width: 100%; border: 0; background: #fff; }
#vText {
  flex: 1; margin: 0; padding: 20px; overflow: auto;
  font-family: var(--mono); font-size: 13px; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere;
}
#vText[hidden], #vHtml[hidden] { display: none; }

/* ============================================================
   Toasts (Sonner-like)
   ============================================================ */
.toaster {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 60;
  width: 356px; max-width: calc(100vw - 32px); height: var(--front-height, 0px);
  margin: 0; padding: 0; list-style: none;
  transform: translateX(-50%);
}
/* Rellena los huecos entre toasts expandidos para no perder el hover */
.toaster::before { content: ""; position: absolute; left: 0; right: 0; bottom: 100%; height: 0; }
.toaster[data-expanded]::before { height: var(--stack-height, 0px); }

.toast {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 13px 16px; border-radius: 12px;
  background: var(--surface); color: var(--fg); box-shadow: var(--shadow-lg);
  font-size: 13px; font-weight: 500;
  opacity: 0; transform: translateY(100%);
  transition: transform 400ms ease, opacity 400ms ease;
}
.toast .icon { width: 16px; height: 16px; }
.toast[data-type="success"] .icon { color: var(--success); }
.toast[data-type="error"] .icon { color: var(--danger); }
.toast[data-mounted] {
  opacity: 1;
  transform: translateY(calc(var(--index) * -14px)) scale(calc(1 - var(--index) * 0.05));
}
.toaster[data-expanded] .toast[data-mounted] { transform: translateY(calc(var(--offset) * -1)); }
.toast[data-mounted]:not([data-front]) > * { opacity: 0; transition: opacity 200ms ease; }
.toaster[data-expanded] .toast > * { opacity: 1; }
.toast[data-hidden] { opacity: 0; pointer-events: none; }
.toast[data-removed] { opacity: 0; transition: transform 400ms ease, opacity 200ms ease; }
.toast[data-removed][data-front] { transform: translateY(100%); }

/* ============================================================
   Responsive + accesibilidad
   ============================================================ */
@media (max-width: 560px) {
  .hero { padding: 16px; }
  .hero-row { flex-direction: column; align-items: stretch; gap: 14px; }
  .copy { width: 100%; }
  .toolbar { display: grid; grid-template-columns: 1fr 1fr; }
  .toolbar .btn { width: 100%; }
  .pop-anchor { grid-column: span 1; }
  .btn-hold { grid-column: 1 / -1; margin-left: 0; }
  .popover { width: calc(100vw - 64px); }
  .drawer { height: 94dvh; }
  .drawer-grab { padding: 0 16px 12px; }
  .switch-label { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .item[data-enter] { animation: fade-in 200ms ease both; }
  .pulse::after { animation: none; }
  .btn-icon[data-loading] .icon { animation-duration: 1.2s; }
  .drawer, .drawer[data-open] { transition: opacity 200ms ease, visibility 0s 200ms; transform: none; opacity: 0; }
  .drawer[data-open] { opacity: 1; transition: opacity 200ms ease, visibility 0s; }
  .toast, .toast[data-mounted], .toaster[data-expanded] .toast[data-mounted], .toast[data-removed][data-front] { transform: none; transition: opacity 200ms ease; }
  .toast[data-mounted]:not([data-front]) { opacity: 0; }
  .popover, .popover[data-open] { transform: none; }
  .btn:active, .item:active, .address:active, .attachment:active { transform: none; }
}
@keyframes fade-in { from { opacity: 0; } }
