@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap");
@import url("tokens.css");

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  font-family: var(--font-sans);
  font-feature-settings: "cv11", "ss01";
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  background: var(--paper-1);
  color: var(--ink-0);
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-size: var(--fs-4);
  line-height: var(--lh-snug);
  overscroll-behavior: none;
}

::selection { background: var(--blue-0); color: #fff; }

:focus-visible {
  outline: 2px solid var(--blue-0);
  outline-offset: 2px;
  border-radius: var(--r-1);
}

/* Topbar */
.topbar {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--line-1);
  background: var(--paper-1);
  padding-top: calc(var(--sp-3) + env(safe-area-inset-top, 0px));
}

/* DESKTOP GRID TOPBAR */
.topbar-row-1 {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 36px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-self: start;
}

.topbar-right {
  display: flex;
  align-items: center;
  justify-self: end;
}

.topbar-row-2 {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  width: 100%;
  justify-content: space-between;
}

.title {
  grid-column: 2;
  justify-self: center;
  font-size: var(--fs-5);
  font-weight: 600;
  letter-spacing: -0.01em;
  text-transform: capitalize;
  color: var(--ink-0);
  white-space: nowrap;
  position: static;
  transform: none;
}

/* Time & Weather Widget */
.time-weather {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-3);
  font-weight: 500;
  color: var(--ink-1);
  background: var(--paper-2);
  padding: 4px 10px;
  border-radius: var(--r-pill);
  border: 1px solid var(--line-1);
  height: 34px;
  box-sizing: border-box;
}

.tw-time, .tw-weather {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.tw-weather[hidden] { display: none !important; }
.tw-time svg { color: var(--ink-2); width: 14px; height: 14px; }
.tw-weather svg { color: var(--ink-2); height: 14px; width: auto; display: block; }

.tw-sep {
  width: 1px;
  height: 14px;
  background: var(--line-2);
  margin: 0 2px;
}
.tw-sep[hidden] { display: none !important; }

/* Desktop: hide install button */
@media (min-width: 721px) {
  #installBtn { display: none !important; }
}

/* Buttons */
.btn {
  padding: 7px 14px;
  border-radius: var(--r-1);
  border: 1px solid var(--line-1);
  background: transparent;
  color: var(--ink-0);
  font-size: var(--fs-3);
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  line-height: 1;
  transition: all var(--dur-fast) var(--ease-squircle);
  touch-action: manipulation;
}

.btn svg, .btn-icon svg, .fab svg { display: block; flex-shrink: 0; }

.btn:hover { border-color: var(--line-2); background: var(--paper-2); }
.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--blue-0);
  color: #fff;
  border-color: var(--blue-0);
  font-weight: 600;
}

.btn-primary:hover {
  background: var(--blue-1);
  border-color: var(--blue-1);
  box-shadow: 0 0 0 4px var(--blue-tint);
}

.btn-danger {
  color: var(--now);
  border-color: var(--line-1);
  background: transparent;
}

.btn-danger:hover {
  background: var(--now);
  color: #fff;
  border-color: var(--now);
}

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--paper-2); border-color: transparent; }

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--r-1);
  border: 1px solid var(--line-1);
  background: transparent;
  color: var(--ink-1);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all var(--dur-fast) var(--ease-squircle);
  touch-action: manipulation;
  flex-shrink: 0;
}

.btn-icon:hover {
  border-color: var(--ink-1);
  color: var(--ink-0);
  background: var(--paper-2);
}

/* View switch */
.view-switch {
  display: inline-flex;
  border: 1px solid var(--line-1);
  border-radius: var(--r-1);
  overflow: hidden;
  background: var(--paper-2);
  padding: 2px;
  gap: 2px;
}

.view-btn {
  border: none;
  border-radius: 3px;
  padding: 5px 12px;
  font-size: var(--fs-3);
  font-weight: 500;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  font-family: inherit;
  transition: all var(--dur-fast) var(--ease-squircle);
  touch-action: manipulation;
  white-space: nowrap;
}

.view-btn:hover { color: var(--ink-0); }
.view-btn.active {
  background: var(--paper-1);
  color: var(--ink-0);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

/* Calendar container */
.calendar {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: var(--line-2) transparent;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.calendar::-webkit-scrollbar { width: 10px; height: 10px; }
.calendar::-webkit-scrollbar-track { background: transparent; }
.calendar::-webkit-scrollbar-thumb {
  background: var(--line-1);
  border-radius: var(--r-pill);
  border: 2px solid var(--paper-1);
}

/* DAY VIEW */
.day-header {
  display: grid;
  grid-template-columns: 60px 1fr;
  position: sticky;
  top: 0;
  background: var(--paper-1);
  border-bottom: 1px solid var(--line-1);
  z-index: 2;
}
.day-header[hidden] { display: none; }
.day-header-spacer { border-right: 1px solid var(--line-1); }
.day-header-cell {
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-4);
  font-weight: 500;
  color: var(--ink-0);
  text-transform: capitalize;
}
.day-header-cell.today { color: var(--blue-0); font-weight: 600; }
.day-grid {
  display: grid;
  grid-template-columns: 60px 1fr;
  flex: 1;
  min-height: calc(var(--hour-height) * 24);
}
.day-grid[hidden] { display: none; }
.day-grid > .hour-labels { position: relative; }
.day-grid > .day-col {
  border-left: 1px solid var(--line-1);
  position: relative;
  cursor: crosshair;
}

/* WEEK VIEW */
.week-header {
  display: grid;
  grid-template-columns: 60px repeat(7, 1fr);
  position: sticky;
  top: 0;
  background: var(--paper-1);
  border-bottom: 1px solid var(--line-1);
  z-index: 2;
}
.week-header[hidden] { display: none; }
.week-day-head {
  padding: var(--sp-3) 0;
  text-align: center;
  font-size: var(--fs-1);
  font-weight: 500;
  color: var(--ink-2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.week-day-head.today { color: var(--blue-0); }
.week-day-num {
  display: block;
  font-size: var(--fs-6);
  font-weight: 500;
  color: var(--ink-0);
  margin-top: 2px;
  line-height: 1;
}
.week-day-head.today .week-day-num { color: var(--blue-0); font-weight: 600; }
.week-day-head.today .week-day-num::after {
  content: "";
  display: inline-block;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--blue-0);
  margin-left: 6px;
  vertical-align: middle;
  position: relative;
  top: -2px;
}
.week-grid {
  display: grid;
  grid-template-columns: 60px repeat(7, 1fr);
  position: relative;
  flex: 1;
}
.week-grid[hidden] { display: none; }

.hour-labels { position: relative; }
.hour-label {
  height: var(--hour-height);
  font-size: var(--fs-1);
  color: var(--ink-3);
  text-align: right;
  padding-right: var(--sp-2);
  position: relative;
  top: -7px;
}
.day-col {
  border-left: 1px solid var(--line-1);
  position: relative;
  min-height: calc(var(--hour-height) * 24);
  cursor: crosshair;
}
.hour-line {
  position: absolute;
  left: 0; right: 0;
  border-top: 1px solid var(--line-1);
  opacity: 0.55;
  pointer-events: none;
}

/* Events */
.event {
  position: absolute;
  left: 3px; right: 3px;
  border-radius: var(--r-1);
  padding: 4px 8px 4px 7px;
  font-size: var(--fs-2);
  font-weight: 500;
  color: #fff;
  overflow: hidden;
  cursor: grab;
  border-left: 3px solid rgba(0, 0, 0, 0.22);
  transition: transform var(--dur-fast), box-shadow var(--dur-base), filter var(--dur-fast);
  z-index: 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  flex-direction: column;
}
.event:active { cursor: grabbing; }
.event:hover {
  transform: translateY(-1px) translateX(1px);
  filter: brightness(1.08);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 2;
}
.event.event-new { animation: eventPop var(--dur-slow) var(--ease-spring); }
.event-title { font-weight: 600; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.event-time { font-size: var(--fs-1); opacity: 0.82; margin-top: 1px; }

.event.dragging { opacity: 0.3; cursor: grabbing; }
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  opacity: 0.95;
  transform: scale(1.02);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25), 0 4px 8px rgba(0, 0, 0, 0.1);
  cursor: grabbing;
}
.day-col.drag-over { background: var(--blue-tint) !important; }

/* Resize Handle */
.resize-handle {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 12px;
  cursor: ns-resize;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 2px;
  opacity: 0;
  transition: opacity var(--dur-fast);
}
.event:hover .resize-handle { opacity: 1; }
.resize-handle svg { pointer-events: none; }

/* Now line */
.now-line {
  position: absolute;
  left: -1px; right: -1px;
  height: 2px;
  background: var(--now);
  z-index: 3;
  box-shadow: 0 0 14px 1px var(--now-glow);
  pointer-events: none;
}
.now-line::before {
  content: "";
  position: absolute;
  left: -5px; top: 50%;
  width: 12px; height: 12px;
  margin-top: -6px;
  border-radius: 50%;
  background: var(--now);
  animation: pulse 2.4s var(--ease-squircle) infinite;
}

/* MONTH VIEW */
.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: auto repeat(6, 1fr);
  flex: 1;
  min-height: 0;
}
.month-grid[hidden] { display: none; }
.month-day-name {
  padding: var(--sp-2) 0;
  text-align: center;
  font-size: var(--fs-1);
  font-weight: 500;
  color: var(--ink-2);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  border-bottom: 1px solid var(--line-1);
  background: var(--paper-1);
  position: sticky;
  top: 0;
}
.month-cell {
  border-left: 1px solid var(--line-1);
  border-bottom: 1px solid var(--line-1);
  padding: 4px 5px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  min-height: 88px;
  touch-action: manipulation;
}
.month-cell:hover { background: var(--paper-2); }
.month-cell.other { opacity: 0.32; }
.month-cell.today { background: var(--blue-tint); }
.month-cell.today:hover { background: rgba(0, 70, 255, 0.12); }
.month-cell:nth-child(7n + 1) { border-left: none; }
.month-day-num {
  font-size: var(--fs-2);
  font-weight: 500;
  color: var(--ink-1);
  margin-bottom: 3px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  border-radius: var(--r-1);
  padding: 0 4px;
}
.month-cell.today .month-day-num { background: var(--blue-0); color: #fff; font-weight: 600; }
.month-event {
  font-size: var(--fs-1);
  font-weight: 500;
  padding: 2px 6px;
  border-radius: 3px;
  color: #fff;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.month-event:hover { filter: brightness(1.12); transform: translateX(1px); }
.month-more { font-size: 10px; color: var(--ink-2); padding: 1px 6px; font-weight: 500; }

/* FAB */
.fab {
  display: none;
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  right: 20px;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--blue-0);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 70, 255, 0.4), 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 50;
  align-items: center;
  justify-content: center;
  transition: transform var(--dur-fast) var(--ease-spring);
}
.fab:active { transform: scale(0.92); }
.fab:hover { background: var(--blue-1); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 16, 0.45);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-content {
  background: var(--paper-1);
  padding: var(--sp-5);
  border-radius: var(--r-3);
  width: 420px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 64px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  border: 1px solid var(--line-1);
  box-shadow: 0 24px 60px rgba(20, 18, 16, 0.25);
}
.modal-content h2 { font-size: var(--fs-5); font-weight: 600; margin-bottom: var(--sp-2); }
.modal-content label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: var(--fs-2);
  font-weight: 500;
  color: var(--ink-1);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.modal-content input {
  padding: 9px 11px;
  border-radius: var(--r-1);
  border: 1px solid var(--line-1);
  background: var(--paper-0);
  color: var(--ink-0);
  font-family: inherit;
  font-size: var(--fs-4);
}
.modal-content input:focus {
  outline: none;
  border-color: var(--blue-0);
  box-shadow: 0 0 0 3px var(--blue-tint);
}
.modal-content input[type="color"] { padding: 3px; height: 36px; cursor: pointer; }

/* Password Wrapper & Eye Icon */
.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}
.password-wrapper input {
  width: 100%;
  padding-right: 44px;
}
.password-wrapper .btn-icon {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--ink-2);
  display: flex;
  align-items: center;
  justify-content: center;
}
.password-wrapper .btn-icon:hover {
  color: var(--ink-0);
  background: var(--paper-2);
  border-radius: var(--r-1);
}
.password-wrapper .btn-icon svg { width: 18px; height: 18px; }

.auth-hint { font-size: var(--fs-3); color: var(--ink-2); line-height: var(--lh-snug); margin-bottom: var(--sp-2); }
.auth-error { font-size: var(--fs-3); color: var(--now); font-weight: 500; }
#authModal .modal-content { max-width: 360px; }

.modal-actions {
  display: flex;
  gap: var(--sp-2);
  align-items: center;
  margin-top: var(--sp-3);
}
.modal-spacer { flex: 1; }

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.28); opacity: 0.65; }
}
@keyframes eventPop {
  0% { transform: scale(0.85); opacity: 0; }
  60% { transform: scale(1.02); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* MOBILE */
@media (max-width: 720px) {
  .topbar { padding: 8px 10px; padding-top: calc(8px + env(safe-area-inset-top, 0px)); gap: 6px; }
  
  .topbar-row-1 {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    min-height: auto;
    gap: 8px;
  }
  
  .topbar-left { gap: 6px; }

  .title {
    width: 100%;
    text-align: center;
    font-size: 15px;
    order: 3;
    white-space: normal;
  }

  .time-weather {
    padding: 4px 8px;
    font-size: 12px;
  }
  .tw-time svg, .tw-weather svg { width: 12px; height: 12px; }
  .tw-sep { display: none; }
  
  .btn-icon { width: 40px; height: 40px; }
  .btn { padding: 9px 12px; font-size: 13px; min-height: 40px; }
  .view-switch { flex: 1; min-width: 0; }
  .view-btn { flex: 1; padding: 8px 6px; font-size: 12px; min-height: 36px; }
  .view-btn[data-view="week"] { display: none; }
  #addBtn { display: none !important; }
  .fab { display: inline-flex; }
  
  .week-header, .week-grid { grid-template-columns: 44px repeat(7, 1fr); }
  .day-header, .day-grid { grid-template-columns: 44px 1fr; }
  .hour-label { font-size: 10px; padding-right: 4px; }
  .week-day-num { font-size: var(--fs-5); }
  
  .month-cell { min-height: 64px; padding: 2px 3px; }
  .month-day-num { font-size: 11px; min-width: 18px; height: 18px; margin-bottom: 1px; }
  .month-event { font-size: 10px; padding: 1px 4px; }
  .month-more { font-size: 9px; padding: 0 4px; }
  .month-day-name { font-size: 10px; padding: 6px 0; }

  .modal { align-items: flex-end; }
  .modal-content {
    width: 100%;
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    padding: 8px 20px calc(20px + env(safe-area-inset-bottom, 0px));
    border-bottom: none;
  }
  .modal-content h2 { font-size: 16px; text-align: center; }
  .modal-content input { font-size: 16px; padding: 12px 14px; min-height: 44px; }
  
  .modal-actions { flex-wrap: wrap; gap: 8px; margin-top: 8px; }
  .modal-actions .btn { 
    flex: 1 1 0; 
    min-width: 0; 
    padding: 12px 8px; 
    font-size: 14px; 
    min-height: 46px; 
    display: inline-flex;
    justify-content: center;
    align-items: center;
  }
  .modal-actions .modal-spacer { display: none; }
  .modal-actions #deleteBtn { flex-basis: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .now-line::before { animation: none; }
}