:root {
  color-scheme: only light;
  --paper: #f8f4e7;
  --paper-deep: #efe5ca;
  --ink: #3d382d;
  --muted: #786f5d;
  --green: #547d36;
  --green-dark: #355c28;
  --green-light: #dfeeb9;
  --line: #d9cdae;
  font-family: 'Jua', -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 320px; min-height: 100%; background: var(--paper); }
body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 12%, rgb(219 234 176 / .58), transparent 25rem),
    radial-gradient(circle at 84% 30%, rgb(255 224 172 / .4), transparent 28rem),
    linear-gradient(180deg, #fbf8ef 0%, var(--paper) 54%, #f2ead7 100%);
  overflow-x: hidden;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.letter-page {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: max(32px, env(safe-area-inset-top)) 20px max(60px, env(safe-area-inset-bottom));
}

.letter-tree-section { text-align: center; }
.letter-hero { max-width: 760px; margin: 0 auto; }
.letter-kicker, .other-kicker, .dialog-header span {
  color: #7e9d45;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .18em;
}

.letter-hero h1 {
  margin: 10px 0 0;
  color: #3f612d;
  font-size: clamp(44px, 8vw, 78px);
  line-height: 1.05;
  letter-spacing: -.05em;
  text-shadow: 0 3px 0 #fff, 0 14px 30px rgb(89 111 48 / .14);
}

.letter-hero > p {
  margin: 17px 0 0;
  color: var(--muted);
  font-size: clamp(16px, 2.5vw, 22px);
  line-height: 1.55;
  word-break: keep-all;
}

.tree-viewport {
  width: min(100%, 780px);
  margin: 14px auto 0;
  overflow: auto;
  border-radius: 44px;
  background: rgb(255 255 255 / .28);
  scrollbar-color: #aabc80 transparent;
  scrollbar-width: thin;
}

.tree-canvas {
  position: relative;
  width: min(100%, 680px);
  aspect-ratio: 3 / 4;
  margin: 0 auto;
  overflow: hidden;
  transition: width .22s ease;
}

.tree-canvas > img { position: relative; z-index: 0; display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; user-select: none; }
.letter-leaves { position: absolute; inset: 0; z-index: 2; }

.tree-intro-pending .tree-canvas,
.tree-intro-pending .letter-hero { opacity: 0; }
.tree-intro-ready .tree-canvas { animation: tree-arrive .85s cubic-bezier(.2,.75,.22,1) both; }
.tree-intro-ready .letter-hero { animation: tree-copy-arrive .62s .24s ease-out both; }

@keyframes tree-arrive {
  from { opacity: 0; transform: translateY(24px) scale(.965); }
  to { opacity: 1; transform: none; }
}

@keyframes tree-copy-arrive {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.letter-leaf {
  position: absolute;
  left: calc(var(--leaf-x) * .1333333333%);
  top: calc(var(--leaf-y) * .1%);
  z-index: 2;
  width: clamp(34px, 6vw, 52px);
  height: clamp(34px, 6vw, 52px);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgb(255 255 255 / .001);
  transform: translate(-50%, -50%) rotate(var(--leaf-rotation)) scale(var(--leaf-scale)) scaleX(var(--leaf-flip));
  transform-origin: 50% 55%;
  transition: filter .14s ease, opacity .14s ease, background .14s ease;
  -webkit-tap-highlight-color: transparent;
}

.letter-leaf:focus-visible { z-index: 6; outline: 4px solid #375c2a; outline-offset: 3px; }

.compose-reveal {
  opacity: 0;
  transform: translateY(34px);
  transition: opacity .7s ease, transform .7s cubic-bezier(.2,.75,.22,1);
}
.compose-reveal.is-visible { opacity: 1; transform: none; }
body.tree-intro-pending .compose-reveal.is-visible { opacity: 0; transform: translateY(34px); }
body.tree-intro-ready .compose-reveal.is-visible { transition-delay: .32s; }

.letter-compose {
  position: relative;
  width: min(100%, 540px);
  margin: 34px auto 0;
  padding: 30px;
  overflow: hidden;
  border: 1px solid #ded1ad;
  border-radius: 12px;
  background:
    repeating-linear-gradient(180deg, transparent 0 31px, rgb(128 156 96 / .12) 31px 32px),
    linear-gradient(145deg, #fffdf4, #f8f0d9);
  box-shadow: 0 26px 58px -35px rgb(77 64 35 / .46), inset 0 1px 0 #fff;
  text-align: left;
  transition: opacity .24s ease, transform .24s ease;
}

.letter-compose.is-sending { opacity: .68; transform: scale(.985); }

.letter-compose::after {
  position: absolute;
  right: -45px;
  bottom: -52px;
  width: 150px;
  height: 105px;
  border-radius: 100% 0 0;
  background: rgb(193 166 101 / .12);
  content: '';
  transform: rotate(-28deg);
  pointer-events: none;
}

.compose-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.compose-heading span { color: #668044; font-size: 13px; }
.compose-heading h2 { margin: 3px 0 0; color: #3f602f; font-size: 29px; letter-spacing: -.03em; }
.letter-compose label, .letter-edit-panel label, .auth-dialog label { display: block; margin: 14px 0 7px; color: #52623f; font-size: 14px; }
.auth-dialog .field-guide { margin: 5px 0 0; font-size: 12px; }

textarea, input {
  width: 100%;
  border: 1px solid #b9c78f;
  border-radius: 16px;
  outline: none;
  color: #3b3b31;
  background: rgb(255 255 255 / .88);
  box-shadow: inset 0 2px 5px rgb(66 76 43 / .05);
}

textarea { min-height: 142px; padding: 15px 16px; resize: vertical; line-height: 1.65; }
input { height: 48px; padding: 0 15px; }
textarea:focus, input:focus { border-color: #658a46; box-shadow: 0 0 0 4px rgb(101 138 70 / .14); }

.letter-counter { margin-top: 6px; color: #708056; text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.letter-counter.is-over { color: #b8473f; font-weight: 800; }
.form-message { min-height: 20px; margin: 9px 2px 0; color: #ad413c; font-family: -apple-system, 'Apple SD Gothic Neo', sans-serif; font-size: 13px; line-height: 1.45; }
.form-message.is-success { color: #3e752f; }

.letter-submit, .primary-button {
  min-height: 50px;
  border: 0;
  border-radius: 15px;
  color: #fff;
  background: linear-gradient(135deg, #547d36, #79a64b);
  box-shadow: 0 12px 24px -14px #355c28;
}
.letter-submit { width: 100%; margin-top: 5px; font-size: 18px; }
.letter-submit:hover, .primary-button:hover { filter: brightness(1.05); transform: translateY(-1px); }
.letter-submit:disabled, textarea:disabled, input:disabled { cursor: not-allowed; opacity: .58; }

.other-content {
  width: min(100%, 900px);
  margin: 80px auto 0;
  padding-top: 46px;
  border-top: 1px solid #d9cdae;
  text-align: center;
}

#home-card-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 11px;
  margin-top: 24px;
}

.home-card {
  min-height: 82px;
  padding: 10px 6px;
  border: 1px solid rgb(70 55 60 / .12);
  border-radius: 18px;
  color: #302a3c;
  background: var(--scene-cream, var(--paper));
  color-scheme: only light;
  forced-color-adjust: none;
  font-size: clamp(17px, 2.2vw, 23px);
  box-shadow: none;
  transition: transform .14s ease, filter .14s ease;
}
.home-card:hover { filter: brightness(1.03); transform: translateY(-2px); }
.home-card:active { transform: scale(.97); }
.home-card:focus-visible { outline: 3px solid #4c6f35; outline-offset: 3px; }

#home-toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 100;
  width: max-content;
  max-width: calc(100vw - 28px);
  padding: 11px 17px;
  border: 1px solid #b9c995;
  border-radius: 999px;
  color: #405530;
  background: rgb(250 252 242 / .97);
  box-shadow: 0 14px 34px rgb(46 59 29 / .2);
  text-align: center;
}

#home-offline {
  position: fixed;
  inset: 0 0 auto;
  z-index: 120;
  padding: calc(7px + env(safe-area-inset-top)) 12px 7px;
  color: #fff;
  background: #a95734;
  text-align: center;
  font-size: 13px;
}

dialog {
  width: min(92vw, 570px);
  max-height: min(86dvh, 760px);
  padding: 0;
  overflow: visible;
  border: 0;
  border-radius: 26px;
  color: var(--ink);
  background: transparent;
}
dialog::backdrop { background: rgb(34 36 25 / .5); backdrop-filter: blur(5px); }
.dialog-card { position: relative; max-height: min(86dvh, 760px); padding: 25px; overflow: auto; border: 1px solid #ded3b6; border-radius: 26px; background: #fffdf6; box-shadow: 0 28px 70px rgb(34 35 23 / .28); }
.dialog-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.dialog-header h2 { margin: 4px 0 0; color: #425c32; font-size: 27px; }
.dialog-header small { display: block; margin-top: 4px; color: #918874; }
.dialog-actions { display: flex; gap: 4px; }
.icon-button { display: grid; width: 42px; height: 42px; place-items: center; border: 0; border-radius: 50%; color: #5d584c; background: #f4f0e3; font-size: 26px; }
.icon-button:hover { background: #e9efd7; }
.icon-button:focus-visible, .letter-menu button:focus-visible { outline: 3px solid #668947; outline-offset: 2px; }

.letter-menu { position: absolute; top: 69px; right: 63px; z-index: 3; display: grid; min-width: 130px; padding: 6px; border: 1px solid #ddd1b2; border-radius: 14px; background: #fffef9; box-shadow: 0 16px 34px rgb(54 49 35 / .18); }
.letter-menu button { padding: 10px 12px; border: 0; border-radius: 9px; color: #4a473d; background: transparent; text-align: left; }
.letter-menu button:hover { background: #eef4dc; }
#letter-delete-open { color: #a33f3b; }

.letter-view-content { min-height: 170px; margin: 23px 0 5px; padding: 22px; border: 1px solid #e2d7ba; border-radius: 18px; background: #fbf7e9; white-space: pre-wrap; overflow-wrap: anywhere; font-family: -apple-system, 'Apple SD Gothic Neo', sans-serif; font-size: 17px; line-height: 1.75; text-align: left; }
.letter-edit-panel { margin-top: 20px; text-align: left; }
.letter-edit-panel textarea { min-height: 190px; }
.auth-guide { margin: 18px 0 4px; color: var(--muted); line-height: 1.55; }

.dialog-button-row { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 12px; }
.dialog-button-row button { min-height: 46px; padding: 9px 14px; border-radius: 13px; }
.primary-button { border: 0; }
.secondary-button { border: 1px solid #d6caad; color: #5b5649; background: #f7f3e8; }
.secondary-button:hover { background: #edf2df; }

.flying-letter {
  position: fixed;
  z-index: 200;
  width: 58px;
  height: 44px;
  pointer-events: none;
}
.flying-paper,
.flying-leaf-shape { position: absolute; inset: 0; display: grid; place-items: center; }
.flying-paper {
  font-size: 42px;
  filter: drop-shadow(0 7px 7px rgb(67 53 31 / .22));
  animation: paper-fold-away 1.25s ease both;
}
.flying-leaf-shape {
  color: #82ae4d;
  opacity: 0;
  filter: drop-shadow(0 6px 5px rgb(49 72 26 / .25));
  animation: leaf-unfold 1.25s ease both;
}

@keyframes paper-fold-away {
  0%, 35% { opacity: 1; transform: scale(1) rotate(0); }
  55% { opacity: .35; transform: scale(.48) rotate(18deg); }
  70%, 100% { opacity: 0; transform: scale(.22) rotate(35deg); }
}

@keyframes leaf-unfold {
  0%, 42% { opacity: 0; transform: scale(.2) rotate(-20deg); }
  64% { opacity: 1; transform: scale(1.08) rotate(7deg); }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}

@media (max-width: 760px) {
  .letter-page { padding-inline: 13px; }
  .tree-viewport { border-radius: 28px; }
  .tree-canvas { width: 100%; }
  .letter-compose { padding: 23px 18px 25px; border-radius: 10px; }
  #home-card-grid { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
  .home-card { min-height: 70px; border-radius: 15px; }
  .other-content { margin-top: 60px; }
}

@media (max-width: 560px) {
  .letter-page { padding-top: max(24px, env(safe-area-inset-top)); }
  .letter-hero h1 { font-size: 47px; }
  .letter-hero > p { font-size: 16px; }
  dialog { width: calc(100vw - 18px); max-height: calc(100dvh - 18px); margin-bottom: 9px; }
  .dialog-card { max-height: calc(100dvh - 18px); padding: 20px 17px calc(20px + env(safe-area-inset-bottom)); border-radius: 23px; }
  .letter-dialog, .auth-dialog { margin-top: auto; }
  .letter-menu { top: 63px; right: 55px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
  .compose-reveal { opacity: 1; transform: none; }
}
