:root {
  --paper: #fbf6ec;
  --paper-deep: #f3ead8;
  --ink: #2b2a33;
  --ink-soft: #6b6674;
  --rule: rgba(43, 42, 51, 0.14);
  --airmail-red: #d9480f;
  --airmail-blue: #1c64b8;
  --postal-navy: #1e3a5f;
  --accent: #d9480f;
  --font-hand: 'Caveat', 'Bradley Hand', 'Segoe Print', cursive;
  --font-serif: 'Fraunces', Georgia, serif;
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: var(--font-ui);
  background: #0b1020;
  color: var(--ink);
}

button {
  padding: 9px 16px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}

button:active:not(:disabled) {
  transform: scale(0.97);
}

button.secondary {
  background: var(--paper-deep);
  color: var(--ink);
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

.hint {
  font-weight: normal;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.error {
  margin: 8px 0 0;
  color: #c92a2a;
  font-size: 0.85rem;
  text-align: center;
}

.error:empty {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Layout: map + right panel ---------- */

.layout {
  display: flex;
  height: 100%;
  height: 100dvh;
}

.map-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
}

#map {
  position: absolute;
  inset: 0;
}

.banner {
  position: absolute;
  top: 16px;
  left: 16px;
  max-width: 320px;
  padding: 14px 18px;
  border-radius: 14px;
  background: rgba(251, 246, 236, 0.94);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.banner h1 {
  margin: 0 0 2px;
  font-family: var(--font-serif);
  font-size: 1.4rem;
  letter-spacing: -0.01em;
}

.banner p {
  margin: 0;
  font-family: var(--font-hand);
  font-size: 1.2rem;
  line-height: 1.2;
  color: var(--ink-soft);
}

/* ---------- Right panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  width: 380px;
  background: var(--paper);
  border-left: 1px solid var(--paper-deep);
}

.panel-handle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 18px 18px 14px;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: default;
  border-bottom: 1px solid var(--paper-deep);
}

.panel-handle:active:not(:disabled) {
  transform: none;
}

.panel-title {
  font-family: var(--font-serif);
  font-size: 1.2rem;
  font-weight: 700;
}

.panel-handle .hint {
  margin-left: auto;
}

.grip {
  display: none;
}

.panel-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--paper-deep);
}

.tab {
  padding: 6px 14px;
  background: none;
  color: var(--ink-soft);
  font-size: 0.85rem;
  font-weight: 500;
}

.tab.active {
  background: var(--postal-navy);
  color: #fff;
}

.table-wrap {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.notes-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.notes-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 8px;
  background: var(--paper);
  color: var(--ink-soft);
  font-size: 0.68rem;
  font-weight: 600;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  border-bottom: 1px solid var(--paper-deep);
}

.notes-table td {
  padding: 12px 8px;
  border-bottom: 1px dashed var(--rule);
  vertical-align: top;
}

.notes-table tbody tr {
  cursor: pointer;
  transition: background 0.15s ease;
}

.notes-table tbody tr:hover,
.notes-table tbody tr:focus-visible {
  background: #fff;
  outline: none;
}

.col-rank {
  width: 36px;
  text-align: center;
}

.notes-table td.col-rank {
  padding-top: 14px;
  font-family: var(--font-serif);
  font-weight: 700;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.notes-table tbody tr:nth-child(1) td.col-rank { color: #c08a00; }
.notes-table tbody tr:nth-child(2) td.col-rank { color: #8a8f98; }
.notes-table tbody tr:nth-child(3) td.col-rank { color: #a0522d; }

.col-votes {
  width: 1%;
  white-space: nowrap;
}

.notes-table .to {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--airmail-red);
}

.notes-table .snippet {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-family: var(--font-hand);
  font-size: 1.2rem;
  line-height: 1.15;
  word-break: break-word;
}

.notes-table .score {
  display: block;
  margin-top: 4px;
  text-align: center;
  font-size: 0.72rem;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.empty {
  padding: 32px 16px;
  text-align: center;
  font-family: var(--font-hand);
  font-size: 1.3rem;
  color: var(--ink-soft);
}

/* ---------- Voting ---------- */

.vote-control {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.vote-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 48px;
  min-height: 34px;
  padding: 4px 10px;
  border: 1px solid var(--rule);
  background: #fff;
  color: var(--ink-soft);
  font-size: 0.8rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.vote-btn:hover:not(:disabled) {
  border-color: var(--ink-soft);
}

.vote-btn.up.active {
  background: #ebfbee;
  border-color: #2b8a3e;
  color: #2b8a3e;
}

.vote-btn.down.active {
  background: #fff5f5;
  border-color: #c92a2a;
  color: #c92a2a;
}

/* ---------- Overlays (compose + view) ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(11, 16, 32, 0.45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  overflow-y: auto;
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  position: relative;
  width: min(620px, 100%);
  min-width: 0; /* flex items default to min-content width, which overflowed on phones */
  margin: auto;
  animation: card-in 0.35s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(24px) rotate(-3deg) scale(0.96);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.close-btn {
  position: absolute;
  top: -14px;
  right: -14px;
  z-index: 2;
  width: 36px;
  height: 36px;
  padding: 0;
  background: #fff;
  color: var(--ink);
  font-size: 1.4rem;
  line-height: 1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

/* ---------- The postcard ---------- */

.postcard {
  --stamp-sky: #74c0fc;
  --stamp-sea: #1c7ed6;
  --postmark-ink: rgba(30, 58, 95, 0.75);

  padding: 9px;
  border-radius: 6px;
  /* Airmail border */
  background: repeating-linear-gradient(
    -45deg,
    var(--airmail-red) 0 14px,
    #fff 14px 22px,
    var(--airmail-blue) 22px 36px,
    #fff 36px 44px
  );
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.15),
    0 18px 40px rgba(0, 0, 0, 0.35);
}

.postcard.theme-1 { --stamp-sky: #ffc9c9; --stamp-sea: #e8590c; --postmark-ink: rgba(165, 29, 42, 0.7); }
.postcard.theme-2 { --stamp-sky: #b2f2bb; --stamp-sea: #2b8a3e; --postmark-ink: rgba(43, 138, 62, 0.75); }
.postcard.theme-3 { --stamp-sky: #e5dbff; --stamp-sea: #6741d9; --postmark-ink: rgba(103, 65, 217, 0.7); }

.postcard-inner {
  display: grid;
  grid-template-columns: 1.25fr auto 1fr;
  gap: 18px;
  min-height: 300px;
  padding: 22px 24px 18px;
  border-radius: 2px;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(255, 255, 255, 0.8), transparent 60%),
    radial-gradient(ellipse at 100% 100%, rgba(214, 196, 160, 0.35), transparent 55%),
    var(--paper);
}

.pc-left {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.pc-message {
  flex: 1;
  margin: 0;
  font-family: var(--font-hand);
  font-size: 1.65rem;
  font-weight: 500;
  line-height: 34px;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: break-word;
  background-image: repeating-linear-gradient(
    transparent 0 33px,
    var(--rule) 33px 34px
  );
  background-attachment: local;
}

.pc-message-input {
  width: 100%;
  min-height: 204px;
  padding: 0;
  border: none;
  outline: none;
  resize: none;
  background-color: transparent;
}

.pc-message-input::placeholder {
  color: rgba(43, 42, 51, 0.35);
}

.pc-sign {
  margin: 10px 0 0;
  font-family: var(--font-hand);
  font-size: 1.3rem;
  color: var(--ink-soft);
  text-align: right;
}

.pc-divider {
  width: 1px;
  background: linear-gradient(transparent, var(--rule) 12%, var(--rule) 88%, transparent);
}

.pc-right {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.pc-stamp-row {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  gap: 4px;
  min-height: 100px;
}

/* Perforated stamp: dotted holes around the edge, artwork inset in the middle. */
.stamp {
  position: relative;
  flex: none;
  width: 78px;
  height: 94px;
  background: radial-gradient(circle, transparent 3px, #fff 3.5px) -5px -5px / 10px 10px;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25));
  transform: rotate(3deg);
}

.stamp-art {
  position: absolute;
  inset: 6px;
  overflow: hidden;
  background: #fff;
}

.stamp-art svg {
  display: block;
  width: 100%;
  height: 100%;
}

.postmark {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 92px;
  height: 92px;
  margin: 14px -30px 0 0;
  border: 2px solid var(--postmark-ink);
  border-radius: 50%;
  box-shadow: inset 0 0 0 4px var(--paper), inset 0 0 0 5px var(--postmark-ink);
  color: var(--postmark-ink);
  font-size: 0.55rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  line-height: 1.35;
  text-align: center;
  text-transform: uppercase;
  transform: rotate(-14deg);
  mix-blend-mode: multiply;
}

.postmark strong {
  font-size: 0.72rem;
  letter-spacing: 0.04em;
}

.postmark span:last-child {
  letter-spacing: 0.02em;
}

/* Wavy cancellation lines */
.postmark::before {
  content: '';
  position: absolute;
  right: 88px;
  top: 30px;
  width: 70px;
  height: 30px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='70' height='30'%3E%3Cg fill='none' stroke='%231e3a5f' stroke-opacity='.6' stroke-width='2'%3E%3Cpath d='M0 5q8.75-5 17.5 0t17.5 0 17.5 0 17.5 0'/%3E%3Cpath d='M0 15q8.75-5 17.5 0t17.5 0 17.5 0 17.5 0'/%3E%3Cpath d='M0 25q8.75-5 17.5 0t17.5 0 17.5 0 17.5 0'/%3E%3C/g%3E%3C/svg%3E") no-repeat;
}

.pc-address {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  flex: 1;
  padding-top: 20px;
}

.pc-label {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.pc-line {
  min-height: 38px;
  padding: 2px 0;
  border-bottom: 1px solid var(--rule);
  font-family: var(--font-hand);
  font-size: 1.55rem;
  font-weight: 700;
  line-height: 34px;
  color: var(--postal-navy);
  overflow-wrap: anywhere;
}

.pc-to-input {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--rule);
  border-radius: 0;
  outline: none;
  background: transparent;
}

.pc-to-input::placeholder {
  font-size: 1.25rem;
  font-weight: 500;
  color: rgba(43, 42, 51, 0.35);
}

.pc-to-input:focus,
.pc-message-input:focus {
  background-color: rgba(255, 255, 255, 0.35);
}

.no-contact {
  flex: 1;
  min-width: 0;
  font-style: italic;
  opacity: 0.85;
}

@media (max-width: 480px) {
  .no-contact {
    flex-basis: 100%;
    text-align: center;
  }
}

/* ---------- Postcard footer (votes, share, report) ---------- */

.pc-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(251, 246, 236, 0.96);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.actions {
  display: flex;
  gap: 8px;
}

.posted {
  width: 100%;
  margin: 0;
  font-family: var(--font-hand);
  font-size: 1.35rem;
  font-weight: 700;
  color: #2b8a3e;
}

.share-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.share-btn {
  padding: 6px 12px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
}

.share-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.share-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.report-btn {
  padding: 4px 10px;
  border: 1px solid transparent;
  background: none;
  color: var(--ink-soft);
  font-size: 0.75rem;
  font-weight: 500;
}

.report-btn:hover:not(:disabled) {
  color: #c92a2a;
  border-color: #c92a2a;
}

@media (prefers-reduced-motion: reduce) {
  .overlay-card {
    animation: none;
  }
}

/* ---------- Mobile ---------- */

@media (max-width: 768px) {
  .layout {
    display: block;
  }

  .map-wrap {
    position: absolute;
    inset: 0;
  }

  .banner {
    top: 10px;
    left: 10px;
    right: 60px;
    max-width: none;
    padding: 8px 12px;
  }

  .banner h1 {
    font-size: 1.05rem;
    margin: 0;
  }

  .banner p {
    font-size: 1rem;
  }

  /* Panel becomes a bottom sheet */
  .panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    width: auto;
    height: 75dvh;
    border-left: none;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -6px 28px rgba(0, 0, 0, 0.3);
    transform: translateY(calc(100% - 58px - env(safe-area-inset-bottom)));
    transition: transform 0.25s ease;
  }

  .panel.open {
    transform: translateY(0);
  }

  .panel-handle {
    position: relative;
    height: 58px;
    padding: 20px 16px 12px;
    border-radius: 18px 18px 0 0;
    cursor: pointer;
  }

  .grip {
    display: block;
    position: absolute;
    top: 7px;
    left: 50%;
    width: 40px;
    height: 4px;
    margin-left: -20px;
    border-radius: 2px;
    background: var(--rule);
  }

  .panel-body {
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Postcard stacks: stamp + address on top, message below */
  .overlay {
    align-items: flex-start;
    padding: 24px 10px calc(16px + env(safe-area-inset-bottom));
  }

  .close-btn {
    top: -12px;
    right: -4px;
  }

  .postcard {
    padding: 7px;
  }

  .postcard-inner {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    min-height: 0;
    padding: 16px 16px 14px;
  }

  .pc-divider {
    display: none;
  }

  .pc-right {
    order: -1;
    flex-direction: row-reverse;
    align-items: flex-end;
    gap: 10px;
  }

  .pc-stamp-row {
    min-height: 0;
  }

  .stamp {
    width: 62px;
    height: 76px;
  }

  .postmark {
    width: 72px;
    height: 72px;
    margin: 8px -24px 0 0;
    font-size: 0.45rem;
  }

  .postmark strong {
    font-size: 0.6rem;
  }

  .postmark span:last-child {
    font-size: 0.4rem;
    letter-spacing: 0;
  }

  .postmark::before {
    display: none;
  }

  .pc-address {
    padding-top: 0;
  }

  .pc-address .pc-line:not(:first-of-type) {
    display: none;
  }

  .pc-message {
    font-size: 1.45rem;
    line-height: 30px;
    background-image: repeating-linear-gradient(transparent 0 29px, var(--rule) 29px 30px);
  }

  .pc-message-input {
    min-height: 150px;
    font-size: 1.45rem; /* above 16px so iOS doesn't zoom on focus */
  }

  .pc-actions {
    justify-content: center;
  }
}
