:root {
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --keyboard-offset: 0px;
  --bg: #02030a;
  --panel: rgba(10, 14, 24, 0.72);
  --border: rgba(255, 255, 255, 0.18);
  --text: rgba(245, 248, 255, 0.92);
  --muted: rgba(220, 228, 242, 0.68);
  --button: #d9deec;
  --button-text: #0f1524;
  --button-theme-bg: linear-gradient(160deg, rgba(245, 201, 106, 0.95), rgba(214, 227, 255, 0.9));
  --button-theme-bg-hover: linear-gradient(160deg, rgba(251, 208, 118, 0.98), rgba(222, 233, 255, 0.94));
  --button-theme-border: rgba(255, 255, 255, 0.22);
  --button-theme-border-hover: rgba(255, 255, 255, 0.3);
  --button-theme-text: #0f1524;
  --button-theme-shadow: 0 8px 18px rgba(0, 0, 0, 0.35);
  --button-theme-shadow-hover: 0 10px 20px rgba(0, 0, 0, 0.4);
  --button-theme-hover-opacity: 0.96;
  --radius-panel: 26px;
  --radius-card: 24px;
  --radius-control: 20px;
  --radius-pill: 999px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: radial-gradient(circle at 20% 20%, #07122e 0%, #03050f 35%, #000 100%);
  color: var(--text);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  user-select: none;
  -webkit-user-select: none;
  pointer-events: none;
}

#app canvas {
  touch-action: none;
  display: block;
  pointer-events: auto;
}

#entry-input,
#entry-full,
.auth-input,
#modal-card {
  user-select: text;
  -webkit-user-select: text;
}

#entry-input,
.auth-input,
#date-filter-input {
  touch-action: manipulation;
}

button {
  -webkit-tap-highlight-color: transparent;
}

#ui-panel {
  position: fixed;
  left: 50%;
  bottom: max(18px, calc(var(--sab) + 10px + var(--keyboard-offset)));
  transform: translateX(-50%);
  width: min(92vw, 780px);
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  z-index: 20;
  transform-origin: center bottom;
  transition: transform 0.42s cubic-bezier(0.2, 0.82, 0.18, 1), opacity 0.28s ease, filter 0.28s ease, bottom 0.22s ease;
}

body.composer-hidden #ui-panel {
  transform: translateX(-50%) translateY(132px) scale(0.92);
  opacity: 0;
  filter: blur(7px);
  pointer-events: none;
}

#entry-compose {
  position: relative;
  grid-column: 1 / -1;
  transition: transform 0.2s ease;
}

#entry-input {
  --compose-height: 64px;
  width: 100%;
  display: block;
  height: var(--compose-height);
  min-height: var(--compose-height);
  max-height: 180px;
  resize: none;
  border-radius: var(--radius-card);
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(4, 8, 18, 0.8);
  color: var(--text);
  padding:
    calc((var(--compose-height) - 1.4em - 2px) / 2)
    68px
    calc((var(--compose-height) - 1.4em - 2px) / 2)
    16px;
  font-size: 14px;
  line-height: 1.4;
  outline: none;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(170, 178, 196, 0.42) rgba(8, 12, 22, 0.32);
}

#entry-input::placeholder {
  color: var(--muted);
}

#entry-input::-webkit-scrollbar {
  width: 8px;
}

#entry-input::-webkit-scrollbar-track {
  background: rgba(8, 12, 22, 0.24);
  border-radius: var(--radius-pill);
}

#entry-input::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, rgba(149, 157, 175, 0.65), rgba(95, 103, 120, 0.68));
  border-radius: var(--radius-pill);
  border: 1px solid rgba(8, 12, 22, 0.45);
}

#submit-btn,
.auth-btn,
#close-modal,
#focus-toggle-btn {
  border: 1px solid var(--button-theme-border);
  background: var(--button-theme-bg);
  color: var(--button-theme-text);
  box-shadow: var(--button-theme-shadow);
  transition: transform 0.2s ease, opacity 0.16s ease, box-shadow 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

#submit-btn:hover,
.auth-btn:hover,
#close-modal:hover,
#focus-toggle-btn:hover {
  transform: translateY(-1px);
  opacity: var(--button-theme-hover-opacity);
  background: var(--button-theme-bg-hover);
  border-color: var(--button-theme-border-hover);
  box-shadow: var(--button-theme-shadow-hover);
}

#submit-btn:active,
.auth-btn:active,
#close-modal:active,
#focus-toggle-btn:active {
  transform: translateY(1px);
  opacity: 0.88;
}

#focus-toggle-btn {
  position: fixed;
  right: max(18px, calc(var(--sar) + 14px));
  bottom: max(18px, calc(var(--sab) + 10px));
  z-index: 24;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
}

.focus-icon {
  grid-area: 1 / 1;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: opacity 0.22s ease, transform 0.24s ease;
}

.focus-show-icon {
  opacity: 0;
  transform: scale(0.56) rotate(-16deg);
}

#focus-toggle-btn[data-mode="hidden"] .focus-hide-icon {
  opacity: 0;
  transform: scale(0.56) rotate(16deg);
}

#focus-toggle-btn[data-mode="hidden"] .focus-show-icon {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}

#focus-toggle-btn.focus-toggle-bounce {
  animation: focusToggleBounce 0.35s cubic-bezier(0.2, 0.82, 0.18, 1);
}

#focus-tip {
  position: fixed;
  right: max(18px, calc(var(--sar) + 14px));
  bottom: max(72px, calc(var(--sab) + 66px));
  z-index: 23;
  width: min(90vw, 350px);
  padding: 12px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: linear-gradient(160deg, rgba(245, 201, 106, 0.13), rgba(114, 166, 255, 0.1) 58%, rgba(16, 24, 42, 0.18)), rgba(10, 15, 26, 0.92);
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(10px);
  display: grid;
  gap: 10px;
  opacity: 0;
  transform: translateY(8px) scale(0.96);
  transition: opacity 0.22s ease, transform 0.25s ease;
}

#focus-tip.open {
  opacity: 1;
  transform: translateY(0) scale(1);
}

#focus-tip-text {
  margin: 0;
  font-size: 12px;
  line-height: 1.4;
  color: rgba(245, 248, 255, 0.92);
}

#focus-tip-close {
  justify-self: end;
  height: 28px;
  padding: 0 10px;
  font-size: 11px;
}

#submit-btn {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.submit-icon {
  grid-area: 1 / 1;
  transition: opacity 0.22s ease, transform 0.24s ease;
}

.chevron-icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 1;
  transform: scale(1);
}

.lock-icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0;
  transform: scale(0.74) rotate(-8deg);
}

#submit-btn:disabled,
.auth-btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

#entry-compose.locked #entry-input {
  border-color: rgba(138, 151, 176, 0.35);
  background: rgba(6, 10, 20, 0.82);
  color: rgba(213, 222, 238, 0.92);
  cursor: not-allowed;
}

#entry-compose.locked #submit-btn {
  background: linear-gradient(160deg, rgba(62, 73, 96, 0.9), rgba(96, 113, 140, 0.88));
  border-color: rgba(171, 185, 210, 0.42);
  color: rgba(235, 243, 255, 0.94);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.34);
}

#entry-compose.locked .chevron-icon {
  opacity: 0;
  transform: scale(0.68) rotate(7deg);
}

#entry-compose.locked .lock-icon {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}

#entry-compose.locked-shake {
  animation: lockedShake 0.34s ease;
}

#entry-compose.unlock-burst .lock-icon {
  animation: lockBreak 0.42s cubic-bezier(0.2, 0.82, 0.18, 1) forwards;
}

#entry-compose.unlock-burst .chevron-icon {
  animation: chevronSnapIn 0.36s 0.08s cubic-bezier(0.2, 0.82, 0.18, 1) both;
}

#auth-panel.snap-collapse {
  animation: authSnapCollapse 0.42s cubic-bezier(0.2, 0.82, 0.18, 1);
}

#auth-panel.auth-pulse {
  animation: authPulse 0.4s ease;
}

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

@keyframes lockBreak {
  0% {
    opacity: 1;
    transform: scale(1) rotate(0deg);
  }
  45% {
    opacity: 1;
    transform: scale(1.15) rotate(-10deg);
  }
  100% {
    opacity: 0;
    transform: scale(0.46) rotate(24deg);
  }
}

@keyframes chevronSnapIn {
  0% {
    opacity: 0;
    transform: scale(0.52) rotate(-14deg);
  }
  65% {
    opacity: 1;
    transform: scale(1.14) rotate(4deg);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(0deg);
  }
}

@keyframes authSnapCollapse {
  0% {
    transform: translateX(0) scale(1);
  }
  40% {
    transform: translateX(8px) scale(0.94);
  }
  72% {
    transform: translateX(-2px) scale(1.02);
  }
  100% {
    transform: translateX(0) scale(1);
  }
}

@keyframes authPulse {
  0% {
    box-shadow: 0 0 0 rgba(245, 201, 106, 0);
  }
  40% {
    box-shadow: 0 0 0 2px rgba(245, 201, 106, 0.38);
  }
  100% {
    box-shadow: 0 0 0 rgba(245, 201, 106, 0);
  }
}

@keyframes focusToggleBounce {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(0.88);
  }
  68% {
    transform: scale(1.07);
  }
  100% {
    transform: scale(1);
  }
}

#tooltip {
  position: fixed;
  pointer-events: none;
  z-index: 25;
  max-width: min(360px, 82vw);
  background: rgba(6, 10, 20, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 14px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text);
  opacity: 0;
  transform: translate(-50%, calc(-100% - 12px));
  transition: opacity 0.12s ease;
  white-space: normal;
}

#reminder-banner {
  grid-column: 1 / -1;
  width: 100%;
  background:
    linear-gradient(160deg, rgba(245, 201, 106, 0.12), rgba(114, 166, 255, 0.08) 62%, rgba(16, 22, 38, 0.12)),
    rgba(10, 15, 26, 0.9);
  border: 1px solid rgba(245, 201, 106, 0.38);
  border-radius: var(--radius-card);
  padding: 12px 16px;
  display: none;
  grid-template-columns: 1fr auto;
  gap: 10px 12px;
  align-items: center;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(8px);
}

#reminder-banner.open {
  display: grid;
}

#reminder-head {
  display: flex;
  align-items: center;
  gap: 0;
  justify-content: flex-start;
}

#reminder-text {
  margin: 0;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  color: rgba(250, 243, 230, 0.95);
}

#reminder-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-self: end;
}

#ios-install-hint {
  grid-column: 1 / -1;
  margin: 1px 0 0;
  color: rgba(226, 212, 183, 0.85);
  font-size: 12px;
  line-height: 1.35;
}

#modal {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(0, 0, 0, 0.6);
  z-index: 30;
  padding: 18px;
}

#modal.open {
  display: grid;
}

#modal-card {
  width: min(680px, 94vw);
  max-height: min(78vh, 760px);
  overflow: auto;
  background: rgba(8, 12, 22, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: var(--radius-panel);
  padding: 16px;
}

#modal-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 12px;
}

#modal-title {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

#close-modal {
  border-radius: var(--radius-pill);
  width: 30px;
  height: 30px;
  padding: 0;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.close-icon {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
}

#entry-meta {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
}

#entry-full {
  margin: 0;
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 14px;
  color: var(--text);
}

#hint {
  position: fixed;
  top: max(12px, calc(var(--sat) + 4px));
  left: 50%;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.02em;
  z-index: 12;
  text-align: center;
  pointer-events: none;
  width: min(92vw, 780px);
  line-height: 1.35;
}

#auth-panel {
  position: fixed;
  top: max(12px, calc(var(--sat) + 4px));
  right: max(12px, calc(var(--sar) + 12px));
  width: min(92vw, 320px);
  display: grid;
  gap: 8px;
  padding: 12px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-panel);
  backdrop-filter: blur(8px);
  z-index: 22;
  transform-origin: top right;
}

#auth-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.auth-input {
  width: 100%;
  height: 34px;
  border-radius: var(--radius-control);
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(4, 8, 18, 0.8);
  color: var(--text);
  padding: 0 12px;
  font-size: 12px;
  outline: none;
}

#auth-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.auth-btn {
  height: 30px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  padding: 0 10px;
}

#logout-btn {
  margin-left: auto;
}

#auth-status {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

#auth-panel.signed-in {
  width: auto;
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  backdrop-filter: none;
  gap: 0;
}

#auth-panel.signed-in #auth-row,
#auth-panel.signed-in #signup-btn,
#auth-panel.signed-in #login-btn,
#auth-panel.signed-in #auth-status {
  display: none;
}

#auth-panel.signed-in #auth-actions {
  display: block;
}

#auth-panel.signed-in #logout-btn {
  margin-left: 0;
  height: 36px;
  padding: 0 14px;
}

/* ── Date Filter ─────────────────────────────────────────── */

#date-filter {
  position: fixed;
  top: max(12px, calc(var(--sat) + 4px));
  left: max(12px, calc(var(--sal) + 12px));
  z-index: 22;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

#date-filter-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
  position: relative;
  flex-shrink: 0;
  transition: transform 0.2s ease, opacity 0.16s ease, box-shadow 0.25s ease, background 0.2s ease, border-color 0.2s ease;
}

#date-filter-btn.active {
  box-shadow: 0 0 0 2px rgba(245, 201, 106, 0.6), 0 0 14px rgba(245, 201, 106, 0.25), 0 8px 18px rgba(0, 0, 0, 0.35);
}

#date-filter-btn.active::after {
  content: '';
  position: absolute;
  top: 7px;
  right: 7px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(245, 201, 106, 0.95);
  box-shadow: 0 0 6px rgba(245, 201, 106, 0.8);
}

.date-filter-icon {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#date-filter-popover {
  width: min(88vw, 268px);
  padding: 13px 14px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background:
    linear-gradient(160deg, rgba(245, 201, 106, 0.09), rgba(114, 166, 255, 0.07) 58%, rgba(16, 24, 42, 0.1)),
    rgba(10, 15, 26, 0.94);
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(12px);
  display: grid;
  gap: 10px;
  opacity: 0;
  transform: translateY(-6px) scale(0.95);
  transition: opacity 0.22s ease, transform 0.25s cubic-bezier(0.2, 0.82, 0.18, 1);
}

#date-filter-popover.open {
  opacity: 1;
  transform: translateY(0) scale(1);
}

#date-filter-label {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: rgba(220, 228, 242, 0.6);
}

#date-filter-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

#date-filter-input {
  flex: 1;
  min-width: 0;
  height: 30px;
  border-radius: var(--radius-control);
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(4, 8, 18, 0.85);
  color: var(--text);
  padding: 0 10px;
  font-size: 12px;
  font-family: inherit;
  outline: none;
  color-scheme: dark;
  transition: border-color 0.18s ease;
}

#date-filter-input:focus {
  border-color: rgba(245, 201, 106, 0.45);
}

#date-filter-clear {
  height: 30px;
  padding: 0 10px;
  font-size: 11px;
  flex-shrink: 0;
}

/* ── End Date Filter ──────────────────────────────────────── */

/* Hide hover-only tooltip on touch devices */
@media (hover: none) {
  #tooltip { display: none; }
}

/* Slide-up animation for mobile bottom-sheet modal */
@keyframes sheetSlideUp {
  from { transform: translateY(100%); opacity: 0.6; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* Respect user's reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  *, ::before, ::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

@media (max-width: 640px) {
  /* ── Auth panel — centered card when signed out ── */
  #auth-panel:not(.signed-in) {
    top: max(36px, calc(var(--sat) + 26px));
    right: auto;
    left: 50%;
    transform: translateX(-50%);
    width: min(88vw, 340px);
  }

  /* Signed-in: just the logout button, back to top-right */
  #auth-panel.signed-in {
    left: auto;
    right: max(12px, calc(var(--sar) + 12px));
    top: max(12px, calc(var(--sat) + 4px));
    transform: none;
  }

  /* Stack email/password inputs vertically */
  #auth-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  /* ── Inputs: 16px prevents iOS auto-zoom, comfortable height ── */
  .auth-input {
    height: 40px;
    font-size: 16px;
    border-radius: var(--radius-card);
  }

  #entry-input {
    --compose-height: 80px;
    height: var(--compose-height);
    min-height: var(--compose-height);
    font-size: 16px;
  }

  #date-filter-input {
    font-size: 16px;
    height: 40px;
  }

  /* ── Touch targets: comfortable without being oversized ── */
  .auth-btn {
    min-height: 40px;
    font-size: 13px;
    padding: 0 14px;
  }

  #submit-btn {
    width: 44px;
    height: 44px;
  }

  #close-modal {
    width: 44px;
    height: 44px;
  }

  #date-filter-btn {
    width: 46px;
    height: 46px;
  }

  #date-filter-clear {
    min-height: 40px;
    padding: 0 12px;
  }

  /* ── Reminder banner ── */
  #reminder-banner {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 10px;
    align-items: center;
  }

  #reminder-head {
    align-items: center;
    justify-content: flex-start;
  }

  #reminder-actions {
    justify-self: stretch;
  }

  #enable-push-btn {
    width: 100%;
    min-height: 40px;
  }

  /* ── Focus controls — safe-area-aware ── */
  #focus-toggle-btn {
    right: max(12px, calc(var(--sar) + 8px));
    bottom: max(12px, calc(var(--sab) + 8px));
    width: 46px;
    height: 46px;
  }

  #focus-tip {
    left: 12px;
    right: max(12px, calc(var(--sar) + 8px));
    bottom: max(68px, calc(var(--sab) + 62px));
    width: auto;
  }

  /* ── Modal → native bottom sheet ── */
  #modal {
    padding: 0;
  }

  #modal.open {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
  }

  #modal-card {
    width: 100%;
    max-height: 88vh;
    max-height: 88dvh;
    border-radius: var(--radius-panel) var(--radius-panel) 0 0;
    padding-bottom: max(24px, calc(var(--sab) + 16px));
    animation: sheetSlideUp 0.32s cubic-bezier(0.22, 0.82, 0.18, 1) both;
  }
}
