:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #17201b;
  --muted: #6b766f;
  --border: #e3e7e4;
  --primary: #16a34a;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --ok-bg: #dcfce7;
  --ok-text: #166534;
  --todo-bg: #fef3c7;
  --todo-text: #92400e;
  --radius: 16px;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.06), 0 6px 20px rgb(0 0 0 / 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1411;
    --surface: #18201b;
    --text: #e8eee9;
    --muted: #94a19a;
    --border: #2a342e;
    --primary: #22c55e;
    --primary-text: #052e16;
    --danger: #f87171;
    --ok-bg: #14532d;
    --ok-text: #bbf7d0;
    --todo-bg: #451a03;
    --todo-text: #fde68a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; text-decoration: none; }
h1 { font-size: 1.25rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 4px; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; padding: 48px 16px; }
.error { color: var(--danger); }
.grow { flex: 1; min-width: 0; }
.row { display: flex; align-items: center; gap: 12px; }

#topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px 16px calc(100px + env(safe-area-inset-bottom));
}

.avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--border); }
.avatar.big { width: 52px; height: 52px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.big { padding: 14px 24px; font-size: 1rem; }
.btn.full { width: 100%; }
.btn.primary.big { background: var(--surface); color: var(--text); border-color: var(--border); box-shadow: var(--shadow); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 1.3rem; line-height: 1; cursor: pointer;
}

/* Login */
.login { display: grid; justify-items: center; gap: 12px; text-align: center; padding-top: 12vh; }
.login h1 { font-size: 1.8rem; }
.login img { border-radius: 22px; }

/* Home */
.albums { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.album-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}
.cover { aspect-ratio: 4 / 3; background: var(--border); display: grid; place-items: center; color: var(--muted); }
.cover img { width: 100%; height: 100%; object-fit: cover; }
.album-meta { display: flex; align-items: center; gap: 12px; padding: 12px 14px; }
.album-meta strong, .album-meta small { display: block; }
.badge { font-size: 0.78rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok-text); }
.badge.todo { background: var(--todo-bg); color: var(--todo-text); }

/* Album */
.album-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.photo {
  margin: 0; cursor: pointer; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.photo img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; background: var(--border); }
.photo figcaption { padding: 8px 10px; display: grid; }
.photo figcaption strong { font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.photo time { font-size: 0.78rem; color: var(--muted); }
.empty { text-align: center; padding: 64px 16px; }

.fab {
  position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 20;
  width: 60px; height: 60px; border-radius: 50%; border: 0;
  background: var(--primary); color: var(--primary-text);
  font-size: 2rem; line-height: 1; cursor: pointer;
  box-shadow: 0 6px 20px rgb(22 163 74 / 0.4);
}

/* Dialogs */
dialog { border: 0; padding: 0; color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / 0.55); }
.sheet { width: min(440px, calc(100vw - 32px)); border-radius: var(--radius); background: var(--surface); }
.sheet form { display: grid; gap: 14px; padding: 20px; }
.picker {
  display: grid; place-items: center; min-height: 180px; cursor: pointer; overflow: hidden;
  border: 2px dashed var(--border); border-radius: 12px; color: var(--muted);
}
.picker.readonly { cursor: default; border-style: solid; }
.picker img { max-height: 320px; width: 100%; object-fit: contain; }
.field { display: grid; gap: 6px; font-size: 0.9rem; font-weight: 600; }
.field input {
  font: inherit; font-weight: 400; padding: 10px 12px; width: 100%;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text);
}
.actions { display: flex; justify-content: flex-end; gap: 8px; }

.viewer {
  width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0;
  background: #000; color: #fff;
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}
.viewer[open] { display: flex; }
.viewer img { max-height: 72dvh; object-fit: contain; border-radius: 8px; }
.viewer .close { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; }
.viewer-meta, .rewind-meta { display: grid; justify-items: center; text-align: center; }
.viewer-meta time, .rewind-meta time { opacity: 0.75; font-size: 0.9rem; }

/* Rewind */
.rewind {
  position: fixed; inset: 0; z-index: 50; background: #000; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: calc(20px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
}
.rewind .progress {
  position: absolute; top: calc(10px + env(safe-area-inset-top)); left: 16px; right: 16px;
  height: 4px; border-radius: 2px; background: rgb(255 255 255 / 0.25); overflow: hidden;
}
#rewind-bar { height: 100%; width: 0; background: #fff; transition: width 0.3s linear; }
.rewind img { max-height: 72dvh; object-fit: contain; border-radius: 8px; }
.rewind-meta strong { font-size: 1.4rem; }
.rewind-controls { display: flex; gap: 12px; }
.rewind .icon-btn, .viewer .icon-btn { background: rgb(255 255 255 / 0.12); border-color: transparent; color: #fff; }

/* Settings */
.settings { display: grid; gap: 14px; max-width: 520px; margin: 0 auto; }
.settings .album-head { margin: 0; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; display: grid; gap: 12px;
}
.card p { margin: 0; }
.switch input { width: 22px; height: 22px; accent-color: var(--primary); }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 100; max-width: calc(100vw - 32px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-weight: 600;
}
