/*
 * Tool UI components, loaded only on pages with an interactive tool.
 * Relies on tokens from main.css.
 *
 * 1. Tool panel  2. Forms  3. Segmented control  4. Dropzone  5. File summary & preview
 * 6. Progress & status  7. Stats  8. Actions  9. Tool page layout  10. Resizer  11. Cropper  12. Converter
 */

/* 1. Tool panel ------------------------------------------------------------ */

.tool-panel {
  display: grid;
  gap: var(--space-5);
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface-raised);
}

@media (min-width: 48rem) {
  .tool-panel {
    padding: var(--space-6);
  }
}

.tool-panel > * {
  margin: 0;
}

/* 2. Forms ----------------------------------------------------------------- */

.form-grid {
  display: grid;
  gap: var(--space-4);
}

@media (min-width: 30rem) {
  .form-grid--2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.field {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.field__label {
  padding: 0;
  font-weight: 600;
}

.field__label-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
}

.field__hint {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.field__error {
  display: flex;
  gap: var(--space-2);
  margin: 0;
  color: var(--color-danger-text);
  font-size: var(--text-sm);
  font-weight: 500;
}

.field__error svg {
  flex: none;
  width: 1rem;
  height: 1rem;
  margin-top: 0.2rem;
}

.input,
.select {
  width: 100%;
  min-height: var(--touch-target);
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-surface-raised);
  color: var(--color-text);
  font-size: max(1rem, 16px); /* ≥16px prevents iOS zoom on focus */
}

.input:focus-visible,
.select:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 0;
  border-color: var(--color-primary);
}

.input[aria-invalid="true"] {
  border-color: var(--color-danger);
}

.input:disabled,
.select:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.input-group {
  display: flex;
}

.input-group .input {
  border-start-end-radius: 0;
  border-end-end-radius: 0;
}

.input-group__addon {
  display: flex;
  align-items: center;
  padding-inline: var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-left: 0;
  border-start-end-radius: var(--radius);
  border-end-end-radius: var(--radius);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
  cursor: pointer;
}

.checkbox input {
  width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  accent-color: var(--color-primary);
}

.range {
  width: 100%;
  height: var(--touch-target);
  margin: 0;
  accent-color: var(--color-primary);
  cursor: pointer;
}

.range-value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* 3. Segmented control (radio group styled as buttons) --------------------- */

.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.segmented__option {
  position: relative;
}

.segmented__option input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.segmented__option label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--touch-target);
  min-height: var(--touch-target);
  padding: 0.375rem 0.875rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-surface-raised);
  font-weight: 600;
  font-size: var(--text-sm);
  cursor: pointer;
  transition:
    background-color var(--duration),
    border-color var(--duration);
}

.segmented__option input:checked + label {
  border-color: var(--color-primary);
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.segmented__option input:focus-visible + label {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.segmented__option input:disabled + label {
  opacity: 0.55;
  cursor: not-allowed;
}

/* 4. Dropzone -------------------------------------------------------------- */

.dropzone {
  position: relative;
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
  border: 2px dashed var(--color-border-strong);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  text-align: center;
  cursor: pointer;
  transition:
    background-color var(--duration),
    border-color var(--duration);
}

@media (min-width: 48rem) {
  .dropzone {
    padding-block: var(--space-8);
  }
}

.dropzone > * {
  margin: 0;
}

.dropzone:hover,
.dropzone.is-dragover {
  border-color: var(--color-brand);
  background: var(--color-primary-soft);
}

.dropzone.is-dragover {
  border-style: solid;
}

.dropzone.is-disabled {
  opacity: 0.6;
  pointer-events: none;
}

.dropzone.is-invalid {
  border-color: var(--color-danger);
}

.dropzone__icon {
  width: 2.5rem;
  height: 2.5rem;
  color: var(--color-brand);
}

.dropzone__title {
  font-size: var(--text-lg);
  font-weight: 600;
}

.dropzone__input:focus-visible + .dropzone__button {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.dropzone__hint {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

/* Touch devices cannot drag files in or paste easily: show a picker-first message instead. */
.dropzone__touch-only {
  display: none;
}

@media (hover: none) and (pointer: coarse) {
  .dropzone__pointer-only {
    display: none;
  }

  .dropzone__touch-only {
    display: block;
  }
}

/* 5. File summary & preview ------------------------------------------------ */

.file-summary {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.file-summary__thumb {
  flex: none;
  width: 4rem;
  height: 4rem;
  border-radius: var(--radius);
  object-fit: cover;
  background: var(--color-surface);
}

.file-summary__body {
  min-width: 0;
}

.file-summary__name {
  margin: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.file-summary__meta {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

/* Checkerboard reveals transparency in PNG/WebP images. */
.preview {
  display: grid;
  place-items: center;
  min-height: 10rem;
  max-height: 65vh;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background-color: var(--color-checker-a);
  background-image: conic-gradient(
    var(--color-checker-b) 25%,
    transparent 0 50%,
    var(--color-checker-b) 0 75%,
    transparent 0
  );
  background-size: 16px 16px;
}

.preview img,
.preview canvas {
  max-width: 100%;
  max-height: 65vh;
  object-fit: contain;
}

/* 6. Progress & status ----------------------------------------------------- */

.status {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin: 0;
  font-weight: 500;
}

.spinner {
  flex: none;
  width: 1.5rem;
  height: 1.5rem;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.progress {
  display: grid;
  gap: var(--space-2);
}

.progress__bar {
  width: 100%;
  height: 0.5rem;
  border: 0;
  border-radius: var(--radius-full);
  background: var(--color-border);
  overflow: hidden;
  appearance: none;
  color: var(--color-primary);
}

.progress__bar::-webkit-progress-bar {
  background: var(--color-border);
}

.progress__bar::-webkit-progress-value {
  border-radius: var(--radius-full);
  background: var(--color-primary);
  transition: width var(--duration);
}

.progress__bar::-moz-progress-bar {
  border-radius: var(--radius-full);
  background: var(--color-primary);
}

.progress__label {
  display: flex;
  justify-content: space-between;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

/* 7. Stats (result summary) ------------------------------------------------ */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 5.5rem), 1fr));
  gap: var(--space-3);
  margin: 0;
}

.stat {
  min-width: 0;
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.stat__label {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.stat__value {
  margin: 0;
  font-size: clamp(1.0625rem, 0.9rem + 0.8vw, 1.25rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat--highlight {
  border-color: transparent;
  background: var(--color-success-soft);
}

.stat--highlight .stat__value {
  color: var(--color-success-text);
}

/* 8. Actions --------------------------------------------------------------- */

.actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

@media (min-width: 30rem) {
  .actions {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

/* 9. Tool page layout ------------------------------------------------------- */

.container--tool {
  max-width: 48rem;
}

.hero--tool {
  padding-block: var(--space-6) var(--space-2);
}

.section--tool {
  padding-top: 0;
}

.hero__tool {
  margin-top: var(--space-5);
}

.hero__tool-link {
  margin: var(--space-3) 0 0;
  font-size: var(--text-sm);
  text-align: center;
}

.tool-stage {
  display: grid;
  gap: var(--space-4);
}

.tool-stage > * {
  margin: 0;
}

.tool-source {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.tool-result__title {
  font-size: var(--text-xl);
  letter-spacing: -0.02em;
}

.tool-result__title:focus {
  outline: none;
}

.stat--muted .stat__value {
  color: var(--color-text-muted);
}

/* Numbered how-to steps */
.steps {
  display: grid;
  gap: var(--space-3);
  max-width: var(--container-narrow);
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: step;
}

.steps li {
  position: relative;
  padding-left: 2.75rem;
  counter-increment: step;
}

.steps li::before {
  content: counter(step);
  position: absolute;
  top: -0.125rem;
  left: 0;
  display: grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  border-radius: var(--radius);
  background: var(--color-action);
  color: var(--color-on-action);
  font-weight: 800;
  font-size: var(--text-sm);
}

/* 10. Resizer ------------------------------------------------------------- */

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0 var(--space-5);
}

.resize-summary {
  display: grid;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.resize-summary > * {
  margin: 0;
}

.resize-summary__size {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* 11. Cropper ------------------------------------------------------------- */

/* Clips the dimming overlay; the padding leaves room for handles at the image edges. */
.cropper {
  display: flex;
  justify-content: center;
  overflow: hidden;
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background-color: var(--color-checker-a);
  background-image: conic-gradient(var(--color-checker-b) 25%, transparent 0 50%, var(--color-checker-b) 0 75%, transparent 0);
  background-size: 16px 16px;
}

/* Sized exactly to the rendered image, so percentage positions map to image pixels. */
.cropper__stage {
  position: relative;
  line-height: 0;
  user-select: none;
  -webkit-user-select: none;
}

.cropper__image {
  max-width: 100%;
  max-height: 60vh;
  width: auto;
  height: auto;
  pointer-events: none;
  -webkit-user-drag: none;
}

.cropper__box {
  position: absolute;
  border: 2px solid #ffffff;
  outline: 1px solid rgb(0 0 0 / 0.6);
  /* Dims everything outside the crop area. */
  box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.55);
  cursor: move;
  touch-action: none;
  /* Rule-of-thirds guides. */
  background-image:
    linear-gradient(to right, transparent calc(100% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(100% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(100% / 3 + 0.5px), transparent calc(100% / 3 + 0.5px), transparent calc(200% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(200% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(200% / 3 + 0.5px), transparent calc(200% / 3 + 0.5px)),
    linear-gradient(to bottom, transparent calc(100% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(100% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(100% / 3 + 0.5px), transparent calc(100% / 3 + 0.5px), transparent calc(200% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(200% / 3 - 0.5px), rgb(255 255 255 / 0.55) calc(200% / 3 + 0.5px), transparent calc(200% / 3 + 0.5px));
}

.cropper__box:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.cropper__handle {
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid var(--color-primary);
  border-radius: 3px;
  background: #ffffff;
  touch-action: none;
}

/* Invisible 44px hit area around each handle for touch. */
.cropper__handle::after {
  content: "";
  position: absolute;
  inset: -15px;
}

.cropper__handle--nw { top: -8px; left: -8px; cursor: nwse-resize; }
.cropper__handle--se { bottom: -8px; right: -8px; cursor: nwse-resize; }
.cropper__handle--ne { top: -8px; right: -8px; cursor: nesw-resize; }
.cropper__handle--sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.cropper__handle--n { top: -8px; left: calc(50% - 7px); cursor: ns-resize; }
.cropper__handle--s { bottom: -8px; left: calc(50% - 7px); cursor: ns-resize; }
.cropper__handle--e { right: -8px; top: calc(50% - 7px); cursor: ew-resize; }
.cropper__handle--w { left: -8px; top: calc(50% - 7px); cursor: ew-resize; }

/* On touch screens, corner handles are enough and edge handles would crowd small crops. */
@media (pointer: coarse) {
  .cropper__handle--n,
  .cropper__handle--s,
  .cropper__handle--e,
  .cropper__handle--w {
    display: none;
  }

  .cropper__handle {
    width: 18px;
    height: 18px;
  }

  .cropper__handle--nw { top: -10px; left: -10px; }
  .cropper__handle--se { bottom: -10px; right: -10px; }
  .cropper__handle--ne { top: -10px; right: -10px; }
  .cropper__handle--sw { bottom: -10px; left: -10px; }
}

.crop-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}

@media (min-width: 40rem) {
  .crop-fields {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.field__label--sm {
  font-size: var(--text-sm);
}

/* 12. Converter ----------------------------------------------------------- */

.conversion-banner {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin: 0;
  font-weight: 700;
}

.conversion-banner svg {
  width: 1.25rem;
  height: 1.25rem;
  color: var(--color-text-muted);
}

.conversion-banner__format {
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.conversion-banner__format--target {
  border-color: var(--color-primary);
  background: var(--color-primary-soft);
  color: var(--color-primary);
}

.input--color {
  width: 4.5rem;
  padding: var(--space-1);
  cursor: pointer;
}

/* Images → PDF: ordered list of pages ------------------------------------- */

.pdf-list__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.pdf-list__count {
  margin: 0;
  font-weight: 600;
}

.pdf-list__header .is-disabled {
  opacity: 0.5;
  pointer-events: none;
}

.pdf-pages {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.pdf-page {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg);
}

.pdf-page__thumb {
  flex: none;
  width: 3.5rem;
  height: 3.5rem;
  border: 1px solid var(--color-border); /* keeps white pages (receipts, documents) visible */
  border-radius: var(--radius);
  object-fit: cover;
  background: var(--color-surface);
}

.pdf-page__info {
  display: grid;
  flex: 1;
  min-width: 0;
}

.pdf-page__number {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* Long file names wrap to two lines instead of being cut to a few characters on phones. */
.pdf-page__name {
  display: -webkit-box;
  overflow: hidden;
  font-weight: 600;
  overflow-wrap: break-word;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
}

.pdf-page__meta {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.pdf-page__actions {
  display: flex;
  flex: none;
}

.pdf-page__button {
  min-width: var(--touch-target);
  min-height: var(--touch-target);
  padding: 0;
}

.pdf-page__button:disabled {
  opacity: 0.35;
}

/* Phones: the buttons move to their own row so the file name gets the full width. */
@media (max-width: 36rem) {
  .pdf-page {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    row-gap: var(--space-1);
  }

  .pdf-page__actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
    margin-top: calc(var(--space-1) * -1);
  }
}

/* The page list also accepts dropped files. */
.tool-stage.is-dragover {
  outline: 2px dashed var(--color-brand);
  outline-offset: var(--space-2);
  border-radius: var(--radius);
}

/* Metadata remover: what the photo reveals ------------------------------- */

.findings {
  display: grid;
  gap: var(--space-2);
}

.findings__title {
  margin: 0;
  font-size: var(--text-base);
}

.findings__list {
  display: grid;
  gap: var(--space-1);
  margin: 0;
  padding: 0;
  list-style: none;
}

.findings__item {
  padding: var(--space-2) var(--space-3);
  border-left: 3px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  overflow-wrap: anywhere;
}

/* Location is the most sensitive finding. */
.findings__item--warning {
  border-left-color: var(--color-warning);
  font-weight: 600;
}

/* Rotate & flip ------------------------------------------------------------ */

/* Checkerboard behind the preview reveals transparent areas, like .preview. */
.rotate-preview {
  display: grid;
  place-items: center;
  min-height: 12rem;
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background-color: var(--color-checker-a);
  background-image: conic-gradient(var(--color-checker-b) 25%, transparent 0 50%, var(--color-checker-b) 0 75%, transparent 0);
  background-size: 16px 16px;
}

.rotate-preview__canvas {
  max-width: 100%;
  max-height: 55vh;
  object-fit: contain;
}

.rotate-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-2);
}

@media (min-width: 40rem) {
  .rotate-controls {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.rotate-controls__button {
  padding-inline: var(--space-3);
}

.rotate-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.rotate-summary__state {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
