:root {
  --ink: #302a22;
  --muted: #817669;
  --sand-50: #fdfaf5;
  --sand-100: #f8f2e7;
  --sand-200: #efe3d1;
  --sand-300: #dfcaa9;
  --gold: #b9925d;
  --gold-dark: #8e6b3e;
  --card: rgba(255, 255, 255, 0.9);
  --danger: #b54c43;
  --success: #4d765d;
  --border: rgba(92, 72, 45, 0.16);
  --shadow: 0 18px 55px rgba(83, 63, 36, 0.12);
  --radius: 18px;
  font-family: "Microsoft YaHei UI", "PingFang SC", "Noto Sans CJK SC", system-ui, sans-serif;
  color: var(--ink);
  background: var(--sand-50);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--sand-50); }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 15% 8%, rgba(223, 202, 169, 0.2), transparent 32%),
    linear-gradient(180deg, #fffdf9 0%, var(--sand-50) 100%);
}

button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.48; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid rgba(185, 146, 93, 0.4);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.boot-loading {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--sand-100);
  z-index: 9999;
}

.boot-loading[hidden] { display: none; }

.boot-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--sand-300);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: boot-spin 0.7s linear infinite;
}

@keyframes boot-spin {
  to { transform: rotate(360deg); }
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px 20px;
  background:
    linear-gradient(90deg, rgba(255,255,255,.42) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,.42) 1px, transparent 1px),
    var(--sand-100);
  background-size: 40px 40px;
}

.login-card {
  width: min(100%, 410px);
  padding: 38px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.72);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.brand-kicker {
  color: var(--gold-dark);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .22em;
  text-transform: uppercase;
}

.login-card h1, .topbar h1 { margin: 8px 0 0; letter-spacing: -.03em; }
.login-card h1 { font-size: 28px; }
.login-subtitle { margin: 8px 0 28px; color: var(--muted); font-size: 14px; }

.field-label, label > span {
  display: block;
  margin: 0 0 8px;
  color: #645a4f;
  font-size: 13px;
  font-weight: 700;
}

input, textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255,255,255,.86);
  color: var(--ink);
  transition: border-color .18s, box-shadow .18s, background .18s;
}

input, select { min-height: 48px; padding: 0 14px; }
textarea { resize: vertical; min-height: 56px; padding: 10px 14px; line-height: 1.55; }
input[type="date"] { text-align: center; -webkit-appearance: none; appearance: none; }
input[type="date"]::-webkit-date-and-time-value { text-align: center; }
input::placeholder, textarea::placeholder { color: #a69c91; }
input:focus, textarea:focus, select:focus {
  border-color: var(--gold);
  outline: none;
  background: #fff;
  box-shadow: 0 0 0 4px rgba(185, 146, 93, .12);
}

.login-card > input, .password-field { margin-bottom: 18px; }
.password-field { position: relative; }
.password-field input { padding-right: 68px; }
.password-toggle {
  position: absolute;
  right: 8px;
  top: 7px;
  height: 34px;
  border: 0;
  background: transparent;
  color: var(--gold-dark);
  font-size: 13px;
}

.remember-row {
  display: flex;
  align-items: center;
  gap: 9px;
  width: fit-content;
  margin: 2px 0 8px;
  color: var(--muted);
}
.remember-row input { width: 18px; min-height: 18px; accent-color: var(--gold); }
.remember-row span { margin: 0; font-weight: 500; }
.form-error { min-height: 22px; margin: 7px 0; color: var(--danger); font-size: 13px; }

.primary-button, .secondary-button {
  min-height: 46px;
  border-radius: 12px;
  padding: 0 20px;
  font-weight: 800;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.primary-button {
  border: 1px solid var(--gold);
  background: linear-gradient(135deg, #c4a16c, var(--gold));
  color: #fff;
  box-shadow: 0 8px 22px rgba(142, 107, 62, .2);
}
.primary-button:hover:not(:disabled) { background: var(--gold-dark); border-color: var(--gold-dark); }
.secondary-button { border: 1px solid var(--border); background: #fff; color: var(--ink); }
.secondary-button:hover:not(:disabled) { border-color: var(--gold); color: var(--gold-dark); }
.login-button { width: 100%; margin-top: 8px; }

.app-shell { min-height: 100vh; }
.topbar {
  position: sticky;
  z-index: 30;
  top: 0;
  min-height: calc(76px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: calc(14px + env(safe-area-inset-top)) max(22px, calc((100vw - 1180px) / 2)) 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(253,250,245,.88);
  backdrop-filter: blur(16px);
}
.topbar h1 { font-size: 20px; }
.user-area { position: relative; }
.user-button {
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px 6px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
}
.avatar {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--sand-200);
  color: var(--gold-dark);
  font-weight: 900;
}
.user-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: 150px;
  padding: 7px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow);
}
.user-menu button { width: 100%; padding: 10px 12px; border: 0; border-radius: 8px; background: transparent; text-align: left; color: var(--ink); }
.user-menu button:hover { background: var(--sand-100); }

.tabbar {
  position: sticky;
  z-index: 20;
  top: calc(76px + env(safe-area-inset-top));
  display: flex;
  gap: 4px;
  padding: 8px max(22px, calc((100vw - 1180px) / 2));
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(14px);
}
.tab-button {
  min-height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-weight: 750;
}
.tab-button.active { background: var(--sand-200); color: var(--gold-dark); }

.main-content { width: min(1180px, calc(100% - 44px)); margin: 0 auto; padding: 36px 0 80px; }
.page { animation: page-in .2s ease-out; }
@keyframes page-in { from { opacity: 0; transform: translateY(5px); } }
.section-heading { display: flex; justify-content: space-between; align-items: end; gap: 30px; margin-bottom: 22px; }
.section-heading h2 { margin: 3px 0 0; font-size: clamp(24px, 3vw, 34px); letter-spacing: -.04em; }
.section-heading > p { max-width: 420px; margin: 0; color: var(--muted); line-height: 1.6; font-size: 14px; text-align: right; }
.eyebrow { margin: 0; color: var(--gold-dark); font-size: 12px; font-weight: 800; letter-spacing: .16em; }

.archive-form, .search-panel, .admin-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 10px 35px rgba(83, 63, 36, .07);
}
.archive-form { padding: 24px; }

.form-grid { display: grid; grid-template-columns: 1.4fr .8fr; gap: 18px; margin-bottom: 18px; }
.archive-form > label { display: block; margin-bottom: 20px; }
label b { color: var(--danger); }

.dropzone {
  min-height: 210px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 28px;
  border: 1.5px dashed #d1b98f;
  border-radius: 16px;
  background: var(--sand-50);
  text-align: center;
  transition: border .18s, background .18s, transform .18s;
}
.dropzone.dragging { border-color: var(--gold-dark); background: var(--sand-200); transform: scale(.997); }
.dropzone.has-files {
  align-items: stretch;
  justify-content: flex-start;
  min-height: auto;
  max-height: 55vh;
  overflow-y: auto;
  gap: 0;
  text-align: left;
}
.dropzone-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
}
.dropzone.has-files .dropzone-empty { display: none; }
.dropzone-empty > span { color: var(--muted); font-size: 13px; }
.upload-mark {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: var(--sand-200);
  color: var(--gold-dark);
  font-size: 28px;
  line-height: 1;
}
.dropzone-files { width: 100%; }
.dropzone-files .selection-header { display: flex; justify-content: space-between; align-items: center; margin: 0 2px 12px; }
.selection-actions { display: flex; gap: 12px; align-items: center; }
.dropzone-files .preview-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(125px, 1fr)); gap: 12px; }

.text-button { border: 0; background: transparent; color: var(--gold-dark); padding: 7px; font-weight: 700; }
.danger-text { color: var(--danger); }
.preview-item {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--sand-100);
}
.preview-item img, .preview-item video { width: 100%; height: 100%; display: block; object-fit: cover; }
.preview-item video { background: #000; }
.preview-item .file-fallback { height: 100%; display: grid; place-items: center; padding: 12px; color: var(--muted); text-align: center; }
.video-badge {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.video-badge::after {
  content: "▶";
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(48, 42, 34, 0.78);
  color: #fff;
  font-size: 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
}
.preview-item button {
  position: absolute;
  right: 7px;
  top: 7px;
  min-width: 34px;
  min-height: 34px;
  border: 0;
  border-radius: 50%;
  background: rgba(48,42,34,.82);
  color: #fff;
}
.preview-item span {
  position: absolute;
  left: 8px;
  bottom: 8px;
  max-width: calc(100% - 16px);
  overflow: hidden;
  padding: 4px 7px;
  border-radius: 7px;
  background: rgba(255,255,255,.86);
  color: var(--ink);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-actions { display: flex; justify-content: space-between; align-items: center; gap: 20px; margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border); }
.upload-actions p { margin: 0; color: var(--muted); font-size: 13px; }

.search-panel { padding: 20px; }
.search-box { display: block; margin-bottom: 14px; }
.search-box input { font-size: 16px; }
.filter-grid { display: grid; grid-template-columns: repeat(3, minmax(120px, 1fr)) auto; gap: 12px; align-items: end; }
.filter-grid label span { margin-bottom: 5px; }
.search-button { min-width: 110px; }
.result-summary { margin: 20px 2px 10px; color: var(--muted); font-size: 13px; }
.record-list { display: grid; gap: 12px; margin-top: 18px; }
.record-card { border: 1px solid var(--border); border-radius: 16px; background: #fff; overflow: hidden; }
.record-header { width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: center; padding: 18px; border: 0; background: transparent; color: var(--ink); text-align: left; }
.record-header:hover { background: var(--sand-50); }
.record-title { display: flex; align-items: center; flex-wrap: wrap; gap: 9px; }
.record-title strong { font-size: 17px; }
.record-date { color: var(--gold-dark); font-weight: 800; }
.record-meta { margin-top: 6px; color: var(--muted); font-size: 12px; }
.record-note { margin: 9px 0 0; color: #61584e; font-size: 13px; }
.record-actions { display: flex; gap: 8px; align-items: center; }
.record-actions button { min-height: 38px; border: 1px solid var(--border); border-radius: 10px; background: #fff; padding: 0 13px; color: var(--ink); }
.record-gallery { padding: 0 18px 18px; border-top: 1px solid var(--border); }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; padding-top: 16px; }
.gallery-item { position: relative; aspect-ratio: 1; overflow: hidden; border: 0; border-radius: 12px; background: var(--sand-100); padding: 0; }
.gallery-item img, .gallery-item video { width: 100%; height: 100%; object-fit: cover; }
.gallery-video { height: 100%; display: grid; place-items: center; color: var(--gold-dark); font-weight: 800; }
.empty-state { min-height: 280px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); text-align: center; }
.empty-mark { font-size: 44px; color: var(--sand-300); }
.load-more { display: flex; margin: 20px auto; }

.admin-grid { display: grid; grid-template-columns: minmax(280px, .75fr) minmax(420px, 1.25fr); gap: 18px; }
.admin-card { padding: 20px; }
.admin-card h3 { margin: 0 0 18px; }
.stack-form { display: grid; gap: 14px; }
.field-help { margin: -4px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.card-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.users-list, .audit-list { display: grid; gap: 9px; }
.user-row, .audit-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px; padding: 12px; border-radius: 12px; background: var(--sand-50); }
.user-row small, .audit-row small { display: block; margin-top: 3px; color: var(--muted); }
.role-badge { display: inline-block; margin-left: 6px; padding: 2px 7px; border-radius: 999px; background: var(--sand-200); color: var(--gold-dark); font-size: 11px; }
.audit-card { margin-top: 18px; }
.recycle-list { margin-top: 14px; }
.audit-row { grid-template-columns: 150px 1fr auto; font-size: 13px; }
.audit-row time { color: var(--muted); white-space: nowrap; }

dialog { border: 0; padding: 0; }
dialog::backdrop { background: rgba(27,23,19,.76); backdrop-filter: blur(5px); }
.media-dialog { width: min(96vw, 1120px); height: min(92vh, 850px); border-radius: 18px; background: #171512; color: #fff; overflow: hidden; }
.dialog-header { height: 62px; display: flex; justify-content: space-between; align-items: center; padding: 0 18px; background: rgba(0,0,0,.28); }
.dialog-header span { margin-left: 10px; color: #cfc7bb; font-size: 12px; }
.dialog-header button, .viewer-controls button { min-height: 40px; border: 1px solid rgba(255,255,255,.2); border-radius: 10px; background: rgba(255,255,255,.1); color: #fff; padding: 0 14px; }
.viewer-stage { height: calc(100% - 124px); display: grid; place-items: center; overflow: hidden; }
.viewer-stage img, .viewer-stage video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-stage img { -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto; }
.video-viewer { width: 100%; height: 100%; min-width: 0; min-height: 0; display: grid; place-items: center; position: relative; overflow: hidden; }
.video-viewer video { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.video-status { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 24px; text-align: center; }
.video-status small, .video-fallback { color: #cfc7bb; }
.video-fallback { position: absolute; left: 14px; right: 14px; bottom: 8px; text-align: center; }
.viewer-controls { height: 62px; display: flex; justify-content: center; align-items: center; gap: 10px; padding: 8px; flex-wrap: wrap; }
.viewer-controls .secondary-button { min-height: 40px; background: rgba(255,255,255,.94); }
.viewer-controls .save-button { min-height: 40px; border: 0; border-radius: 10px; background: #4CAF50; color: #fff; padding: 0 14px; font-size: 14px; }
.viewer-controls .save-button:active { background: #3d8b40; }
.viewer-hint { padding: 6px 14px; text-align: center; color: #cfc7bb; font-size: 12px; background: rgba(0,0,0,.22); }
.password-dialog { width: min(92vw, 440px); border-radius: 18px; background: #fff; }
.dialog-card { display: grid; gap: 16px; padding: 24px; }
.dialog-card h2 { margin: 0; }

.progress-overlay { position: fixed; z-index: 100; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(48,42,34,.58); backdrop-filter: blur(6px); }
.progress-card { width: min(100%, 380px); display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 30px; border-radius: 20px; background: #fff; box-shadow: var(--shadow); text-align: center; }
.progress-card span { color: var(--muted); font-size: 13px; max-width: 100%; word-break: break-all; }
.spinner { width: 38px; height: 38px; border: 4px solid var(--sand-200); border-top-color: var(--gold); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress-track { width: 100%; height: 9px; overflow: hidden; border-radius: 999px; background: var(--sand-200); }
.progress-track div { width: 0; height: 100%; background: var(--gold); transition: width .15s; }
.progress-actions { display: flex; gap: 10px; }
.toast { position: fixed; z-index: 120; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: min(90vw, 520px); padding: 12px 18px; border-radius: 12px; background: #302a22; color: #fff; box-shadow: var(--shadow); text-align: center; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

@media (max-width: 760px) {
  .login-page { place-items: stretch; padding: 0; background: var(--sand-100); }
  .login-card { align-self: center; width: 100%; padding: 28px 20px; border: 0; border-radius: 0; background: transparent; box-shadow: none; }
  .topbar { min-height: calc(68px + env(safe-area-inset-top)); padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left)); }
  .topbar .brand-kicker { font-size: 10px; }
  .topbar h1 { font-size: 18px; }
  .user-button { max-width: 150px; }
  .user-button #currentUsername { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar { top: calc(68px + env(safe-area-inset-top)); padding: 7px max(10px, env(safe-area-inset-right)) 7px max(10px, env(safe-area-inset-left)); overflow-x: auto; }
  .tab-button { flex: 1 0 auto; padding: 0 14px; }
  .main-content { width: 100%; padding: 24px 14px 104px; }
  .section-heading { display: block; margin-bottom: 18px; }
  .section-heading h2 { font-size: 25px; }
  .section-heading > p { margin-top: 8px; text-align: left; }
  .archive-form, .search-panel, .admin-card { border-radius: 15px; }
  .archive-form { padding: 16px; }
  .form-grid, .filter-grid, .admin-grid { grid-template-columns: 1fr; }
  .dropzone { min-height: 185px; padding: 22px 14px; }
  .dropzone-files .preview-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
  .dropzone-files .selection-header { margin: 0 1px 10px; }
  .upload-actions { position: fixed; z-index: 18; left: 0; right: 0; bottom: 0; min-height: 76px; margin: 0; padding: 10px 14px max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: rgba(255,255,255,.96); backdrop-filter: blur(14px); }
  .upload-actions p { display: none; }
  .upload-actions .primary-button { width: 100%; }
  .filter-grid { grid-template-columns: repeat(3, 1fr); }
  .filter-grid .search-button { grid-column: 1 / -1; width: 100%; }
  .filter-grid select { padding: 0 8px; }
  .record-header { grid-template-columns: 1fr; padding: 15px; }
  .record-actions { justify-content: flex-end; }
  .record-gallery { padding: 0 12px 12px; }
  .gallery-grid { grid-template-columns: repeat(3, 1fr); gap: 7px; }
  .audit-row { grid-template-columns: 1fr; }
  .audit-row time { white-space: normal; }
  .media-dialog { width: 100vw; height: 100dvh; max-width: none; max-height: none; border-radius: 0; }
  .dialog-header { padding-top: env(safe-area-inset-top); height: calc(62px + env(safe-area-inset-top)); }
  .viewer-stage { height: calc(100% - 124px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .viewer-controls { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .progress-actions { width: 100%; }
  .progress-actions button { flex: 1; }
}

@media (max-width: 420px) {
  .filter-grid { grid-template-columns: 1fr 1fr; }
  .filter-grid label:first-child { grid-column: 1 / -1; }
  .dropzone-files .preview-grid, .gallery-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .record-actions { justify-content: stretch; }
  .record-actions button { flex: 1; }
}

@media (max-height: 480px) and (orientation: landscape) {
  .media-dialog { width: 100vw; height: 100dvh; max-width: none; max-height: none; border-radius: 0; }
  .dialog-header { height: calc(48px + env(safe-area-inset-top)); }
  .viewer-stage { height: calc(100% - 100px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .viewer-controls { height: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
