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

:root {
  --bg: #F5F0E8;
  --paper: #FFFDF8;
  --paper-shadow: #E8DFD0;
  --ink: #2C1810;
  --ink-light: #5C4A3A;
  --ink-faint: #9A8A7A;
  --accent: #8B3A3A;
  --accent-light: #B85C5C;
  --gold: #C4956A;
  --gold-light: #D4AD82;
  --gold-faint: rgba(196, 149, 106, 0.15);
  --rose: #D4A0A0;
  --cream: #FAF6F0;
  --error: #9B3030;
  --success: #3A6B4A;
}

html { font-size: 16px; }

body {
  font-family: 'Lora', Georgia, serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  line-height: 1.7;
  overflow-x: hidden;
}

/* Subtle paper texture overlay */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(ellipse at 20% 50%, rgba(196, 149, 106, 0.06) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 20%, rgba(139, 58, 58, 0.04) 0%, transparent 50%),
    radial-gradient(ellipse at 50% 80%, rgba(196, 149, 106, 0.05) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* Grain texture */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  opacity: 0.03;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 1;
}

.container {
  position: relative;
  z-index: 2;
  max-width: 720px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* ─── HEADER / BRANDING ─── */
.brand {
  text-align: center;
  margin-bottom: 2.5rem;
  opacity: 0;
  animation: fadeDown 0.8s ease-out 0.2s forwards;
}

.brand-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 1rem;
  opacity: 0.7;
}

.brand h1 {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-weight: 300;
  font-size: 1.1rem;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

/* ─── PAPER CARD ─── */
.paper {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.72) 0%, rgba(250, 246, 240, 0.52) 100%),
    var(--paper);
  width: 100%;
  border-radius: 2px;
  padding: 3rem 2.5rem;
  position: relative;
  border: 1px solid rgba(212, 173, 130, 0.14);
  box-shadow:
    0 1px 3px rgba(44, 24, 16, 0.04),
    0 4px 12px rgba(44, 24, 16, 0.06),
    0 12px 40px rgba(44, 24, 16, 0.08);
  opacity: 0;
  animation: fadeUp 0.8s ease-out 0.4s forwards;
}

/* Subtle top gold rule */
.paper::before {
  content: '';
  position: absolute;
  top: 0;
  left: 2.5rem;
  right: 2.5rem;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gold-light), transparent);
}

/* ─── COMPOSE VIEW ─── */
.compose-header {
  text-align: center;
  margin-bottom: 2.5rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--gold-faint);
}

.compose-header h2 {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-weight: 400;
  font-size: 2rem;
  color: var(--ink);
  margin-bottom: 0.5rem;
}

.compose-header p {
  font-size: 0.9rem;
  color: var(--ink-faint);
  font-style: italic;
  letter-spacing: 0.02em;
}

.field {
  margin-bottom: 1.8rem;
}

.field label {
  display: block;
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-light);
  margin-bottom: 0.6rem;
}

.field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

.field textarea {
  width: 100%;
  min-height: 450px;
  border: 1px solid var(--paper-shadow);
  border-radius: 2px;
  padding: 1.2rem 1.4rem;
  font-family: 'Lora', Georgia, serif;
  font-size: 1rem;
  line-height: 1.8;
  color: var(--ink);
  background: var(--cream);
  resize: vertical;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  outline: none;
}

.field textarea:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-faint);
}

.field textarea::placeholder {
  color: var(--ink-faint);
  font-style: italic;
}

.field input[type="password"],
.field input[type="text"] {
  width: 100%;
  border: 1px solid var(--paper-shadow);
  border-radius: 2px;
  padding: 0.85rem 1.2rem;
  font-family: 'Lora', Georgia, serif;
  font-size: 1rem;
  color: var(--ink);
  background: var(--cream);
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  outline: none;
}

.field input:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-faint);
}

.field input::placeholder {
  color: var(--ink-faint);
  font-style: italic;
}

.field .hint {
  font-size: 0.8rem;
  color: var(--ink-faint);
  margin-top: 0.45rem;
  font-style: italic;
}

/* ─── BUTTONS ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.9rem 2.2rem;
  border: none;
  border-radius: 2px;
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.btn-primary {
  background: var(--accent);
  color: var(--paper);
}

.btn-primary:hover {
  background: var(--accent-light);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(139, 58, 58, 0.25);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

.btn-secondary:hover {
  background: rgba(139, 58, 58, 0.05);
}

.btn-mini {
  padding: 0.45rem 0.8rem;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
}

.btn-gold {
  background: var(--gold);
  color: var(--paper);
}

.btn-gold:hover {
  background: var(--gold-light);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(196, 149, 106, 0.3);
}

.actions {
  display: flex;
  gap: 1rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

body.writing-focus-active {
  overflow: hidden;
}

body.writing-focus-active .brand,
body.writing-focus-active .site-footer,
body.writing-focus-active #compose-view .compose-header p,
body.writing-focus-active #passphrase-field,
body.writing-focus-active #compose-view .actions,
body.writing-focus-active #compose-view #url-output {
  display: none;
}

body.writing-focus-active .container {
  max-width: 100%;
  min-height: 100vh;
  padding: 0;
  display: block;
}

body.writing-focus-active #compose-view {
  position: fixed;
  inset: 1rem;
  z-index: 20;
  margin: 0;
  width: auto;
  max-width: none;
  padding: 1.6rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

body.writing-focus-active #compose-view .compose-header {
  margin-bottom: 1rem;
  padding-bottom: 1rem;
}

body.writing-focus-active #compose-view h2 {
  font-size: 1.5rem;
}

body.writing-focus-active #letter-field {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  margin-bottom: 0;
}

body.writing-focus-active #letter-content {
  flex: none;
  min-height: 220px;
  height: calc(100vh - 260px);
  max-height: 100%;
  min-width: min(92vw, 520px);
  font-size: 1.08rem;
  line-height: 1.95;
  background: #fff;
  resize: both;
  overflow: auto;
}

body.writing-focus-active #focus-write-btn {
  background: var(--accent);
  color: var(--paper);
  border-color: var(--accent);
}

/* ─── GENERATED URL ─── */
.url-output {
  margin-top: 2rem;
  padding: 1.5rem;
  background: var(--cream);
  border: 1px solid var(--paper-shadow);
  border-radius: 2px;
  display: none;
}

.url-output.visible { display: block; animation: fadeUp 0.4s ease-out; }

.url-output h3 {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--success);
  margin-bottom: 0.8rem;
}

.url-display {
  background: var(--paper);
  border: 1px solid var(--paper-shadow);
  border-radius: 2px;
  padding: 0.8rem 1rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.75rem;
  font-weight: 300;
  color: var(--ink-light);
  word-break: break-all;
  line-height: 1.6;
  max-height: 120px;
  overflow-y: auto;
  margin-bottom: 1rem;
}

.copy-feedback {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: 0.95rem;
  font-weight: 500;
  font-style: italic;
  letter-spacing: 0.04em;
  color: var(--success);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}

.copy-feedback.show {
  opacity: 1;
  transform: translateY(0);
}

.password-reveal {
  margin-top: 1.2rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--paper-shadow);
}

.password-reveal p {
  font-size: 0.85rem;
  color: var(--ink-light);
  margin-bottom: 0.4rem;
}

.password-reveal .derived {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.95rem;
  color: var(--accent);
  font-weight: 400;
  letter-spacing: 0.05em;
  user-select: all;
  background: var(--paper);
  padding: 0.5rem 0.8rem;
  border-radius: 2px;
  border: 1px dashed var(--gold-light);
}

/* ─── READ / UNLOCK VIEW ─── */
.unlock-view {
  text-align: center;
  padding: 1rem 0;
}

.seal {
  width: 80px;
  height: 80px;
  margin: 0 auto 2rem;
  position: relative;
}

.seal-circle {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, var(--accent-light), var(--accent) 60%, #6B2A2A);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow:
    0 2px 8px rgba(139, 58, 58, 0.3),
    inset 0 -2px 4px rgba(0,0,0,0.15),
    inset 0 2px 4px rgba(255,255,255,0.1);
  animation: sealPulse 3s ease-in-out infinite;
}

.seal-circle svg {
  width: 32px;
  height: 32px;
  fill: none;
  stroke: rgba(255, 253, 248, 0.7);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.unlock-view h2 {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-weight: 400;
  font-size: 1.8rem;
  color: var(--ink);
  margin-bottom: 0.5rem;
}

.unlock-view .subtitle {
  font-size: 0.9rem;
  color: var(--ink-faint);
  font-style: italic;
  margin-bottom: 2rem;
}

.unlock-field {
  max-width: 320px;
  margin: 0 auto 1.5rem;
}

.unlock-field input {
  width: 100%;
  text-align: center;
  border: 1px solid var(--paper-shadow);
  border-bottom: 2px solid var(--gold);
  border-radius: 2px;
  padding: 0.9rem 1.2rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: 1.05rem;
  letter-spacing: 0.1em;
  color: var(--ink);
  background: var(--cream);
  transition: all 0.3s ease;
  outline: none;
}

.unlock-field input:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-faint);
  border-bottom-color: var(--accent);
}

.error-msg {
  color: var(--error);
  font-size: 0.85rem;
  font-style: italic;
  margin-top: 0.8rem;
  display: none;
}

.error-msg.visible { display: block; animation: shake 0.4s ease-out; }

/* ─── LETTER VIEW ─── */
.letter-view {
  display: none;
}

.letter-view.visible { display: block; }

.letter-ornament {
  text-align: center;
  margin-bottom: 2rem;
  color: var(--gold);
  font-size: 1.4rem;
  letter-spacing: 0.5em;
  opacity: 0;
  animation: fadeDown 0.6s ease-out 0.2s forwards;
}

.letter-body {
  font-family: 'Lora', Georgia, serif;
  font-size: 1.05rem;
  line-height: 1.9;
  color: var(--ink);
  word-wrap: break-word;
  opacity: 0;
  animation: fadeUp 0.8s ease-out 0.4s forwards;
}

.letter-body p {
  margin-bottom: 1em;
  text-indent: 0;
}

.letter-footer {
  margin-top: 2.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--gold-faint);
  text-align: center;
  opacity: 0;
  animation: fadeUp 0.6s ease-out 0.8s forwards;
}

.letter-footer .ornament-end {
  color: var(--gold-light);
  font-size: 0.9rem;
  letter-spacing: 0.3em;
}

.letter-footer .timestamp {
  font-size: 0.75rem;
  color: var(--ink-faint);
  margin-top: 0.8rem;
  font-style: italic;
}

.reply-link {
  display: inline-block;
  margin-top: 1.1rem;
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: 1.02rem;
  letter-spacing: 0.03em;
  font-style: italic;
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid rgba(139, 58, 58, 0.28);
  padding-bottom: 0.05rem;
  transition: color 0.25s ease, border-color 0.25s ease;
}

.reply-link:hover {
  color: var(--accent-light);
  border-bottom-color: rgba(184, 92, 92, 0.65);
}

/* ─── FOOTER ─── */
.site-footer {
  text-align: center;
  margin-top: 2.5rem;
  opacity: 0;
  animation: fadeUp 0.6s ease-out 1s forwards;
}

.site-footer p {
  font-size: 0.72rem;
  color: var(--ink-faint);
  letter-spacing: 0.05em;
}

/* ─── ANIMATIONS ─── */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeDown {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes sealPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 2px 8px rgba(139,58,58,0.3), inset 0 -2px 4px rgba(0,0,0,0.15), inset 0 2px 4px rgba(255,255,255,0.1); }
  50% { transform: scale(1.03); box-shadow: 0 4px 16px rgba(139,58,58,0.35), inset 0 -2px 4px rgba(0,0,0,0.15), inset 0 2px 4px rgba(255,255,255,0.1); }
}

@keyframes revealLetter {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ─── RESPONSIVE ─── */
@media (max-width: 600px) {
  .container { padding: 1.5rem 1rem 3rem; }
  .paper { padding: 2rem 1.5rem; }
  .paper::before { left: 1.5rem; right: 1.5rem; }
  .compose-header h2, .unlock-view h2 { font-size: 1.5rem; }
  .field textarea { min-height: 330px; }
  .actions { flex-direction: column; }
  .btn { width: 100%; }
  .letter-body { font-size: 0.95rem; }
  body.writing-focus-active #letter-content {
    min-width: 100%;
    resize: vertical;
  }
}

@media (max-width: 380px) {
  .paper { padding: 1.5rem 1.2rem; }
  .compose-header h2, .unlock-view h2 { font-size: 1.3rem; }
}

/* ─── LOADING STATE ─── */
.loading { position: relative; pointer-events: none; }
.loading::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 253, 248, 0.7);
  border-radius: 2px;
}

/* ─── SCROLL BAR ─── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--cream); }
::-webkit-scrollbar-thumb { background: var(--paper-shadow); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--ink-faint); }

/* ─── HIDDEN ─── */
.hidden { display: none !important; }
