:root {
  --accent: #d62828;
  --primary: #8b5e3c;
  --secondary: #f4eadb;
  --ink: #3a2a1c;
  --tag: #f0e3cd;
  --tag-dark: #d9c6a6;
  --line: rgba(58, 42, 28, 0.18);
  --peg-hole: rgba(0, 0, 0, 0.45);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: #7a5133;
  background-image:
    radial-gradient(circle at 10px 10px, var(--peg-hole) 3px, transparent 3.5px),
    radial-gradient(circle at 10px 10px, var(--peg-hole) 3px, transparent 3.5px),
    linear-gradient(rgba(0,0,0,0.04), rgba(0,0,0,0.04));
  background-size: 40px 40px, 40px 40px, 100% 100%;
  background-position: 0 0, 20px 20px, 0 0;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: calc(var(--safe-top) + 14px) 14px calc(76px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

.screen { display: none; }
.screen.active { display: block; animation: fadein 0.18s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.screen-title {
  font-family: "Comic Sans MS", "Chalkboard SE", cursive;
  color: var(--secondary);
  text-shadow: 0 2px 4px rgba(0,0,0,0.4);
  margin: 4px 0 16px;
  font-size: 30px;
  letter-spacing: 0.5px;
  text-align: center;
}

/* ---------- Dashboard ---------- */
.pegboard-area { position: relative; }

.shelf {
  height: 8px;
  background: linear-gradient(#b08050, #7a4f2c);
  border-radius: 4px;
  box-shadow: 0 6px 8px rgba(0,0,0,0.3);
  margin: 18px 6px 0;
}

.jars {
  display: flex;
  gap: 18px;
  justify-content: center;
  margin: 4px 10px 8px;
}

.jar { width: 42%; max-width: 150px; text-align: center; }

.jar-label {
  font-family: "Comic Sans MS", "Chalkboard SE", cursive;
  color: #fff;
  letter-spacing: 2px;
  font-size: 16px;
  text-shadow: 0 0 4px rgba(255,255,255,0.6), 0 2px 3px rgba(0,0,0,0.4);
  margin-bottom: 2px;
}

.jar-glass {
  position: relative;
  height: 150px;
  border: 3px solid rgba(255,255,255,0.45);
  border-radius: 14px 14px 6px 6px;
  background: linear-gradient(180deg, rgba(255,255,255,0.22), rgba(255,255,255,0.05));
  box-shadow: inset 0 0 12px rgba(255,255,255,0.25), 0 6px 10px rgba(0,0,0,0.35);
  overflow: hidden;
}

.jar-lid {
  position: absolute;
  top: 10px;
  left: -8px;
  right: -8px;
  height: 26px;
  border-radius: 8px;
  background: linear-gradient(180deg, #cfd3d6, #9aa0a6);
  box-shadow: 0 2px 3px rgba(0,0,0,0.4);
  border-top: 2px solid #eef0f2;
}

.jar-lid::after {
  content: "";
  position: absolute;
  top: 12px;
  left: 0; right: 0;
  height: 2px;
  background: repeating-linear-gradient(90deg, #8a8f94 0 4px, transparent 4px 7px);
}

.jar-slip {
  position: absolute;
  left: 50%;
  top: 60%;
  transform: translate(-50%, -50%);
  width: 54px;
  height: 84px;
  background: linear-gradient(#fdf4e0, #eee0c0);
  border-radius: 4px;
  box-shadow: 0 3px 6px rgba(0,0,0,0.3), inset 0 0 2px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Comic Sans MS", "Chalkboard SE", cursive;
  font-size: 40px;
  font-weight: bold;
  color: #333;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.12);
  transform: translate(-50%, -50%) rotate(-4deg);
}
.jar:nth-child(2) .jar-slip { transform: translate(-50%, -50%) rotate(3deg); }

.recent-wrap { margin-top: 16px; }
.recent-heading {
  text-align: center;
  font-family: "Comic Sans MS", "Chalkboard SE", cursive;
  color: var(--secondary);
  letter-spacing: 1px;
  margin-bottom: 10px;
  text-shadow: 0 2px 3px rgba(0,0,0,0.4);
}

/* ---------- Price tags ---------- */
.tag-column {
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
}

.tag {
  position: relative;
  width: 94%;
  max-width: 360px;
  background: var(--tag);
  border: 1px solid var(--tag-dark);
  border-radius: 6px;
  box-shadow: 0 5px 8px rgba(0,0,0,0.35);
  padding: 20px 42px 14px 16px;
  cursor: default;
  touch-action: pan-y;
  user-select: none;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.tag:active { transform: scale(0.985); }

.tag::before {
  content: "";
  position: absolute;
  top: 12px;
  right: 12px;
  width: 8px;
  height: 8px;
  background: rgba(0,0,0,0.35);
  border-radius: 50%;
  box-shadow: inset 0 0 2px rgba(0,0,0,0.3);
}

.tag-loop {
  position: absolute;
  top: -14px;
  left: 18px;
  width: 22px;
  height: 28px;
  border: 4px solid #c62828;
  border-radius: 12px;
  background: transparent;
  border-bottom-width: 0;
}

.tag .tag-topline {
  font-weight: 700;
  font-size: 16px;
  line-height: 1.25;
  padding-right: 6px;
}

.tag .tag-room {
  font-size: 13px;
  color: #7a5a38;
  margin-top: 3px;
}

.tag .tag-qty {
  display: inline-block;
  margin-top: 8px;
  font-size: 12px;
  font-weight: 600;
  background: var(--tag-dark);
  border-radius: 999px;
  padding: 2px 8px;
  color: #5a3c24;
}

.tag .status-sticker {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,0.25);
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.tag .status-sticker.needed    { background: var(--accent); }
.tag .status-sticker.purchased { background: #e6b800; }
.tag .status-sticker.installed { background: #43a047; }

.tag.ghost { opacity: 0.55; }

.tag.mini {
  width: 92%;
  max-width: 320px;
  padding: 13px 34px 10px 12px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
.tag.mini .tag-loop { top: -10px; left: 12px; width: 16px; height: 20px; border-width: 3px; }
.tag.mini .tag-topline { font-size: 14px; }
.tag.mini .tag-room { font-size: 12px; }
.tag.mini .status-sticker { right: 8px; bottom: 8px; width: 11px; height: 11px; }

.tag.empty-hint {
  background: rgba(244, 234, 219, 0.5);
  border-style: dashed;
  text-align: center;
  color: rgba(58, 42, 28, 0.55);
  font-style: italic;
  padding: 16px;
}

/* ---------- Add floating button ---------- */
.add-btn {
  position: fixed;
  right: 20px;
  bottom: calc(86px + var(--safe-bottom));
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: linear-gradient(160deg, #f4f6f8, #c3ccd4 55%, #9aa2ab);
  box-shadow: 0 6px 12px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.7);
  z-index: 5;
}
.add-btn::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 50%;
  width: 34px;
  height: 12px;
  transform: translateX(-50%);
  border-radius: 4px;
  background: rgba(255,255,255,0.55);
  filter: blur(2px);
}
.add-plus {
  font-size: 34px;
  font-weight: bold;
  color: #5a5f66;
  text-shadow: 0 1px 0 rgba(255,255,255,0.6);
}

/* ---------- Search ---------- */
.search-bar { margin-bottom: 14px; }
.search-bar input {
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: 999px;
  background: var(--secondary);
  font-size: 15px;
  color: var(--ink);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
.search-bar input:focus { outline: 2px solid var(--secondary); outline-offset: 1px; }

/* ---------- Rooms ---------- */
.room-list { display: flex; flex-direction: column; gap: 12px; }

.room-group { display: flex; flex-direction: column; gap: 14px; }

.room-sign {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(180deg, #b08050, #8b5e3c);
  color: #fff;
  padding: 10px 14px;
  border-radius: 4px;
  border-bottom: 3px solid #6b4527;
  box-shadow: 0 4px 6px rgba(0,0,0,0.3);
  cursor: pointer;
  font-weight: 700;
  letter-spacing: 0.5px;
  user-select: none;
}
.room-sign .caret { transition: transform 0.15s ease; font-size: 14px; }
.room-group.collapsed .caret { transform: rotate(-90deg); }
.room-group.collapsed .room-materials { display: none; }
.room-materials { display: flex; flex-direction: column; gap: 14px; align-items: center; }

/* ---------- Add form ---------- */
.add-form {
  background: var(--secondary);
  border-radius: 10px;
  padding: 18px;
  box-shadow: 0 6px 12px rgba(0,0,0,0.3);
}
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
  color: #6b4527;
}
.field input[type="text"],
.field input[type="number"] {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--tag-dark);
  border-radius: 6px;
  background: #fffdf7;
  font-size: 15px;
  color: var(--ink);
}
.field input:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
.field-error {
  color: var(--accent);
  font-size: 12px;
  min-height: 14px;
  display: block;
  margin-top: 4px;
}
.field.invalid input { border-color: var(--accent); }

.status-picker { display: flex; flex-direction: column; gap: 8px; }
.status-opt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--tag-dark);
  border-radius: 6px;
  background: #fffdf7;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.status-opt input { display: none; }
.status-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,0.2);
}
.status-opt.needed .status-dot { background: var(--accent); }
.status-opt.purchased .status-dot { background: #e6b800; }
.status-opt.installed .status-dot { background: #43a047; }
.status-opt.selected { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 1px var(--primary); }

.submit-btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background: linear-gradient(180deg, #8b5e3c, #6b4527);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
.submit-btn:active { transform: translateY(1px); }

/* ---------- Empty state ---------- */
.empty-state { display: none; text-align: center; padding: 30px 0; color: var(--secondary); }
.empty-state.show { display: block; }
.pegboard-markers {
  width: 70px;
  height: 70px;
  margin: 0 auto 10px;
  background:
    radial-gradient(circle at 12px 12px, var(--peg-hole) 3px, transparent 3.5px),
    radial-gradient(circle at 37px 12px, var(--peg-hole) 3px, transparent 3.5px);
  background-size: 50px 26px;
  opacity: 0.6;
}
.empty-state p { font-style: italic; opacity: 0.9; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(64px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  align-items: stretch;
  background: linear-gradient(180deg, #4a555d, #38414a);
  box-shadow: 0 -3px 8px rgba(0,0,0,0.45);
  z-index: 20;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: #b8c0c6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  font-size: 11px;
  padding: 0;
}
.tab-ico {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  background: linear-gradient(160deg, #6a7076, #4a5056);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.2), 0 1px 0 rgba(0,0,0,0.5);
  color: #dfe3e6;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.tab.active .tab-ico {
  background: linear-gradient(160deg, #f4f6f8, #c9d1d8 55%, #aab2ba);
  color: #333a40;
  transform: translateY(-8px);
  box-shadow: 0 6px 12px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.8);
}
.tab.active { color: #fff; }
.tab-label { letter-spacing: 0.4px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  width: 100%;
  max-width: 340px;
  background: var(--tag);
  border: 1px solid var(--tag-dark);
  border-radius: 8px;
  padding: 34px 18px 16px;
  box-shadow: 0 10px 24px rgba(0,0,0,0.5);
  position: relative;
}
.modal-tag {
  position: absolute;
  top: -18px;
  left: 50%;
  transform: translateX(-50%);
}
.tag-hole {
  width: 22px; height: 22px;
  border-radius: 50%;
  background: rgba(0,0,0,0.3);
}
.tag-loop {
  position: absolute;
  top: -24px;
  left: 50%;
  transform: translateX(-50%);
  width: 22px; height: 30px;
  border: 4px solid #c62828;
  border-bottom-width: 0;
  border-radius: 12px;
}
.modal h2 { margin: 0 0 6px; font-size: 18px; text-align: center; }
.modal-name { margin: 6px 0 2px; font-weight: 700; text-align: center; }
.modal-meta { margin: 0 0 16px; text-align: center; color: #7a5a38; font-size: 13px; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; }
.modal-btn {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--tag-dark);
  border-radius: 6px;
  background: #fffdf7;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
}
.modal-btn.edit { background: var(--secondary); }
.modal-btn.danger { background: var(--accent); border-color: var(--accent); color: #fff; }
.modal-btn:active { transform: translateY(1px); }

@media (min-width: 520px) {
  .content { max-width: 520px; margin: 0 auto; width: 100%; }
}