:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-soft: #f0f0ed;
  --text: #121212;
  --muted: #6f716d;
  --line: #deded9;
  --accent: #171717;
  --accent-contrast: #ffffff;
  --danger: #b42318;
  --warning-bg: #fff4d6;
  --warning-text: #694f00;
  --error-bg: #fff0ee;
  --shadow: 0 10px 30px rgba(0,0,0,.08);
  --radius: 18px;
  --radius-sm: 12px;
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
}
button, input, textarea { font: inherit; }
button { cursor: pointer; }
img, video { max-width: 100%; display: block; }
.hidden { display: none !important; }

.eyebrow {
  font-size: 11px;
  letter-spacing: .22em;
  font-weight: 700;
  color: var(--muted);
}

.site-header,
.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: calc(18px + env(safe-area-inset-top)) 18px 18px;
  background: rgba(245,245,243,.94);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid rgba(0,0,0,.06);
}
.site-header h1, .admin-header h1 { margin: 4px 0 0; font-size: 28px; line-height: 1; }
.site-header p { margin: 7px 0 0; color: var(--muted); font-size: 13px; }

.public-main,
.admin-main { width: min(1100px, 100%); margin: 0 auto; padding: 22px 16px 110px; }
.section-heading { display: flex; align-items: end; justify-content: space-between; margin-bottom: 14px; }
.section-heading h2 { margin: 0; font-size: 22px; }
.section-heading p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }

.ghost-btn,
.primary-btn,
.danger-btn {
  border: 0;
  border-radius: 12px;
  min-height: 42px;
  padding: 0 15px;
  font-weight: 700;
}
.ghost-btn { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.primary-btn { background: var(--accent); color: var(--accent-contrast); }
.danger-btn { background: #fff; color: var(--danger); border: 1px solid #f1c5c0; }
.wide { width: 100%; }
.link-btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

.technician-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.technician-card {
  border: 0;
  padding: 0;
  text-align: left;
  background: var(--surface);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(0,0,0,.05);
}
.card-media {
  aspect-ratio: 3 / 4;
  background: #e6e6e2;
  overflow: hidden;
  position: relative;
}
.card-media img { width: 100%; height: 100%; object-fit: cover; }
.media-placeholder {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  font-size: 34px; font-weight: 800; color: #9a9b97;
  background: linear-gradient(145deg, #ecece8, #dadad5);
}
.card-code {
  position: absolute;
  top: 9px; left: 9px;
  background: rgba(0,0,0,.68);
  color: #fff;
  border-radius: 999px;
  padding: 5px 8px;
  font-size: 11px;
  font-weight: 700;
  backdrop-filter: blur(10px);
}
.card-body { padding: 11px 12px 13px; }
.card-body h3 { margin: 0 0 6px; font-size: 17px; }
.card-meta { color: var(--muted); font-size: 12px; line-height: 1.55; }
.card-intro {
  margin-top: 7px;
  color: #4f504c;
  font-size: 12px;
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.site-footer {
  position: fixed;
  z-index: 15;
  bottom: 0;
  left: 0; right: 0;
  padding: 12px 16px calc(10px + env(safe-area-inset-bottom));
  background: rgba(245,245,243,.94);
  backdrop-filter: blur(18px);
  border-top: 1px solid rgba(0,0,0,.06);
}
.site-footer p { margin: 7px 0 0; text-align: center; color: var(--muted); font-size: 11px; }

.notice { border-radius: 12px; padding: 12px 14px; margin-bottom: 14px; line-height: 1.5; font-size: 13px; }
.notice-warning { background: var(--warning-bg); color: var(--warning-text); }
.notice-error { background: var(--error-bg); color: var(--danger); }
.empty-state { text-align: center; padding: 80px 20px; color: var(--muted); }
.empty-state h3 { color: var(--text); margin-bottom: 6px; }
.empty-icon { font-size: 42px; }

.detail-dialog,
.edit-dialog {
  border: 0;
  padding: 0;
  border-radius: 22px;
  width: min(680px, calc(100vw - 24px));
  max-height: calc(100vh - 40px);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.detail-dialog::backdrop,
.edit-dialog::backdrop { background: rgba(0,0,0,.56); backdrop-filter: blur(4px); }
.dialog-close {
  position: absolute;
  right: 12px;
  top: 12px;
  z-index: 2;
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 0;
  background: rgba(255,255,255,.88);
  font-size: 25px;
}
.detail-hero { aspect-ratio: 4 / 5; background: #e6e6e2; overflow: hidden; }
.detail-hero img { width: 100%; height: 100%; object-fit: cover; }
.detail-body { padding: 20px; }
.detail-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.detail-title-row h2 { margin: 0; font-size: 27px; }
.detail-code { color: var(--muted); font-weight: 700; }
.detail-meta { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.meta-pill { border: 1px solid var(--line); border-radius: 999px; padding: 6px 9px; font-size: 12px; }
.detail-intro { line-height: 1.75; color: #444541; white-space: pre-wrap; }
.detail-gallery { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 16px; }
.detail-gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 12px; }
.detail-video { width: 100%; border-radius: 12px; margin-top: 16px; background: #000; }
.detail-contact { margin-top: 18px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 100;
  background: #111;
  color: #fff;
  padding: 10px 14px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
}

/* Admin */
.admin-page { min-height: 100vh; }
.admin-setup { margin: 12px; }
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(430px, 100%); padding: 26px; background: var(--surface); border-radius: 22px; box-shadow: var(--shadow); }
.login-card h1 { margin: 7px 0 5px; }
.login-card p { color: var(--muted); margin: 0 0 22px; }
.field-label { display: block; font-size: 12px; font-weight: 700; color: #51524e; margin: 14px 0 7px; }
.input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  min-height: 44px;
  padding: 10px 12px;
  color: var(--text);
  outline: none;
}
.input:focus { border-color: #91918b; box-shadow: 0 0 0 3px rgba(0,0,0,.05); }
.textarea { resize: vertical; min-height: 90px; }
.login-card .primary-btn { margin-top: 18px; }
.form-error { margin-top: 10px; color: var(--danger); font-size: 13px; }

.admin-header-actions { display: flex; gap: 8px; }
.admin-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.segmented { display: flex; gap: 4px; background: #e9e9e5; padding: 4px; border-radius: 12px; }
.segment { border: 0; background: transparent; min-height: 38px; padding: 0 11px; border-radius: 9px; font-weight: 700; color: #666762; white-space: nowrap; }
.segment.active { background: #fff; color: #111; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.segment span { display: inline-block; margin-left: 4px; color: #8a8b86; }
.library-tools { display: grid; grid-template-columns: 1fr auto; gap: 9px; margin-bottom: 14px; }
.loading-state { padding: 28px; text-align: center; color: var(--muted); }
.admin-list { display: grid; gap: 10px; }
.admin-item {
  display: grid;
  grid-template-columns: 68px minmax(0,1fr);
  gap: 12px;
  padding: 10px;
  border-radius: 15px;
  background: var(--surface);
  border: 1px solid rgba(0,0,0,.055);
}
.admin-thumb { width: 68px; height: 88px; border-radius: 11px; overflow: hidden; background: #e6e6e2; }
.admin-thumb img { width: 100%; height: 100%; object-fit: cover; }
.admin-thumb .media-placeholder { font-size: 20px; }
.admin-item-main { min-width: 0; }
.admin-item-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.admin-name { font-weight: 800; font-size: 16px; }
.admin-code { font-size: 11px; color: var(--muted); margin-left: 5px; }
.status-badge { padding: 4px 7px; border-radius: 999px; font-size: 10px; font-weight: 800; white-space: nowrap; }
.status-on { background: #e7f6ea; color: #1d6d2b; }
.status-off { background: #eeeeeb; color: #6d6e69; }
.admin-meta { color: var(--muted); font-size: 11px; margin-top: 5px; }
.admin-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.mini-btn {
  min-height: 34px;
  border-radius: 9px;
  padding: 0 10px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 12px;
  font-weight: 700;
}
.mini-btn.primary { background: #171717; color: #fff; border-color: #171717; }
.mini-btn.danger { color: var(--danger); }
.mini-btn:disabled { opacity: .38; cursor: default; }

.edit-dialog form { padding: 22px; max-height: calc(100vh - 40px); overflow: auto; }
.dialog-title-row { display: flex; align-items: flex-start; justify-content: space-between; }
.dialog-title-row h2 { margin: 5px 0 0; }
.static-close { position: static; background: var(--surface-soft); flex: 0 0 auto; }
.form-grid { display: grid; gap: 10px; }
.two-col { grid-template-columns: 1fr 1.6fr; }
.three-col { grid-template-columns: repeat(3, 1fr); }
.check-row { display: flex; gap: 9px; align-items: center; margin-top: 15px; font-size: 13px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 9px; margin-top: 20px; }

@media (min-width: 720px) {
  .technician-grid { grid-template-columns: repeat(4, minmax(0,1fr)); gap: 16px; }
  .public-main { padding-top: 30px; }
  .site-header, .admin-header { padding-left: max(24px, calc((100vw - 1100px)/2)); padding-right: max(24px, calc((100vw - 1100px)/2)); }
  .admin-list { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .site-footer { position: static; width: min(1100px,100%); margin: 0 auto; background: transparent; border: 0; padding-bottom: 30px; }
  .public-main { padding-bottom: 35px; }
  .toast { bottom: 30px; }
}

@media (max-width: 520px) {
  .admin-toolbar { align-items: stretch; flex-direction: column; }
  .admin-toolbar .primary-btn { width: 100%; }
  .segmented { width: 100%; }
  .segment { flex: 1; }
  .two-col, .three-col { grid-template-columns: 1fr; }
  .admin-header h1 { font-size: 24px; }
  .admin-header-actions .link-btn { display: none; }
}

/* R2 media uploader */
.media-editor-block {
  margin-top: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fafaf8;
}
.media-editor-block .field-label { margin-top: 0; }
.media-editor-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.field-help { margin: -2px 0 10px; color: var(--muted); font-size: 11px; line-height: 1.55; }
.file-input-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.media-upload-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.upload-status { margin-top: 9px; color: #246b35; font-size: 12px; line-height: 1.5; }
.upload-status.upload-error { color: var(--danger); }
.media-preview-single,
.media-preview-video {
  min-height: 130px;
  border-radius: 12px;
  overflow: hidden;
  background: #e6e6e2;
}
.media-preview-single { width: min(210px, 100%); aspect-ratio: 3 / 4; }
.media-preview-single img { width: 100%; height: 100%; object-fit: cover; }
.media-preview-video { min-height: 120px; }
.media-preview-video video { width: 100%; max-height: 280px; background: #000; }
.media-preview-empty { display: grid; place-items: center; color: #8b8c87; font-size: 12px; border: 1px dashed #c8c8c2; background: #f0f0ed; }
.media-preview-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.media-preview-empty-grid { grid-column: 1 / -1; min-height: 88px; border-radius: 11px; }
.media-preview-tile { position: relative; aspect-ratio: 1; overflow: hidden; border-radius: 11px; background: #e6e6e2; }
.media-preview-tile img { width: 100%; height: 100%; object-fit: cover; }
.media-remove-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 27px;
  height: 27px;
  border: 0;
  border-radius: 999px;
  background: rgba(0,0,0,.68);
  color: #fff;
  font-size: 18px;
  line-height: 1;
}
@media (max-width: 520px) {
  .media-preview-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .media-upload-actions .ghost-btn { flex: 1 1 auto; }
}
