:root {
  --paper: #f6efe1;
  --paper-card: #fffcf5;
  --ink: #4a3c2f;
  --ink-dim: #948572;
  --line: #e3d3b4;
  --terracotta: #d97757;
  --terracotta-dark: #b85e42;
  --sage: #8ba888;
  --sage-dark: #6f8f6c;
  --mustard: #e8b84b;
  --mustard-dark: #c99527;
  --blush: #eeb0ac;
  --blush-dark: #c97d7d;
  --radius: 10px;
  font-family: 'Nunito', -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }

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

body {
  background-color: var(--paper);
  background-image: radial-gradient(circle at 1px 1px, rgba(74, 60, 47, 0.08) 1px, transparent 0);
  background-size: 20px 20px;
  color: var(--ink);
}

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

h1, h2, h3 {
  font-family: 'Fredoka', sans-serif;
}

/* Buttons: stamped/sticker style with an offset hard shadow */
.btn {
  border: 2px solid var(--ink);
  background: var(--paper-card);
  color: var(--ink);
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 2px 2px 0 var(--ink);
  transition: transform 0.1s, box-shadow 0.1s;
}
.btn:hover { transform: translate(-1px, -1px); box-shadow: 3px 3px 0 var(--ink); }
.btn:active { transform: translate(1px, 1px); box-shadow: 0 0 0 var(--ink); }

.btn-primary {
  background: var(--terracotta);
  border-color: var(--terracotta-dark);
  color: #fff;
  box-shadow: 2px 2px 0 var(--terracotta-dark);
}
.btn-primary:hover { box-shadow: 3px 3px 0 var(--terracotta-dark); }

.btn-ghost { background: var(--paper-card); }
.btn-small { padding: 5px 10px; font-size: 12px; box-shadow: 1.5px 1.5px 0 var(--ink); }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

/* Polaroid photo accent (login page + topbar brand) */
.polaroid {
  background: var(--paper-card);
  border: 1px solid var(--line);
  padding: 6px 6px 0;
  box-shadow: 0 4px 10px rgba(74, 60, 47, 0.18);
  border-radius: 3px;
  transform: rotate(-4deg);
  flex-shrink: 0;
}
.polaroid img { display: block; width: 100%; height: 100%; object-fit: cover; border-radius: 1px; }

.login-photo {
  position: relative;
  width: 96px;
  height: 96px;
  padding: 6px 6px 10px;
  align-self: center;
  margin-bottom: 6px;
  margin-top: 6px;
}
.login-photo img { aspect-ratio: 1 / 1; }
.login-photo::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%) rotate(-3deg);
  width: 46px;
  height: 18px;
  background: repeating-linear-gradient(45deg, rgba(232, 184, 75, 0.9), rgba(232, 184, 75, 0.9) 6px, rgba(255, 255, 255, 0.4) 6px, rgba(255, 255, 255, 0.4) 12px);
  border-radius: 2px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.brand-photo {
  width: 40px;
  height: 40px;
  padding: 3px 3px 5px;
  transform: rotate(-6deg);
}
.brand-photo img { aspect-ratio: 1 / 1; }

.brand-block { display: flex; flex-direction: column; justify-content: center; }
.brand-sub { font-size: 11px; color: var(--ink-dim); font-style: italic; }

.who-picker { display: flex; gap: 8px; justify-content: center; margin: 4px 0; }
.who-option {
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 6px 16px;
  cursor: pointer;
  font-family: 'Fredoka', sans-serif;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-dim);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.who-option input { display: none; }
.who-option:has(input:checked) {
  background: var(--terracotta);
  border-color: var(--terracotta-dark);
  color: #fff;
}

/* Settings page */
.settings-main { padding: 24px; max-width: 560px; margin: 0 auto; }
.settings-title { font-size: 24px; margin: 0 0 16px; }
.settings-card { text-align: left; }
.settings-form { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--ink-dim); font-weight: 700; }
.settings-form input {
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
  outline: none;
  color: var(--ink);
  font-weight: 400;
}
.settings-form input:focus { border-color: var(--terracotta); }
.settings-form .btn { align-self: flex-start; margin-top: 4px; }
.settings-success {
  background: var(--sage);
  border: 2px solid var(--sage-dark);
  color: #1f3320;
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 14px;
}

/* Login */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}

.login-card {
  position: relative;
  background: var(--paper-card);
  border: 2px solid var(--ink);
  border-radius: 14px;
  box-shadow: 6px 6px 0 var(--terracotta);
  padding: 44px 32px 32px;
  width: 100%;
  max-width: 340px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-card h1 { margin: 0; font-size: 26px; font-weight: 700; color: var(--ink); }
.login-card h1 em { color: var(--terracotta); font-style: normal; }
.login-sub { color: var(--ink-dim); font-size: 13px; margin: 0 0 8px; font-style: italic; }
.login-error { color: var(--blush-dark); font-size: 13px; margin: 0; font-weight: 700; }

.login-card input {
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 15px;
  outline: none;
}
.login-card input:focus { border-color: var(--terracotta); }
.login-card .btn { align-self: center; padding: 8px 28px; }

/* Layout */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 16px 24px;
  background: var(--paper-card);
  border-bottom: 2px dashed var(--line);
  flex-wrap: wrap;
}

.brand {
  font-family: 'Fredoka', sans-serif;
  font-size: 25px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: var(--ink);
}
.brand em { color: var(--terracotta); font-style: normal; }

.tabs {
  display: flex;
  gap: 6px;
  align-items: flex-end;
}

.tab-btn {
  border: 2px solid var(--line);
  border-bottom: none;
  background: var(--paper);
  color: var(--ink-dim);
  padding: 7px 18px 6px;
  border-radius: 10px 10px 0 0;
  cursor: pointer;
  font-family: 'Fredoka', sans-serif;
  font-size: 14.5px;
  font-weight: 700;
  transform: translateY(2px);
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.tab-btn.active {
  background: var(--terracotta);
  border-color: var(--terracotta-dark);
  color: #fff;
  transform: translateY(0);
}

.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

.logout-form { display: inline; }

main { padding: 24px; max-width: 1400px; margin: 0 auto; }

.view { }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 30px 24px;
  padding: 12px 6px 6px;
}

.empty-state {
  color: var(--ink-dim);
  text-align: center;
  padding: 60px 20px;
  font-family: 'Fredoka', sans-serif;
  font-size: 16px;
}

/* Tiles: polaroid photos pinned to the page */
.tile {
  position: relative;
  background: var(--paper-card);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 10px 10px 4px;
  box-shadow: 0 6px 14px rgba(74, 60, 47, 0.16), 0 2px 4px rgba(74, 60, 47, 0.08);
  transition: transform 0.2s, box-shadow 0.2s;
}
.grid .tile:nth-child(5n+1) { transform: rotate(-2deg); }
.grid .tile:nth-child(5n+2) { transform: rotate(1.5deg); }
.grid .tile:nth-child(5n+3) { transform: rotate(-1deg); }
.grid .tile:nth-child(5n+4) { transform: rotate(2deg); }
.grid .tile:nth-child(5n+5) { transform: rotate(-1.5deg); }
.tile:hover {
  transform: rotate(0deg) translateY(-4px) scale(1.03);
  box-shadow: 0 14px 26px rgba(74, 60, 47, 0.22);
  z-index: 2;
}

.tile::before {
  content: '';
  position: absolute;
  top: -11px;
  left: 50%;
  transform: translateX(-50%) rotate(-4deg);
  width: 56px;
  height: 22px;
  background: repeating-linear-gradient(45deg, rgba(139, 168, 136, 0.85), rgba(139, 168, 136, 0.85) 6px, rgba(255, 255, 255, 0.35) 6px, rgba(255, 255, 255, 0.35) 12px);
  border-radius: 2px;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.12);
}
.grid .tile:nth-child(3n+2)::before {
  background: repeating-linear-gradient(45deg, rgba(232, 184, 75, 0.85), rgba(232, 184, 75, 0.85) 6px, rgba(255, 255, 255, 0.35) 6px, rgba(255, 255, 255, 0.35) 12px);
}
.grid .tile:nth-child(3n)::before {
  background: repeating-linear-gradient(45deg, rgba(216, 119, 87, 0.85), rgba(216, 119, 87, 0.85) 6px, rgba(255, 255, 255, 0.35) 6px, rgba(255, 255, 255, 0.35) 12px);
}

.tile-media {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  background: #e9ddc7;
  overflow: hidden;
  border-radius: 2px;
}
.tile-media img { width: 100%; height: 100%; object-fit: cover; display: block; }

.tile-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.15s;
}
.tile:hover .tile-actions,
.tile:focus-within .tile-actions {
  opacity: 1;
}
.tile-actions button {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1.5px solid var(--ink);
  background: rgba(255, 252, 245, 0.9);
  color: var(--ink);
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tile-actions button:hover { background: var(--terracotta); border-color: var(--terracotta-dark); color: #fff; }

.tile-caption {
  padding: 10px 4px 2px;
  text-align: center;
}
.tile-caption h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--ink);
}
.tile-caption h3 .year {
  font-family: 'Nunito', sans-serif;
  color: var(--ink-dim);
  font-weight: 600;
  font-size: 12px;
}
.tile-meta {
  color: var(--ink-dim);
  font-size: 11px;
  font-style: italic;
  margin-top: 1px;
}

.tile-footer {
  padding: 10px 6px 12px;
  margin-top: 4px;
  border-top: 2px dashed var(--line);
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.rater-row { display: flex; align-items: center; gap: 8px; }
.rater-name { color: var(--ink-dim); font-size: 11px; width: 50px; flex-shrink: 0; font-weight: 700; }

.rating-row { display: flex; gap: 2px; }
.star { color: #ddcda9; cursor: pointer; font-size: 17px; transition: color 0.1s; }
.star.filled { color: var(--terracotta); }
.star:hover { color: var(--terracotta); }

.asleep-row { display: flex; flex-direction: column; gap: 6px; }

/* Single pill: toggle + inline minute counter live inside the same button */
.asleep-chip {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 6px;
  background: var(--paper);
  border: 1.5px dashed var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  color: var(--ink-dim);
  width: fit-content;
  max-width: 100%;
  row-gap: 2px;
}
.asleep-chip.asleep {
  background: var(--blush);
  border: 1.5px solid var(--blush-dark);
  color: #6b3535;
  font-weight: 700;
}
.asleep-toggle { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.asleep-toggle input { accent-color: var(--blush-dark); margin: 0; }

.asleep-min-wrap {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  padding-left: 6px;
  border-left: 1.5px dashed rgba(107, 53, 53, 0.35);
}
.asleep-min-inline {
  width: 22px;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  text-align: right;
  padding: 0;
  outline: none;
  -moz-appearance: textfield;
}
.asleep-min-inline::-webkit-outer-spin-button,
.asleep-min-inline::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.notes-input {
  background: repeating-linear-gradient(var(--paper-card) 0px, var(--paper-card) 20px, var(--line) 21px);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  font-family: 'Nunito', sans-serif;
  font-size: 13px;
  color: var(--ink);
  resize: vertical;
  min-height: 42px;
  outline: none;
  line-height: 21px;
}
.notes-input:focus { border-color: var(--terracotta); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(74, 60, 47, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 16px;
  z-index: 20;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  position: relative;
  background: var(--paper-card);
  border: 2px solid var(--ink);
  border-radius: 14px;
  box-shadow: 6px 6px 0 var(--terracotta);
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 20px;
}

.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; border-bottom: 2px dashed var(--line); padding-bottom: 10px; }
.modal-head h2 { margin: 0; font-size: 20px; color: var(--ink); }

#search-input {
  width: 100%;
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  outline: none;
  margin-bottom: 10px;
}
#search-input:focus { border-color: var(--terracotta); }

.search-results { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; max-height: 260px; overflow-y: auto; }
.search-result.is-busy { opacity: 0.5; pointer-events: none; }

.search-result {
  display: flex;
  gap: 10px;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
  align-items: center;
}
.search-result:hover { background: var(--paper); }
.search-result img { width: 40px; height: 60px; object-fit: cover; border-radius: 4px; flex-shrink: 0; }
.sr-title { font-size: 13.5px; font-weight: 700; }
.sr-title .year { color: var(--ink-dim); font-weight: 400; }
.sr-overview {
  color: var(--ink-dim);
  font-size: 11.5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.manual-form { display: flex; flex-direction: column; gap: 8px; border-top: 2px dashed var(--line); padding-top: 14px; }
.manual-hint { color: var(--ink-dim); font-size: 12px; margin: 0 0 4px; font-style: italic; }
.manual-form input, .manual-form textarea {
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13.5px;
  outline: none;
}
.manual-form input:focus, .manual-form textarea:focus { border-color: var(--terracotta); }
.manual-form textarea { min-height: 60px; resize: vertical; }

/* Stats */
#stats-content { padding: 6px; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--paper-card);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 18px 14px;
  text-align: center;
  box-shadow: 3px 3px 0 var(--line);
}
.stat-num { font-family: 'Fredoka', sans-serif; font-size: 32px; line-height: 1; color: var(--terracotta); font-weight: 700; }
.stat-num .unit { font-size: 16px; font-family: 'Nunito', sans-serif; margin-left: 2px; color: var(--ink-dim); }
.stat-label { font-size: 12.5px; color: var(--ink-dim); margin-top: 6px; }
.stat-label small { display: block; font-style: italic; }

.stat-compare {
  background: var(--paper-card);
  border: 2px dashed var(--line);
  border-radius: 14px;
  padding: 16px 20px;
  margin-bottom: 16px;
}
.stat-compare h3 { font-size: 17px; margin: 0 0 12px; color: var(--ink); }
.compare-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.compare-side { text-align: center; flex: 1; }
.compare-name { font-size: 12px; color: var(--ink-dim); font-weight: 700; }
.compare-value { font-family: 'Fredoka', sans-serif; font-size: 23px; color: var(--ink); line-height: 1.1; }
.compare-value small { font-size: 12.5px; color: var(--ink-dim); font-family: 'Nunito', sans-serif; display: block; }
.compare-vs { font-family: 'Fredoka', sans-serif; color: var(--ink-dim); font-size: 13px; }
.compare-winner .compare-value { color: var(--terracotta); }

.stat-highlight {
  text-align: center;
  font-size: 15px;
  padding: 14px;
  background: var(--mustard);
  border: 2px solid var(--mustard-dark);
  border-radius: 12px;
  color: #4a3410;
  margin-bottom: 12px;
}
.stat-highlight strong { font-family: 'Fredoka', sans-serif; font-size: 15px; }

/* Touch devices can't hover, so keep tile actions reachable */
@media (hover: none) {
  .tile-actions { opacity: 1; }
}

@media (max-width: 480px) {
  .topbar { padding: 12px 16px; gap: 12px; }
  main { padding: 16px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(135px, 1fr)); gap: 26px 14px; }
}
