:root {
  color-scheme: light;
  --bg: #f5f7f4;
  --panel: #ffffff;
  --ink: #1f2a27;
  --muted: #65726d;
  --line: #dce4df;
  --accent: #257469;
  --accent-strong: #174f49;
  --warm: #a6532b;
  --soft: #edf4f1;
  --shadow: 0 14px 34px rgba(27, 45, 40, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body.viewer-open {
  overflow: hidden;
}

body.modal-open {
  overflow: hidden;
}

[hidden] {
  display: none !important;
}

.app-header {
  background: linear-gradient(135deg, #164a45 0%, #276e62 58%, #b76538 100%);
  color: #fff;
}

.header-inner {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 44px 0 34px;
}

.eyebrow {
  margin: 0 0 8px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 14px;
}

h1 {
  margin: 0;
  font-size: 48px;
  font-weight: 800;
  letter-spacing: 0;
  line-height: 1.12;
}

.summary {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 24px;
}

.summary span {
  min-width: 116px;
  padding: 10px 14px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(8px);
}

.summary strong {
  font-size: 21px;
  margin-right: 4px;
}

.page-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 24px auto 48px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1.4fr) minmax(150px, 0.7fr) minmax(150px, 0.7fr) auto auto;
  gap: 12px;
  align-items: end;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.field {
  display: grid;
  gap: 7px;
}

.field span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input,
select,
button {
  appearance: none;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

input,
select {
  width: 100%;
  padding: 0 12px;
  font-size: 16px;
}

input:focus,
select:focus,
button:focus-visible {
  outline: 3px solid rgba(37, 116, 105, 0.22);
  border-color: var(--accent);
}

button {
  padding: 0 18px;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

button:disabled {
  cursor: progress;
  opacity: 0.68;
}

.secondary-button {
  border-color: var(--line);
  background: #fff;
  color: var(--accent-strong);
}

.secondary-button:hover {
  border-color: var(--accent);
  background: var(--soft);
}

.icon-button {
  width: 40px;
  min-height: 40px;
  padding: 0;
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
  font-size: 26px;
  line-height: 1;
}

.icon-button:hover {
  background: var(--soft);
}

.portrait-button {
  width: 86px;
  height: 110px;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  cursor: zoom-in;
  overflow: hidden;
}

.portrait-button:hover {
  background: transparent;
}

.portrait-button:focus-visible {
  outline: 3px solid rgba(37, 116, 105, 0.28);
  outline-offset: 3px;
}

.result-bar {
  display: flex;
  justify-content: space-between;
  margin: 22px 0 14px;
  color: var(--muted);
  font-weight: 700;
}

.stats-section {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin: 0 0 22px;
}

.stats-panel {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 20px rgba(31, 42, 39, 0.06);
}

.stats-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.stats-header h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

.stats-header span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}

.bar-chart {
  display: grid;
  gap: 9px;
  margin-bottom: 14px;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(54px, 92px) minmax(0, 1fr) 30px;
  gap: 8px;
  align-items: center;
  min-height: 24px;
}

.bar-label {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--soft);
}

.bar-fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #b76538);
}

.bar-value {
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  text-align: right;
}

.contacts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.contact-card {
  display: grid;
  grid-template-columns: 86px 1fr;
  gap: 14px;
  min-height: 144px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 20px rgba(31, 42, 39, 0.07);
}

.contact-card.edited-card {
  border-color: rgba(37, 116, 105, 0.36);
}

.portrait {
  width: 86px;
  height: 110px;
  border-radius: 8px;
  background: var(--soft);
  object-fit: cover;
}

.contact-info {
  min-width: 0;
}

.contact-name {
  margin: 2px 0 8px;
  font-size: 20px;
  font-weight: 800;
  color: var(--ink);
}

.meta {
  display: grid;
  gap: 6px;
  margin: 0;
}

.meta-row {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 8px;
  align-items: baseline;
}

.meta dt {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.meta dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
}

.phone-row dd {
  overflow: visible;
}

.phone-link {
  display: inline-block;
  max-width: 100%;
  color: var(--accent-strong);
  text-decoration: none;
  font-weight: 700;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  letter-spacing: 0;
}

.chip {
  display: inline-flex;
  max-width: 100%;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--soft);
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 700;
}

.area-chip {
  background: #fff2ea;
  color: var(--warm);
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.card-actions button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.danger-button {
  border-color: #c85d47;
  background: #fff;
  color: #a43d2b;
}

.danger-button:hover {
  background: #fff1ed;
}

.empty-state {
  margin-top: 42px;
  padding: 42px 16px;
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.68);
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 22px;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
}

.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: max(28px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(28px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
  background: rgba(16, 25, 23, 0.78);
}

.image-viewer[hidden] {
  display: none;
}

.viewer-figure {
  display: grid;
  gap: 12px;
  margin: 0;
  max-width: min(92vw, 720px);
  max-height: min(88vh, 860px);
  justify-items: center;
}

.viewer-figure img {
  display: block;
  max-width: 100%;
  max-height: calc(88vh - 64px);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.34);
  object-fit: contain;
}

.viewer-figure figcaption {
  color: #fff;
  font-size: 17px;
  font-weight: 800;
  text-align: center;
}

.viewer-close {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  min-height: 40px;
  padding: 0 16px;
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.14);
}

.viewer-close:hover {
  background: rgba(255, 255, 255, 0.24);
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 18;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: rgba(16, 25, 23, 0.62);
}

.dialog-backdrop[hidden] {
  display: none;
}

.dialog-panel {
  width: min(520px, 100%);
  max-height: min(88vh, 760px);
  overflow: auto;
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 72px rgba(0, 0, 0, 0.28);
}

.dialog-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.dialog-header h2 {
  margin: 0;
  font-size: 22px;
}

.contact-form {
  display: grid;
  gap: 14px;
  padding: 16px;
}

.contact-form input[type="file"] {
  min-height: 44px;
  padding: 9px 10px;
  border: 1px dashed var(--line);
}

.form-message {
  min-height: 22px;
  margin: 0;
  color: var(--warm);
  font-size: 14px;
  font-weight: 700;
}

.form-message.success {
  color: var(--accent-strong);
}

.form-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 760px) {
  .app-header {
    background: linear-gradient(145deg, #164a45 0%, #257469 68%, #9b5534 100%);
  }

  .header-inner,
  .page-shell {
    width: min(100% - 24px, 1180px);
  }

  .header-inner {
    padding: 28px 0 24px;
  }

  h1 {
    font-size: 34px;
  }

  .summary {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-top: 18px;
  }

  .summary span {
    min-width: 0;
    padding: 9px 10px;
    font-size: 13px;
  }

  .summary strong {
    display: block;
    margin-right: 0;
    font-size: 22px;
    line-height: 1.1;
  }

  .page-shell {
    margin-top: 14px;
    margin-bottom: 28px;
  }

  .toolbar {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 12px;
    box-shadow: 0 10px 24px rgba(27, 45, 40, 0.1);
  }

  .contacts-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .stats-section {
    grid-template-columns: 1fr;
    gap: 12px;
    margin-bottom: 16px;
  }

  .result-bar {
    margin: 16px 0 10px;
  }

  .viewer-figure {
    max-width: 100%;
    max-height: 86vh;
  }

  .viewer-figure img {
    max-height: calc(86vh - 62px);
  }

  .dialog-panel {
    align-self: end;
    max-height: 92vh;
  }
}

@media (max-width: 430px) {
  .header-inner,
  .page-shell {
    width: calc(100% - 20px);
  }

  .eyebrow {
    font-size: 13px;
  }

  h1 {
    font-size: 29px;
  }

  .summary {
    grid-template-columns: 1fr 1fr;
  }

  .summary span:last-child {
    grid-column: span 2;
  }

  .stats-panel {
    padding: 12px;
  }

  .bar-row {
    grid-template-columns: minmax(48px, 80px) minmax(0, 1fr) 28px;
  }

  .contact-card {
    min-height: 126px;
  }
}

@media (max-width: 390px) {
  .contact-card {
    grid-template-columns: 68px minmax(0, 1fr);
    gap: 12px;
    padding: 12px;
  }

  .portrait-button,
  .portrait {
    width: 68px;
    height: 88px;
  }

  .contact-name {
    font-size: 18px;
  }

  .meta-row {
    grid-template-columns: 38px minmax(0, 1fr);
    gap: 6px;
  }

  .phone-link {
    font-size: 14px;
  }
}

@media (max-width: 340px) {
  .contact-card {
    grid-template-columns: 60px minmax(0, 1fr);
    gap: 10px;
    padding: 10px;
  }

  .portrait-button,
  .portrait {
    width: 60px;
    height: 78px;
  }

  .meta-row {
    grid-template-columns: 34px minmax(0, 1fr);
    gap: 5px;
  }

  .meta dt,
  .chip {
    font-size: 12px;
  }
}
