:root {
  --bg: #f5f2ec;
  --text: #171717;
  --muted: #68645d;
  --card: #ffffff;
  --border: #ded8ce;
  --accent: #111111;
  --accent-hover: #333333;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at top left, #fff7de 0, transparent 32%), var(--bg);
  color: var(--text);
}

.app-shell {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 56px 0;
}

.hero {
  max-width: 760px;
  margin-bottom: 32px;
}

.eyebrow {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-size: clamp(40px, 7vw, 76px);
  line-height: 0.92;
  letter-spacing: -0.06em;
}

h2 {
  margin: 0;
  font-size: 22px;
  letter-spacing: -0.03em;
}

.intro {
  margin: 24px 0 0;
  font-size: 20px;
  line-height: 1.45;
  color: #2c2b28;
}

.notice {
  display: inline-block;
  margin: 20px 0 0;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  font-size: 14px;
  line-height: 1.4;
}

.card {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid var(--border);
  border-radius: 28px;
  padding: 24px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.upload-card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  align-items: stretch;
  margin-bottom: 24px;
  transition: 180ms ease;
}

.upload-card.is-compact {
  grid-template-columns: 180px 1fr;
  align-items: center;
  padding: 16px;
}

.upload-area {
  min-height: 190px;
  border: 2px dashed #c8beb0;
  border-radius: 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: 180ms ease;
  text-align: center;
  padding: 24px;
}

.upload-card.is-compact .upload-area {
  min-height: 94px;
  padding: 14px;
  gap: 4px;
}

.upload-card.is-compact .upload-icon {
  width: 34px;
  height: 34px;
  font-size: 22px;
}

.upload-card.is-compact .upload-title {
  font-size: 14px;
}

.upload-card.is-compact .upload-hint {
  font-size: 11px;
}

.upload-area:hover {
  border-color: var(--accent);
  background: #faf7f1;
}

.upload-area input {
  display: none;
}

.upload-icon {
  width: 52px;
  height: 52px;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  display: grid;
  place-items: center;
  font-size: 34px;
  line-height: 1;
}

.upload-title {
  font-size: 20px;
  font-weight: 750;
}

.upload-hint,
.muted {
  color: var(--muted);
  font-size: 14px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  gap: 12px;
  align-items: end;
}

.settings-intro {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.settings-intro strong {
  color: var(--text);
  font-size: 15px;
}

.action-field {
  justify-content: flex-end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.controls label {
  font-weight: 700;
  font-size: 13px;
}

select,
input[type="number"] {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  font: inherit;
  background: white;
  width: 100%;
}

.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.9fr);
  gap: 24px;
  align-items: start;
}

.image-card {
  position: sticky;
  top: 0;
  align-self: start;
  z-index: 2;
}

.result-card {
  max-height: calc(100vh - 24px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.result-card .section-header {
  flex: 0 0 auto;
}

.result-card .loading {
  flex: 0 0 auto;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.section-header.stacked {
  align-items: flex-start;
  flex-direction: column;
  gap: 6px;
}

.primary-button {
  border: 0;
  border-radius: 999px;
  padding: 13px 18px;
  background: var(--accent);
  color: white;
  font-weight: 800;
  cursor: pointer;
  transition: 180ms ease;
  width: 100%;
}

.primary-button:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.primary-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.image-stage {
  position: relative;
  width: 100%;
  aspect-ratio: var(--uploaded-aspect, 16 / 10);
  line-height: 0;
  margin: 0 auto;
  border-radius: 20px;
  background: #eee9df;
  overflow: hidden;
}

#imagePreview {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 20px;
  background: #eee9df;
  display: block;
}

.highlight-canvas {
  position: absolute;
  pointer-events: none;
  border-radius: 20px;
  mix-blend-mode: multiply;
}

.color-hover-cursor {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 2px solid #111;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.85);
  pointer-events: none;
  transform: translate(-50%, -50%);
  z-index: 4;
}

.color-magnifier {
  position: absolute;
  width: 108px;
  height: 108px;
  border-radius: 999px;
  border: 2px solid #111;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.24), 0 0 0 3px rgba(255, 255, 255, 0.86);
  background-repeat: no-repeat;
  pointer-events: none;
  z-index: 3;
}

.color-magnifier::before,
.color-magnifier::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  background: rgba(17, 17, 17, 0.72);
  transform: translate(-50%, -50%);
}

.color-magnifier::before {
  width: 22px;
  height: 1px;
}

.color-magnifier::after {
  width: 1px;
  height: 22px;
}

.palette {
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  min-height: 0;
  padding-right: 4px;
  scroll-behavior: smooth;
}

.color-row {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 14px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: #fff;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

.color-row.is-active,
.color-row:hover {
  border-color: #111;
  box-shadow: 0 0 0 1px #111;
}

.swatch {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 14px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 7px;
}

.swatch-percentage {
  font-size: 12px;
  line-height: 1;
  font-weight: 900;
  letter-spacing: -0.02em;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.22);
}

.row-topline {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
}

.main-result {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: baseline;
  min-width: 0;
}

.main-result strong {
  font-size: 19px;
  letter-spacing: -0.02em;
}

.main-result span {
  font-size: 16px;
  font-weight: 800;
  color: var(--text);
}

.dose-count {
  font-size: 20px;
  font-weight: 900;
  letter-spacing: -0.03em;
  text-align: right;
  white-space: nowrap;
  color: var(--text);
}

.sub-result,
.analysis-result {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  margin-top: 6px;
}

.sub-result span,
.analysis-result span {
  color: var(--muted);
  font-size: 12px;
}

.analysis-result {
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.percentage {
  font-size: 20px;
  font-weight: 850;
}

.loading {
  padding: 14px;
  border-radius: 14px;
  background: #f4efe6;
  color: var(--muted);
  margin-bottom: 14px;
}

.hidden {
  display: none !important;
}

@media (max-width: 820px) {
  .app-shell {
    width: min(100% - 20px, 1120px);
    padding: 32px 0;
  }

  .upload-card,
  .upload-card.is-compact,
  .grid {
    grid-template-columns: 1fr;
  }

  .image-card {
    position: static;
  }

  .result-card {
    max-height: none;
  }

  .palette {
    overflow-y: visible;
  }

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

  .settings-intro {
    flex-direction: column;
    gap: 4px;
  }

  .action-field {
    grid-column: 1 / -1;
  }

  .section-header {
    align-items: flex-start;
    flex-direction: column;
  }
}

/* Mobile refinement: prevent horizontal overflow and keep the image inside the viewport. */
html,
body {
  max-width: 100%;
  overflow-x: hidden;
}

img,
canvas,
.card,
.upload-card,
.image-stage,
.result-card,
.palette,
.color-row {
  max-width: 100%;
}

.mobile-selected-card {
  display: none;
}

@media (max-width: 820px) {
  body {
    width: 100%;
  }

  .app-shell {
    width: 100%;
    max-width: 100%;
    padding: 24px 10px 132px;
    overflow-x: hidden;
  }

  .hero,
  .upload-card,
  .grid,
  .card,
  .image-card,
  .result-card {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .card {
    padding: 16px;
    border-radius: 22px;
  }

  .upload-card.is-compact {
    padding: 12px;
  }

  .image-stage {
    width: 100% !important;
    height: auto !important;
    max-width: 100% !important;
    aspect-ratio: var(--uploaded-aspect, 1 / 1);
    touch-action: none;
  }

  #imagePreview {
    width: 100%;
    height: 100%;
    max-width: 100%;
  }

  .grid {
    gap: 16px;
  }

  .image-card {
    position: static !important;
    top: auto;
    z-index: auto;
  }

  .result-card {
    max-height: none !important;
    overflow: visible !important;
  }

  .palette {
    overflow-y: visible !important;
    padding-right: 0;
  }

  .color-row {
    grid-template-columns: 56px minmax(0, 1fr);
    gap: 10px;
    padding: 10px;
  }

  .swatch {
    width: 56px;
    height: 56px;
  }

  .row-topline {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .main-result strong {
    font-size: 16px;
  }

  .main-result span {
    font-size: 13px;
  }

  .dose-count {
    font-size: 17px;
  }

  .sub-result span,
  .analysis-result span {
    font-size: 11px;
  }

  .color-magnifier {
    width: 96px;
    height: 96px;
  }

  .mobile-selected-card {
    display: block;
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 20;
    background: #fff;
    border: 2px solid #111;
    border-radius: 20px;
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.24);
    padding: 10px;
    max-width: calc(100vw - 20px);
  }

  .mobile-selected-card.hidden {
    display: none !important;
  }

  .mobile-selected-card .color-row {
    border: 0;
    box-shadow: none;
    padding: 0;
  }

  .mobile-selected-card .analysis-result {
    display: none;
  }
}

/* Mobile: place the calculation button directly below the image upload area. */
@media (max-width: 820px) {
  .upload-card.is-compact {
    display: grid;
    grid-template-columns: 1fr;
  }

  .upload-card.is-compact .upload-area {
    order: 1;
  }

  .upload-card.is-compact .settings-panel {
    display: contents;
  }

  .upload-card.is-compact .settings-intro {
    order: 3;
    grid-column: 1 / -1;
  }

  .upload-card.is-compact .settings-panel > .field {
    order: 4;
  }

  .upload-card.is-compact .settings-panel > .action-field {
    order: 2;
    grid-column: 1 / -1;
    width: 100%;
    margin-top: -4px;
  }

  .upload-card.is-compact .settings-panel > .action-field .primary-button {
    width: 100%;
  }
}

/* Mobile: calculation button belongs below the displayed image, not below the upload field. */
.mobile-analyze-button {
  display: none;
}

@media (max-width: 820px) {
  .mobile-analyze-button {
    display: block;
    width: 100%;
    margin-top: 12px;
  }

  .upload-card.is-compact .settings-panel > .action-field {
    display: none !important;
  }
}

/* Cropper */
body.is-cropping {
  overflow: hidden;
}

.crop-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(17, 17, 17, 0.62);
}

.crop-dialog {
  width: min(1040px, 100%);
  max-height: calc(100vh - 40px);
  overflow: auto;
  background: #fff;
  border-radius: 28px;
  border: 1px solid var(--border);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.28);
  padding: 20px;
}

.crop-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}

.crop-header p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.4;
}

.crop-canvas-wrap {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 240px;
  padding: 12px;
  border-radius: 20px;
  background: #eee9df;
  overflow: hidden;
}

#cropCanvas {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 14px;
  cursor: move;
  touch-action: none;
}

.crop-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 16px;
}

.secondary-button {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 13px 18px;
  background: #fff;
  color: var(--text);
  font-weight: 800;
  cursor: pointer;
  transition: 180ms ease;
}

.secondary-button:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.crop-actions .primary-button {
  width: auto;
}

@media (max-width: 820px) {
  .crop-overlay {
    padding: 10px;
    align-items: start;
  }

  .crop-dialog {
    max-height: calc(100vh - 20px);
    border-radius: 22px;
    padding: 14px;
  }

  .crop-header {
    flex-direction: column;
  }

  .crop-actions {
    flex-direction: column-reverse;
  }

  .crop-actions .primary-button,
  .crop-actions .secondary-button,
  .crop-header .secondary-button {
    width: 100%;
  }
}
