:root {
  --bg: #f4efe6;
  --bg-2: #efe8db;
  --card: #fffdf8;
  --ink: #43413b;
  --ink-soft: #8a857a;
  --line: #e6ded0;
  --sage: #8ba888;
  --sage-deep: #6f8f6d;
  --rose: #cf9a86;
  --shadow: 0 6px 24px rgba(120, 110, 90, 0.10);
  --radius: 18px;
  --serif: "Fraunces", Georgia, "Times New Roman", serif;
  --sans: "Nunito Sans", system-ui, -apple-system, Segoe UI, sans-serif;
}

* { box-sizing: border-box; }

/* Make the hidden attribute win over any display rule below. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, #fbf7ee 0%, transparent 60%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ---------- LOGIN ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 40px 34px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}
.leaf { font-size: 44px; }
.login-card h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 40px;
  margin: 8px 0 2px;
  letter-spacing: 0.5px;
}
.tagline {
  font-family: var(--serif);
  font-style: italic;
  color: var(--ink-soft);
  margin: 0 0 26px;
  font-size: 16px;
}
.who {
  display: flex;
  gap: 12px;
  margin-bottom: 18px;
}
.who-btn {
  flex: 1;
  padding: 12px;
  border-radius: 14px;
  border: 1.5px solid var(--line);
  background: transparent;
  color: var(--ink);
  font-size: 16px;
  font-weight: 600;
  transition: all 0.18s ease;
}
.who-btn:hover { border-color: var(--sage); }
.who-btn.selected {
  background: var(--sage);
  border-color: var(--sage);
  color: #fff;
}
#login-form { display: flex; flex-direction: column; gap: 12px; }
#password {
  padding: 13px 16px;
  border-radius: 14px;
  border: 1.5px solid var(--line);
  background: #fdfbf6;
  font-size: 16px;
  outline: none;
  transition: border-color 0.18s ease;
}
#password:focus { border-color: var(--sage); }
#enter-btn {
  padding: 13px;
  border-radius: 14px;
  border: none;
  background: var(--sage-deep);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  transition: opacity 0.18s ease, transform 0.05s ease;
}
#enter-btn:disabled { opacity: 0.45; cursor: not-allowed; }
#enter-btn:not(:disabled):active { transform: translateY(1px); }
.error { color: #b5654e; font-size: 14px; margin: 4px 0 0; }

/* ---------- APP SHELL ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px clamp(14px, 4vw, 28px);
  background: rgba(244, 239, 230, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 500;
}
.leaf-sm { font-size: 18px; }
.tabs {
  display: flex;
  gap: 4px;
  margin: 0 auto;
  background: #ece5d7;
  padding: 4px;
  border-radius: 14px;
}
.tab {
  border: none;
  background: transparent;
  color: var(--ink-soft);
  padding: 8px 18px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 700;
  transition: all 0.15s ease;
}
.tab.active {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 1px 4px rgba(120, 110, 90, 0.12);
}
.me {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--ink-soft);
  font-size: 14px;
}
#logout {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-soft);
  padding: 6px 12px;
  border-radius: 10px;
  font-size: 13px;
}
#logout:hover { color: var(--ink); border-color: var(--rose); }

.view {
  max-width: 720px;
  margin: 0 auto;
  padding: 22px clamp(14px, 4vw, 20px) 60px;
}

/* ---------- CARDS / COMPOSE ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.compose { margin-bottom: 24px; }
.diary-title-input, .diary-body-input, .mood-input {
  width: 100%;
  border: none;
  background: transparent;
  outline: none;
  color: var(--ink);
}
.diary-title-input {
  font-family: var(--serif);
  font-size: 21px;
  font-weight: 500;
  padding: 2px 2px 8px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 10px;
}
.diary-body-input {
  font-size: 16px;
  line-height: 1.6;
  resize: vertical;
  min-height: 90px;
}
.compose-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 12px;
}
.mood-input {
  flex: 1;
  border: 1px solid var(--line);
  background: #fdfbf6;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
}
.primary {
  border: none;
  background: var(--sage-deep);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  padding: 11px 20px;
  border-radius: 12px;
  transition: opacity 0.15s ease, transform 0.05s ease;
  white-space: nowrap;
}
.primary:disabled { opacity: 0.45; cursor: not-allowed; }
.primary:not(:disabled):active { transform: translateY(1px); }

/* ---------- DIARY FEED ---------- */
.feed { display: flex; flex-direction: column; gap: 16px; }
.entry {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
}
.entry.by-Veera { border-left: 4px solid var(--rose); }
.entry.by-Ahad { border-left: 4px solid var(--sage); }
.entry-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.entry-author { font-weight: 700; font-size: 15px; }
.entry-meta { color: var(--ink-soft); font-size: 13px; }
.entry-mood {
  margin-left: auto;
  font-size: 13px;
  color: var(--sage-deep);
  background: #eef2ea;
  padding: 3px 10px;
  border-radius: 20px;
}
.entry-title {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 500;
  margin: 0 0 6px;
}
.entry-body { font-size: 16px; line-height: 1.7; white-space: pre-wrap; word-wrap: break-word; }

/* ---------- CHAT ---------- */
#view-chat { display: flex; flex-direction: column; height: calc(100dvh - 62px); padding-bottom: 14px; }
.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 2px 14px;
}
.bubble {
  max-width: 76%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 1px 3px rgba(120, 110, 90, 0.08);
}
.bubble .stamp { display: block; font-size: 11px; opacity: 0.6; margin-top: 4px; }
.bubble.mine { align-self: flex-end; background: var(--sage); color: #fff; border-bottom-right-radius: 5px; }
.bubble.theirs { align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.day-sep { align-self: center; font-size: 12px; color: var(--ink-soft); margin: 10px 0 4px; }
.chat-form { display: flex; gap: 10px; padding-top: 8px; border-top: 1px solid var(--line); }
#chat-input {
  flex: 1;
  border: 1.5px solid var(--line);
  background: #fdfbf6;
  border-radius: 14px;
  padding: 12px 16px;
  font-size: 16px;
  outline: none;
}
#chat-input:focus { border-color: var(--sage); }

/* ---------- SHARED ---------- */
.drop {
  display: block;
  border: 2px dashed var(--line);
  border-radius: 14px;
  padding: 26px;
  text-align: center;
  color: var(--ink-soft);
  cursor: pointer;
  transition: all 0.15s ease;
}
.drop:hover, .drop.hot { border-color: var(--sage); color: var(--sage-deep); background: #fbfdfa; }
.upload-status { margin-top: 10px; font-size: 14px; color: var(--ink-soft); }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
}
.item {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.item img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  background: #f0eadd;
}
.item .fileicon {
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  font-size: 40px;
  background: #f0eadd;
  text-decoration: none;
}
.item-meta { padding: 10px 12px; font-size: 13px; }
.item-caption { color: var(--ink); margin-bottom: 4px; word-wrap: break-word; }
.item-sub { color: var(--ink-soft); font-size: 12px; }
.item a.dl { color: var(--sage-deep); text-decoration: none; font-weight: 600; }

.empty {
  text-align: center;
  color: var(--ink-soft);
  font-family: var(--serif);
  font-style: italic;
  font-size: 17px;
  padding: 40px 20px;
}

@media (max-width: 560px) {
  .brand { display: none; }
  .tabs { margin: 0 auto 0 0; }
  .tab { padding: 8px 14px; }
}

/* ============================================================
   ANIMATIONS
   ============================================================ */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes popIn {
  0%   { opacity: 0; transform: scale(0.92); }
  60%  { opacity: 1; transform: scale(1.02); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes bubbleIn {
  0%   { opacity: 0; transform: translateY(8px) scale(0.96); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-14px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes loginOut {
  from { opacity: 1; transform: scale(1); filter: blur(0); }
  to   { opacity: 0; transform: scale(0.94); filter: blur(4px); }
}
@keyframes appIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes floaty {
  0%, 100% { transform: translateY(0) rotate(-2deg); }
  50%      { transform: translateY(-6px) rotate(2deg); }
}
@keyframes sway {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-2px); }
}

/* Login entrance + exit */
.login-card { animation: popIn 0.6s cubic-bezier(0.22, 1, 0.36, 1) both; }
.login.leaving { animation: loginOut 0.45s ease forwards; }
.leaf { display: inline-block; animation: floaty 4.5s ease-in-out infinite; }
.leaf-sm { display: inline-block; animation: sway 4s ease-in-out infinite; }
.who-btn, #password, #enter-btn { animation: fadeUp 0.5s ease both; }
.who { animation: fadeUp 0.5s ease 0.05s both; }
#login-form { animation: fadeUp 0.5s ease 0.12s both; }

/* App shell entrance */
#app.enter-anim { animation: appIn 0.6s ease both; }
#app.enter-anim .topbar { animation: slideDown 0.55s ease both; }

/* View (tab) entrance — replays each time a view is un-hidden */
.view:not([hidden]) { animation: fadeUp 0.42s ease both; }

/* Diary entries + shared items: gentle rise, staggered via inline delay */
.entry { animation: fadeUp 0.5s ease both; }
.item  { animation: popIn 0.45s ease both; }

/* Chat bubbles pop in as they arrive */
.bubble { animation: bubbleIn 0.32s ease both; }
.day-sep { animation: fadeIn 0.4s ease both; }

/* Hover lifts */
.entry, .item { transition: transform 0.2s ease, box-shadow 0.2s ease; }
.entry:hover, .item:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 30px rgba(120, 110, 90, 0.16);
}
.tab { transition: background 0.2s ease, color 0.2s ease, transform 0.1s ease; }
.tab:active { transform: scale(0.95); }
.primary, #enter-btn { transition: transform 0.08s ease, box-shadow 0.2s ease, opacity 0.15s ease; }
.primary:hover, #enter-btn:not(:disabled):hover {
  box-shadow: 0 8px 20px rgba(111, 143, 109, 0.35);
}
.who-btn { transition: all 0.18s ease, transform 0.1s ease; }
.who-btn:active { transform: scale(0.96); }

/* Respect users who prefer less motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ============================================================
   STATIC PEACEFUL BACKGROUNDS + PER-TAB THEMES + DARK MODE
   ============================================================ */
body { background-color: var(--bg); }
body[data-theme="diary"]  { --bg: #f4efe6; }
body[data-theme="chat"]   { --bg: #eceff7; }
body[data-theme="shared"] { --bg: #ecf3ea; }

/* One fixed, static, GPU-cheap gradient layer — no animation, no blur. */
#ambient { position: fixed; inset: 0; z-index: -1; pointer-events: none; transition: background 0.6s ease; }
body[data-theme="diary"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(236,214,178,0.65), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(233,196,170,0.60), transparent 72%),
    radial-gradient(520px 520px at 82% 12%, rgba(243,228,188,0.50), transparent 72%),
    #f4efe6;
}
body[data-theme="chat"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(196,208,242,0.60), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(216,203,242,0.58), transparent 72%),
    radial-gradient(520px 520px at 82% 12%, rgba(193,224,238,0.50), transparent 72%),
    #eceff7;
}
body[data-theme="shared"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(198,226,189,0.60), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(217,237,201,0.58), transparent 72%),
    radial-gradient(520px 520px at 82% 12%, rgba(191,227,209,0.50), transparent 72%),
    #ecf3ea;
}

/* topbar stays readable over any theme */
.topbar { background: rgba(255, 253, 248, 0.78); }

/* ---- Dark mode ---- */
body.dark {
  color-scheme: dark;
  --card: #26241f; --ink: #ece7dd; --ink-soft: #a69f92; --line: #39342d;
  --sage: #829d7f; --sage-deep: #94b191; --rose: #d3a692;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.42);
}
body.dark[data-theme="diary"]  { --bg: #1c1a17; }
body.dark[data-theme="chat"]   { --bg: #17181f; }
body.dark[data-theme="shared"] { --bg: #171c17; }
body.dark[data-theme="diary"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(120,96,60,0.28), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(110,78,66,0.26), transparent 72%),
    #1c1a17;
}
body.dark[data-theme="chat"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(78,92,150,0.30), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(96,80,150,0.26), transparent 72%),
    #17181f;
}
body.dark[data-theme="shared"] #ambient {
  background:
    radial-gradient(760px 620px at 10% 6%, rgba(70,110,72,0.30), transparent 72%),
    radial-gradient(680px 680px at 92% 96%, rgba(88,120,92,0.26), transparent 72%),
    #171c17;
}
body.dark .topbar { background: rgba(28, 26, 23, 0.75); }
body.dark .tabs { background: #201e1a; }
body.dark #password,
body.dark .mood-input,
body.dark #chat-input { background: #201e1a; }
body.dark .entry-mood { background: #2b332a; }
body.dark .drop:hover, body.dark .drop.hot { background: #22201c; }
body.dark .item .fileicon, body.dark .item img { background: #2b2924; }
body.dark .cal-day.has:hover { background: rgba(146, 177, 145, 0.18); }

/* ---- Dark / light toggle ---- */
#mode-toggle {
  position: fixed; top: 12px; right: 16px; z-index: 200;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  font-size: 18px; cursor: pointer; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.12s ease, background 0.3s ease, border-color 0.3s ease;
}
#mode-toggle:hover { transform: scale(1.08); }
#mode-toggle:active { transform: scale(0.94); }
.topbar .me { margin-right: 46px; } /* keep clear of the fixed toggle */

/* ---- Removable paste thumbnails ---- */
.thumb-wrap { position: relative; display: inline-block; line-height: 0; }
.thumb-x {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px;
  border-radius: 50%; border: 2px solid var(--card); background: #b5654e; color: #fff;
  font-size: 13px; line-height: 1; cursor: pointer; display: flex;
  align-items: center; justify-content: center; padding: 0;
}
.thumb-x:hover { background: #9c4f3c; }

/* ============================================================
   CALENDAR
   ============================================================ */
.calendar { margin-bottom: 24px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.cal-title { font-family: var(--serif); font-size: 19px; font-weight: 500; }
.cal-arrow {
  border: 1px solid var(--line); background: transparent; width: 34px; height: 34px;
  border-radius: 10px; font-size: 18px; color: var(--ink-soft); transition: all 0.15s ease;
}
.cal-arrow:hover { border-color: var(--sage); color: var(--sage-deep); }
.cal-weekdays {
  display: grid; grid-template-columns: repeat(7, 1fr); text-align: center;
  font-size: 12px; color: var(--ink-soft); font-weight: 700; margin-bottom: 6px;
}
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-day {
  aspect-ratio: 1 / 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; border-radius: 10px; font-size: 14px; color: var(--ink);
  border: 1px solid transparent; transition: all 0.15s ease;
}
.cal-day.empty { visibility: hidden; }
.cal-day.today { border-color: var(--sage); }
.cal-day.has { cursor: pointer; font-weight: 700; }
.cal-day.has:hover { background: rgba(139, 168, 136, 0.16); transform: scale(1.05); }
.cal-day.selected { background: var(--sage); color: #fff; }
.cal-dot { width: 5px; height: 5px; border-radius: 50%; background: var(--sage-deep); margin-top: 3px; }
.cal-day.selected .cal-dot { background: #fff; }
.cal-all { margin-top: 12px; border: none; background: transparent; color: var(--sage-deep); font-weight: 700; font-size: 14px; cursor: pointer; }
.cal-all:hover { text-decoration: underline; }
.filter-note { font-family: var(--serif); font-style: italic; color: var(--ink-soft); margin-bottom: 14px; font-size: 16px; }

/* ============================================================
   DIARY PASTE ATTACHMENTS + INLINE IMAGES
   ============================================================ */
.attachments { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.attachments .thumb {
  width: 58px; height: 58px; border-radius: 10px; object-fit: cover;
  border: 1px solid var(--line); animation: popIn 0.3s ease; box-shadow: var(--shadow);
}
.attachments .thumb.pending { opacity: 0.5; }
.diary-img { display: block; max-width: 100%; border-radius: 12px; margin: 12px 0; border: 1px solid var(--line); cursor: zoom-in; }

/* ============================================================
   MEDIA VIEWER (lightbox)
   ============================================================ */
.item img { cursor: zoom-in; }
.viewer {
  position: fixed; inset: 0; z-index: 100; background: rgba(38, 34, 28, 0.84);
  backdrop-filter: blur(10px); display: flex; align-items: center; justify-content: center;
  animation: fadeIn 0.25s ease;
}
.viewer-stage { margin: 0; max-width: 92vw; max-height: 86vh; display: flex; flex-direction: column; align-items: center; gap: 12px; }
#viewer-img {
  max-width: 92vw; max-height: 78vh; border-radius: 12px; object-fit: contain;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45); animation: popIn 0.32s ease; background: #1a1712;
}
.viewer-cap { color: #f4efe6; font-size: 14px; text-align: center; max-width: 80vw; }
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%); width: 54px; height: 54px;
  border-radius: 50%; border: none; background: rgba(255, 255, 255, 0.14); color: #fff;
  font-size: 30px; cursor: pointer; transition: background 0.15s ease, transform 0.1s ease;
  display: flex; align-items: center; justify-content: center;
}
.viewer-nav:hover { background: rgba(255, 255, 255, 0.28); }
.viewer-nav:active { transform: translateY(-50%) scale(0.92); }
.viewer-nav.prev { left: 18px; }
.viewer-nav.next { right: 18px; }
.viewer-nav[disabled] { opacity: 0.2; cursor: default; }
.viewer-close {
  position: absolute; top: 18px; right: 20px; width: 44px; height: 44px; border-radius: 50%;
  border: none; background: rgba(255, 255, 255, 0.14); color: #fff; font-size: 18px; cursor: pointer;
}
.viewer-close:hover { background: rgba(255, 255, 255, 0.28); }
.viewer-count { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); color: #f4efe6; font-size: 13px; letter-spacing: 0.5px; }
@media (max-width: 560px) { .viewer-nav { width: 46px; height: 46px; font-size: 26px; } }
