:root {
  --tile: 150px;
  --bg: #f3f1ec;
  --panel: #ffffff;
  --text: #222;
  --muted: #666;
  --border: #d8d4cc;
  --primary: #2f5d8a;
  --primary-text: #fff;
  --danger: #b3261e;
  --selected: #e8a33d;
  --slot: #2f5d8a;
  --chip-on: #2f5d8a;
  --year: #8a5a2f;
  font-size: 18px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button { font: inherit; }
[hidden] { display: none !important; }

.btn {
  min-height: 48px;
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { filter: brightness(0.92); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-small { min-height: 44px; padding: 0 12px; }

.hint { color: var(--muted); font-size: 0.85rem; }

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.toolbar-left { display: flex; align-items: center; gap: 16px; }
.title { margin: 0; font-size: 1.3rem; }
.toolbar-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

.tabs { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.tab {
  min-height: 44px;
  padding: 0 20px;
  border: none;
  border-radius: 9px;
  background: none;
  cursor: pointer;
  color: var(--muted);
}
.tab.active { background: var(--panel); color: var(--text); font-weight: bold; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); }

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  padding-bottom: 150px; /* room for the action bar */
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 30px 10px;
  line-height: 1.5;
}

/* Tag chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips-scroll { max-height: 45vh; overflow-y: auto; padding: 2px; }
.chip {
  min-height: 44px;
  padding: 0 16px;
  border: 2px solid var(--border);
  border-radius: 22px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.chip .n { color: var(--muted); margin-left: 6px; font-size: 0.85rem; }
.chip.year { border-color: #d9bfa5; }
.chip.on { background: var(--chip-on); border-color: var(--chip-on); color: #fff; }
.chip.on .n { color: #dfe8f1; }
.chip.year.on { background: var(--year); border-color: var(--year); }
.chip.some { border-color: var(--chip-on); border-style: dashed; }
.chip.special { font-style: italic; }
.chip.create { border-style: dashed; color: var(--primary); }

/* Filter bar */
.filter-bar {
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.filter-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.filter-summary { font-weight: bold; }
.filter-actions { display: flex; gap: 8px; }
.filter-bar .chips { max-height: 112px; overflow-y: auto; }

/* Photo grid */
.grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: stretch;
}

.tile {
  position: relative;
  width: var(--tile);
  height: var(--tile);
  border-radius: 8px;
  overflow: hidden;
  background: #ccc;
  border: 3px solid transparent;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}
.tile.selected { border-color: var(--selected); }
.tile.selected img { opacity: 0.75; }
.tile .badge {
  position: absolute;
  top: 4px;
  left: 4px;
  min-width: 32px;
  height: 32px;
  padding: 0 6px;
  border-radius: 16px;
  background: var(--selected);
  color: #000;
  font-weight: bold;
  display: none;
  align-items: center;
  justify-content: center;
}
.tile.selected .badge { display: flex; }
.tile .zoom {
  position: absolute;
  right: 4px;
  top: 4px;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
}
.tile .caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 6px 4px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75));
  color: #fff;
  font-size: 0.75rem;
  line-height: 1.25;
  max-height: 3.6em;
  overflow: hidden;
}
.tile .caption:empty { display: none; }

/* Insertion slots shown while moving photos */
.slot {
  width: 40px;
  height: var(--tile);
  border: 2px dashed var(--slot);
  border-radius: 8px;
  background: rgba(47, 93, 138, 0.08);
  color: var(--slot);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0;
}
.slot:active { background: rgba(47, 93, 138, 0.25); }

.year-heading {
  width: 100%;
  margin: 10px 0 2px;
  font-size: 1.05rem;
  color: var(--year);
}
.year-heading:first-child { margin-top: 0; }

/* Tagger */
.tagger { display: grid; grid-template-columns: 3fr 2fr; }
@media (orientation: portrait) {
  .tagger { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .tagger-panel { max-height: 55vh; }
}
.tagger-photo {
  position: relative;
  min-height: 0;
  background: #1d1d1d;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tagger-photo img { max-width: 100%; max-height: 100%; object-fit: contain; cursor: zoom-in; }
.tagger-done {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  color: #fff;
  text-align: center;
  font-size: 1.2rem;
}
.tagger-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--border);
}
.tagger-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
#tagger-progress { font-weight: bold; }
.segmented { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg { min-height: 44px; padding: 0 14px; border: none; background: var(--panel); cursor: pointer; }
.seg.active { background: var(--primary); color: #fff; }
.tagger-current { min-height: 1.5em; color: var(--muted); }
.tagger-current strong { color: var(--text); }
.btn-copy { white-space: normal; text-align: left; padding: 8px 16px; }
.tag-entry { display: flex; gap: 8px; }
.tag-entry input {
  flex: 1;
  min-width: 0;
  min-height: 52px;
  font: inherit;
  padding: 0 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
}
.tag-entry input:focus { border-color: var(--primary); outline: none; }
.tagger-nav { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; margin-top: auto; }
.tagger-nav .btn { min-height: 64px; font-size: 1.15rem; }
.tagger-remove { align-self: flex-start; }

/* Bottom action bar */
.action-bar {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px 14px;
  background: var(--panel);
  border: 2px solid var(--selected);
  border-radius: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
  z-index: 10;
}
.action-info { display: flex; flex-direction: column; }
.action-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

/* Full-screen viewer */
.viewer {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
}
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-btn {
  position: absolute;
  width: 64px;
  height: 64px;
  border: none;
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
}
.viewer-btn:disabled { opacity: 0.3; }
.viewer-close { top: 16px; right: 16px; }
.viewer-prev { left: 16px; top: 50%; transform: translateY(-50%); }
.viewer-next { right: 16px; top: 50%; transform: translateY(-50%); }
.viewer-footer {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: #fff;
  text-shadow: 0 1px 3px #000;
}

/* Dialogs */
.dialog {
  border: none;
  border-radius: 14px;
  padding: 20px;
  width: min(560px, 92vw);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog h3 { margin: 0 0 10px; }
.dialog .tag-entry { margin: 12px 0; }
.dialog .chips { margin-bottom: 16px; }
#dialog-message { line-height: 1.5; margin-bottom: 14px; }
#dialog-message:empty { display: none; }
#dialog-input {
  width: 100%;
  min-height: 52px;
  font: inherit;
  padding: 0 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
  margin-bottom: 14px;
}
.dialog-buttons { display: flex; flex-direction: column; gap: 8px; }
.dialog-buttons.inline { flex-direction: row; justify-content: flex-end; flex-wrap: wrap; }
.dialog-list { max-height: 50vh; overflow-y: auto; }

.tag-rows { display: flex; flex-direction: column; gap: 6px; max-height: 55vh; overflow-y: auto; }
.tag-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid #eee; }
.tag-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-row .n { color: var(--muted); font-size: 0.85rem; margin-left: 6px; }

/* Progress */
.progress-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
}
.progress-box {
  background: var(--panel);
  border-radius: 14px;
  padding: 24px;
  width: min(420px, 90vw);
}
.progress-track { height: 14px; background: #e5e1d8; border-radius: 7px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--primary); transition: width 0.15s; }

/* Notice bar: save problems, storage protection, backup reminders */
.notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 14px;
  background: #fff6dc;
  border-bottom: 1px solid #e8d49a;
  line-height: 1.4;
}
.notice.error { background: #fde8e6; border-bottom-color: #e7a7a1; color: #7d1a14; }
.notice-buttons { display: flex; gap: 8px; }
