:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e0e0e0;
  --text: #1d1d1f;
  --text-muted: #6e6e73;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #dc2626;
  --tag-bg: #ebebf0;
  --tag-active-bg: #4f46e5;
  --tag-active-text: #ffffff;
  --shadow-sm: 0 1px 4px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.1);
  --radius: 10px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ---- Header ---- */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 200;
  box-shadow: var(--shadow-sm);
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.02em;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-name { font-size: 0.875rem; color: var(--text-muted); }

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
}

/* ---- Buttons ---- */
.btn-primary, .btn-secondary {
  padding: 7px 16px;
  border-radius: 7px;
  border: none;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: background 0.15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-secondary { background: var(--tag-bg); color: var(--text); }
.btn-secondary:hover { background: var(--border); }

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 3px 5px;
  border-radius: 5px;
  font-size: 0.95rem;
  line-height: 1;
  opacity: 0.55;
  transition: opacity 0.15s, background 0.15s;
}
.btn-icon:hover { opacity: 1; background: var(--tag-bg); }

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: inherit;
  padding: 0;
  text-decoration: underline;
}

/* ---- Page layout ---- */
.page-layout {
  display: flex;
  min-height: calc(100vh - 56px);
}

/* ---- Sidebar ---- */
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 56px;
  height: calc(100vh - 56px);
  overflow-y: auto;
  z-index: 100;
}

.sidebar-title {
  padding: 16px 16px 8px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
}

.tag-nav { display: flex; flex-direction: column; padding-bottom: 12px; }

.tag-nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  margin: 1px 8px;
  border-radius: 7px;
  text-decoration: none;
  color: var(--text);
  font-size: 0.875rem;
  transition: background 0.15s, color 0.15s;
  gap: 6px;
}
.tag-nav-item:hover { background: var(--bg); }
.tag-nav-item.active { background: var(--primary); color: #fff; }
.tag-nav-item.active .tag-nav-count {
  background: rgba(255,255,255,0.22);
  color: #fff;
}

.tag-nav-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tag-nav-count {
  font-size: 0.7rem;
  background: var(--tag-bg);
  color: var(--text-muted);
  border-radius: 100px;
  padding: 1px 7px;
  flex-shrink: 0;
}

.sidebar-empty {
  padding: 8px 16px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 56px 0 0 0;
  background: rgba(0,0,0,0.35);
  z-index: 90;
}

/* ---- Header left slot ---- */
.header-left { display: flex; align-items: center; gap: 10px; }

.sidebar-toggle-btn {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 5px 8px;
  color: var(--text);
  border-radius: 6px;
  line-height: 1;
  align-items: center;
}
.sidebar-toggle-btn:hover { background: var(--tag-bg); }

/* ---- Active filter bar ---- */
.active-filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 0.875rem;
  color: var(--text-muted);
}
.clear-filter {
  color: var(--danger);
  text-decoration: none;
  font-size: 0.8rem;
  padding: 2px 8px;
  border-radius: 100px;
  background: #fee2e2;
  transition: background 0.15s;
}
.clear-filter:hover { background: #fca5a5; }

/* ---- Layout ---- */
main {
  flex: 1;
  min-width: 0;
  padding: 24px 20px;
}

/* ---- Upload ---- */
.upload-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 36px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  color: var(--text-muted);
}
.drop-zone svg { display: block; margin: 0 auto 12px; opacity: 0.4; }
.drop-zone p { font-size: 0.9rem; }
.drop-zone .drop-hint { margin-top: 6px; font-size: 0.85rem; }
.drop-zone kbd { font-family: inherit; font-size: 0.8rem; padding: 1px 5px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface); }
.drop-zone:hover, .drop-zone.drag-over {
  border-color: var(--primary);
  background: #f0f0ff;
}

.preview-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.preview-item {
  position: relative;
}
.preview-item img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 7px;
  border: 1px solid var(--border);
  display: block;
}
.remove-preview {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 0.65rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.upload-form {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  align-items: center;
}
.upload-form input[type="text"] {
  flex: 1;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.15s;
}
.upload-form input[type="text"]:focus { border-color: var(--primary); }

.remove-bg-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  cursor: pointer;
}

/* ---- Tag chips ---- */
.tag-chip {
  display: inline-block;
  padding: 3px 10px;
  background: var(--tag-bg);
  border-radius: 100px;
  font-size: 0.78rem;
  color: var(--text);
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  cursor: pointer;
}
.tag-chip:hover { background: #d8d8e0; }
.tag-chip.active { background: var(--tag-active-bg); color: var(--tag-active-text); }
.tag-chip.small { padding: 2px 7px; font-size: 0.72rem; display: inline-flex; align-items: center; gap: 2px; }

.tag-remove-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 0 1px;
  color: inherit;
  opacity: 0.55;
  transition: opacity 0.15s;
  flex-shrink: 0;
}
.tag-remove-btn:hover { opacity: 1; }

/* ---- Gallery grid ---- */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
}

.image-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: transform 0.2s, box-shadow 0.2s;
}
.image-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}
.image-wrapper {
  aspect-ratio: 1;
  overflow: hidden;
  cursor: pointer;
  background: #e8e8e8;
  position: relative;
}
.image-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
  display: block;
}
.image-wrapper:hover img { transform: scale(1.06); }

.card-footer {
  padding: 7px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-height: 36px;
}
.tag-list { display: flex; flex-wrap: wrap; gap: 3px; flex: 1; min-width: 0; }
.card-actions { display: flex; gap: 2px; flex-shrink: 0; }

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 64px 20px;
  color: var(--text-muted);
}

/* ---- Lightbox ---- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.93);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox.hidden { display: none; }

.lightbox-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 90vw;
  max-height: 90vh;
}
#lightbox-img {
  max-width: 90vw;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 6px;
  display: block;
}
.lightbox-filename {
  color: rgba(255,255,255,0.65);
  font-size: 0.82rem;
}

.lightbox-close {
  position: fixed;
  top: 18px;
  right: 20px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.lightbox-close:hover { background: rgba(255,255,255,0.3); }

.lightbox-prev, .lightbox-next {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 46px;
  height: 46px;
  font-size: 1.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.lightbox-prev:hover, .lightbox-next:hover { background: rgba(255,255,255,0.28); }
.lightbox-prev { left: 16px; }
.lightbox-next { right: 16px; }

/* ---- Modal ---- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal.hidden { display: none; }

.modal-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 440px;
  max-width: 92vw;
  box-shadow: var(--shadow-md);
}
.modal-box h3 { margin-bottom: 14px; font-size: 1rem; }
.modal-box input[type="text"] {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 0.875rem;
  outline: none;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s;
}
.modal-box input[type="text"]:focus { border-color: var(--primary); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ---- Login ---- */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 48px 40px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  max-width: 380px;
  width: 90%;
}
.login-card h1 { font-size: 1.6rem; color: var(--primary); margin-bottom: 6px; }
.login-card > p { color: var(--text-muted); margin-bottom: 32px; }

.google-btn {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 11px 22px;
  text-decoration: none;
  color: var(--text);
  font-weight: 500;
  font-size: 0.95rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.google-btn:hover { border-color: var(--primary); box-shadow: 0 2px 10px rgba(79,70,229,0.2); }

.error-msg { color: var(--danger); font-size: 0.85rem; margin-top: 16px; }

/* ---- Selection bar ---- */
.selection-bar {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #1d1d1f;
  color: #fff;
  padding: 10px 18px;
  border-radius: 100px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 300;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
  font-size: 0.875rem;
  white-space: nowrap;
}

#selection-count { font-weight: 600; margin-right: 4px; }

.sel-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  padding: 5px 14px;
  border-radius: 100px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 500;
  transition: background 0.15s;
}
.sel-btn:hover { background: rgba(255,255,255,0.28); }
.sel-btn-danger { background: rgba(220,38,38,0.7); }
.sel-btn-danger:hover { background: rgba(220,38,38,0.95); }

/* ---- Card selection ---- */
.select-overlay {
  position: absolute;
  top: 7px;
  left: 7px;
  z-index: 10;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}

.select-overlay input[type="checkbox"] { display: none; }

.select-check {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(255,255,255,0.85);
  border: 2px solid rgba(255,255,255,0.9);
  color: transparent;
  font-size: 0.75rem;
  font-weight: 700;
  transition: background 0.15s, color 0.15s;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.image-card:hover .select-overlay,
.image-card.selected .select-overlay { opacity: 1; }

.image-card.selected .select-check {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.image-card.selected {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}
.image-card.selected:hover { transform: none; }

/* ---- Tag color button ---- */
.tag-color-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.7rem;
  opacity: 0;
  padding: 0 2px;
  line-height: 1;
  transition: opacity 0.15s;
  flex-shrink: 0;
}
.tag-nav-item:hover .tag-color-btn { opacity: 0.5; }
.tag-nav-item:hover .tag-color-btn:hover { opacity: 1; }

/* ---- Utilities ---- */
.hidden { display: none !important; }

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .sidebar-toggle-btn { display: flex; }

  .sidebar {
    position: fixed;
    top: 56px;
    left: 0;
    height: calc(100vh - 56px);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: var(--shadow-md);
    z-index: 100;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.open { display: block; }
}

@media (max-width: 600px) {
  main { padding: 14px 12px; }
  header { padding: 0 14px; }
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .login-card { padding: 36px 24px; }
}

/* ---- Dark mode ---- */
[data-theme="dark"] {
  --bg: #18181c;
  --surface: #24242b;
  --border: #38383f;
  --text: #e8e8f0;
  --text-muted: #9090a0;
  --tag-bg: #2e2e38;
  --shadow-sm: 0 1px 4px rgba(0,0,0,0.35);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.5);
}
[data-theme="dark"] .drop-zone:hover,
[data-theme="dark"] .drop-zone.drag-over { background: #1e1e2e; }
[data-theme="dark"] .tag-chip:hover { background: #3a3a48; }
[data-theme="dark"] .login-card { background: var(--surface); }
[data-theme="dark"] .google-btn { background: var(--surface); color: var(--text); }

/* ---- Theme toggle button ---- */
.theme-toggle-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.1rem;
  padding: 4px 6px;
  border-radius: 6px;
  line-height: 1;
  transition: background 0.15s;
}
.theme-toggle-btn:hover { background: var(--tag-bg); }

/* ---- Gallery toolbar ---- */
.gallery-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 12px;
}
.size-selector { display: flex; gap: 4px; }
.size-btn {
  background: var(--tag-bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.size-btn:hover { background: var(--border); color: var(--text); }
.size-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---- Gallery grid sizes ---- */
.gallery-grid.size-sm { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.gallery-grid.size-md { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.gallery-grid.size-lg { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.gallery-grid.size-xl { grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 18px; }

/* ---- Tag autocomplete ---- */
.tag-input-wrap { position: relative; margin-bottom: 16px; }
.tag-input-wrap input[type="text"] { margin-bottom: 0; width: 100%; }
.tag-suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 7px 7px;
  box-shadow: var(--shadow-md);
  z-index: 20;
  max-height: 160px;
  overflow-y: auto;
}
.tag-suggestion {
  display: block;
  width: 100%;
  text-align: left;
  padding: 7px 12px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--text);
  transition: background 0.1s;
}
.tag-suggestion:hover { background: var(--bg); }

/* ---- Tag picker ---- */
.tag-picker-section { margin-bottom: 12px; }
.tag-picker-label { font-size: 0.72rem; color: var(--text-muted); font-weight: 500; margin-bottom: 6px; }
.tag-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  max-height: 130px;
  overflow-y: auto;
  margin-bottom: 2px;
}
.tag-pick-chip {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  background: var(--tag-bg);
  border: 1.5px solid var(--border);
  border-radius: 100px;
  font-size: 0.78rem;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
}
.tag-pick-chip:hover { background: var(--border); }
.tag-pick-chip.selected { background: var(--primary); border-color: var(--primary); color: #fff; }

/* Upload tag area */
.upload-tag-area { flex: 1; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.upload-tag-picker { display: flex; flex-wrap: wrap; gap: 4px; }
