/* ==========================================================================
   ALGO IMAGE CROP TOOL — STYLES
   Mirrors the Algo Image Resize Tool architecture: external CSS only,
   token-driven, component-based, fully responsive.
   ========================================================================== */

* { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  background: var(--c-bg);
  color: var(--c-text);
  font-family: var(--font-body);
  font-size: var(--fs-base);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  transition: background var(--dur-base) var(--ease-out), color var(--dur-base) var(--ease-out);
}

img { max-width: 100%; display: block; }
button { font-family: inherit; cursor: pointer; }
a { color: inherit; text-decoration: none; }

.container {
  max-width: var(--container-max);
  margin-inline: auto;
  padding-inline: var(--sp-5);
}

/* Skip link for keyboard users */
.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--c-accent);
  color: #05130D;
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--r-sm);
  z-index: 999;
  font-weight: 600;
}
.skip-link:focus { left: var(--sp-4); top: var(--sp-4); }

:focus-visible {
  outline: 2px solid var(--c-accent-2);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ---------- Header ---------- */
.site-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--c-border);
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--c-bg) 88%, transparent);
  backdrop-filter: blur(10px);
  z-index: 50;
}
.site-header .container { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-4); width: -webkit-fill-available; }

.brand { display: flex; align-items: center; gap: var(--sp-3); font-family: var(--font-display); font-weight: 600; font-size: var(--fs-md); }
.brand-mark {
  width: 36px; height: 36px; border-radius: var(--r-sm);
  background: linear-gradient(135deg, var(--c-accent), var(--c-accent-2));
  display: flex; align-items: center; justify-content: center;
  color: #05130D; font-weight: 700; font-family: var(--font-display);
}
.brand-tool { color: var(--c-text-muted); font-weight: 400; font-size: var(--fs-sm); }

.header-actions { display: flex; align-items: center; gap: var(--sp-3); }

.icon-btn {
  width: 40px; height: 40px; border-radius: var(--r-sm);
  border: 1px solid var(--c-border); background: var(--c-surface);
  color: var(--c-text-muted); display: flex; align-items: center; justify-content: center;
  transition: border-color var(--dur-fast), color var(--dur-fast), transform var(--dur-fast);
}
.icon-btn:hover { color: var(--c-text); border-color: var(--c-accent-2); transform: translateY(-1px); }
.icon-btn svg { width: 18px; height: 18px; }

.lang-switch { display: flex; border: 1px solid var(--c-border); border-radius: var(--r-full); overflow: hidden; }
.lang-switch button {
  border: 0; background: transparent; color: var(--c-text-muted);
  padding: var(--sp-2) var(--sp-3); font-size: var(--fs-xs); font-weight: 600;
  letter-spacing: 0.02em;
}
.lang-switch button[aria-pressed="true"] { background: var(--c-accent-soft); color: var(--c-accent); }

/* ---------- Hero ---------- */
.hero { padding-block: var(--sp-8) var(--sp-6); text-align: center; }
.hero h1 {
  font-family: var(--font-display); font-size: clamp(var(--fs-2xl), 4vw, var(--fs-3xl));
  font-weight: 700; letter-spacing: -0.01em; margin-bottom: var(--sp-3);
}
.hero p { color: var(--c-text-muted); max-width: 640px; margin-inline: auto; font-size: var(--fs-md); }

/* ---------- Steps indicator ---------- */
.steps {
  display: flex; justify-content: center; gap: var(--sp-2);
  margin-block: var(--sp-6); flex-wrap: wrap;
}
.step-pill {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-4); border-radius: var(--r-full);
  background: var(--c-surface); border: 1px solid var(--c-border);
  font-size: var(--fs-xs); color: var(--c-text-faint); font-weight: 600;
  transition: all var(--dur-base) var(--ease-out);
}
.step-pill .num {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--c-surface-2); color: var(--c-text-faint);
  display: flex; align-items: center; justify-content: center; font-size: 0.65rem;
}
.step-pill.is-active { border-color: var(--c-accent); color: var(--c-text); background: var(--c-accent-soft); }
.step-pill.is-active .num { background: var(--c-accent); color: #05130D; }
.step-pill.is-done { color: var(--c-success); }
.step-pill.is-done .num { background: var(--c-success); color: #05130D; }

/* ---------- Card ---------- */
.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

/* ---------- Upload ---------- */
.upload-section { padding-block: var(--sp-6); }

.dropzone {
  border: 2px dashed var(--c-border);
  border-radius: var(--r-lg);
  padding: var(--sp-10) var(--sp-5);
  text-align: center;
  background: var(--c-surface);
  transition: border-color var(--dur-base) var(--ease-out), background var(--dur-base) var(--ease-out);
  cursor: pointer;
}
.dropzone.is-dragover { border-color: var(--c-accent); background: var(--c-accent-soft); box-shadow: var(--shadow-glow); }
.dropzone-icon {
  width: 64px; height: 64px; margin-inline: auto var(--sp-4);
  border-radius: 50%; background: var(--c-accent-2-soft);
  display: flex; align-items: center; justify-content: center; color: var(--c-accent-2);
}
.dropzone-icon svg { width: 28px; height: 28px; }
.dropzone h3 { font-family: var(--font-display); font-size: var(--fs-lg); margin-bottom: var(--sp-2); }
.dropzone p { color: var(--c-text-muted); margin-bottom: var(--sp-4); font-size: var(--fs-sm); }
.dropzone .hint { color: var(--c-text-faint); font-size: var(--fs-xs); margin-top: var(--sp-3); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  border-radius: var(--r-md); border: 1px solid transparent;
  padding: var(--sp-3) var(--sp-5); font-weight: 600; font-size: var(--fs-sm);
  transition: transform var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out), background var(--dur-fast), border-color var(--dur-fast);
}
.btn svg { width: 16px; height: 16px; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: linear-gradient(135deg, var(--c-accent), var(--c-accent-2)); color: #05130D; box-shadow: var(--shadow-sm); }
.btn-primary:hover { box-shadow: var(--shadow-glow); transform: translateY(-1px); }
.btn-secondary { background: var(--c-surface-2); color: var(--c-text); border-color: var(--c-border); }
.btn-secondary:hover { border-color: var(--c-accent-2); }
.btn-ghost { background: transparent; color: var(--c-text-muted); border-color: var(--c-border); }
.btn-ghost:hover { color: var(--c-text); border-color: var(--c-danger); }
.btn-danger { background: transparent; color: var(--c-danger); border-color: var(--c-danger); }
.btn-danger:hover { background: rgba(239, 69, 96, 0.12); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; box-shadow: none; }
.btn-block { width: 100%; }

/* File input visually hidden but accessible */
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Selected files list ---------- */
.files-bar {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: var(--sp-5); flex-wrap: wrap; gap: var(--sp-3);
}
.files-bar h4 { font-family: var(--font-display); font-size: var(--fs-sm); color: var(--c-text-muted); }

.file-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--sp-3); margin-top: var(--sp-4);
}
.file-card {
  background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--r-md);
  overflow: hidden; position: relative; transition: border-color var(--dur-fast), transform var(--dur-fast);
}
.file-card:hover { border-color: var(--c-accent-2); transform: translateY(-2px); }
.file-card.is-active { border-color: var(--c-accent); box-shadow: var(--shadow-glow); }
.file-thumb { width: 100%; aspect-ratio: 4/3; object-fit: cover; background: var(--c-surface-2); }
.file-meta { padding: var(--sp-2) var(--sp-3); }
.file-name { font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-sub { font-size: 0.7rem; color: var(--c-text-faint); margin-top: 2px; }
.file-remove {
  position: absolute; top: var(--sp-2); right: var(--sp-2);
  width: 24px; height: 24px; border-radius: 50%; border: none;
  background: rgba(10, 14, 26, 0.7); color: #fff; display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}
.file-remove svg { width: 12px; height: 12px; }
.file-remove:hover { background: var(--c-danger); }

/* ---------- Crop workspace ---------- */
.workspace-section { padding-block: var(--sp-6); display: none; }
.workspace-section.is-visible { display: block; }

.workspace-grid {
  display: grid; grid-template-columns: 1fr 300px; gap: var(--sp-5);
}

.stage-card { padding: var(--sp-5); }
.stage {
  position: relative; width: 100%; aspect-ratio: 4/3;
  background: repeating-conic-gradient(var(--c-surface-2) 0% 25%, var(--c-bg-alt) 0% 50%) 50% / 20px 20px;
  border-radius: var(--r-md); overflow: hidden; touch-action: none;
  border: 1px solid var(--c-border);
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: grab; }
.stage canvas:active { cursor: grabbing; }

.crop-box {
  position: absolute; border: 2px solid var(--c-accent);
  box-shadow: 0 0 0 9999px rgba(10, 14, 26, 0.55);
  cursor: move;
}
.crop-box::before {
  content: ""; position: absolute; inset: 0;
  background-image:
    linear-gradient(to right, rgba(244,246,251,0.35) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(244,246,251,0.35) 1px, transparent 1px);
  background-size: 33.333% 33.333%;
  pointer-events: none;
}
.crop-handle {
  position: absolute; width: 16px; height: 16px; background: var(--c-accent);
  border: 2px solid var(--c-bg); border-radius: 4px; z-index: 2;
}
.crop-handle.nw { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-handle.ne { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-handle.sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-handle.se { bottom: -8px; right: -8px; cursor: nwse-resize; }
.crop-handle.n { top: -8px; left: calc(50% - 8px); cursor: ns-resize; }
.crop-handle.s { bottom: -8px; left: calc(50% - 8px); cursor: ns-resize; }
.crop-handle.e { right: -8px; top: calc(50% - 8px); cursor: ew-resize; }
.crop-handle.w { left: -8px; top: calc(50% - 8px); cursor: ew-resize; }

.stage-toolbar {
  display: flex; align-items: center; justify-content: center; gap: var(--sp-2);
  margin-top: var(--sp-4); flex-wrap: wrap;
}
.stage-toolbar .icon-btn { width: 42px; height: 42px; }

.zoom-row { display: flex; align-items: center; gap: var(--sp-3); margin-top: var(--sp-4); }
.zoom-row input[type="range"] { flex: 1; accent-color: var(--c-accent); }
.zoom-value { font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--c-text-muted); width: 3.5em; text-align: right; }

/* ---------- Side panel ---------- */
.panel { padding: var(--sp-5); display: flex; flex-direction: column; gap: var(--sp-5); }
.panel-block h4 {
  font-family: var(--font-display); font-size: var(--fs-sm);
  color: var(--c-text-muted); margin-bottom: var(--sp-3); text-transform: uppercase; letter-spacing: 0.04em;
}

.preset-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-2); }
.preset-btn {
  background: var(--c-surface-2); border: 1px solid var(--c-border); border-radius: var(--r-sm);
  padding: var(--sp-2) var(--sp-2); font-size: var(--fs-xs); color: var(--c-text-muted);
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
  transition: border-color var(--dur-fast), color var(--dur-fast);
}
.preset-btn .ratio-icon { width: 22px; height: 16px; border: 1.5px solid currentColor; border-radius: 3px; }
.preset-btn:hover { color: var(--c-text); border-color: var(--c-accent-2); }
.preset-btn[aria-pressed="true"] { border-color: var(--c-accent); color: var(--c-accent); background: var(--c-accent-soft); }

.custom-size { display: flex; gap: var(--sp-2); align-items: center; margin-top: var(--sp-3); }
.custom-size input {
  width: 100%; background: var(--c-surface-2); border: 1px solid var(--c-border); border-radius: var(--r-sm);
  color: var(--c-text); padding: var(--sp-2); font-size: var(--fs-xs); font-family: var(--font-mono);
}
.custom-size span { color: var(--c-text-faint); font-size: var(--fs-xs); }

.info-list { list-style: none; font-size: var(--fs-xs); color: var(--c-text-muted); display: flex; flex-direction: column; gap: var(--sp-2); }
.info-list li { display: flex; justify-content: space-between; }
.info-list b { color: var(--c-text); font-family: var(--font-mono); font-weight: 500; }

.panel-actions { display: flex; flex-direction: column; gap: var(--sp-2); margin-top: auto; }

/* ---------- Processing indicator ---------- */
.processing-overlay {
  position: fixed; inset: 0; background: rgba(10, 14, 26, 0.7); backdrop-filter: blur(3px);
  display: none; align-items: center; justify-content: center; z-index: 200;
}
.processing-overlay.is-visible { display: flex; }
.processing-box {
  background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--r-lg);
  padding: var(--sp-6); text-align: center; box-shadow: var(--shadow-md);
}
.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 3px solid var(--c-border); border-top-color: var(--c-accent);
  margin: 0 auto var(--sp-3); animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Result / download ---------- */
.result-section { padding-block: var(--sp-6); display: none; }
.result-section.is-visible { display: block; }
.result-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--sp-4); }
.result-card { padding: var(--sp-3); display: flex; flex-direction: column; gap: var(--sp-3); }
.result-thumb { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: var(--r-sm); background: var(--c-surface-2); }
.result-meta { font-size: var(--fs-xs); color: var(--c-text-muted); display: flex; justify-content: space-between; }
.result-actions { display: flex; gap: var(--sp-2); }
.result-actions .btn { flex: 1; padding: var(--sp-2) var(--sp-3); font-size: var(--fs-xs); }

.result-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--sp-4); flex-wrap: wrap; gap: var(--sp-3); }

/* ---------- History ---------- */
.history-section { padding-block: var(--sp-8); }
.section-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--sp-5); flex-wrap: wrap; gap: var(--sp-3); }
.section-heading h2 { font-family: var(--font-display); font-size: var(--fs-xl); }

.history-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: var(--sp-3); }
.history-card { padding: var(--sp-2); }
.history-thumb { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: var(--r-sm); }
.history-meta { padding: var(--sp-2) var(--sp-1) 0; }
.history-name { font-size: var(--fs-xs); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-sub { font-size: 0.7rem; color: var(--c-text-faint); margin-top: 2px; }
.history-actions { display: flex; gap: var(--sp-2); margin-top: var(--sp-2); }
.history-actions button { flex: 1; padding: var(--sp-1) 0; font-size: 0.7rem; }
.history-empty { color: var(--c-text-faint); font-size: var(--fs-sm); text-align: center; padding: var(--sp-8) 0; }

/* ---------- Footer ---------- */
.site-footer { border-top: 1px solid var(--c-border); padding-block: var(--sp-6); margin-top: var(--sp-10); }
.site-footer .container { display: flex; justify-content: space-between; flex-wrap: wrap; gap: var(--sp-3); color: var(--c-text-faint); font-size: var(--fs-xs); }
.site-footer a:hover { color: var(--c-accent-2); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: var(--sp-5); left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--c-surface); border: 1px solid var(--c-border); box-shadow: var(--shadow-md);
  padding: var(--sp-3) var(--sp-5); border-radius: var(--r-md); font-size: var(--fs-sm);
  opacity: 0; pointer-events: none; transition: all var(--dur-base) var(--ease-out); z-index: 300;
  display: flex; align-items: center; gap: var(--sp-2);
}
.toast.is-visible { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.is-success { border-color: var(--c-success); }
.toast.is-error { border-color: var(--c-danger); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .workspace-grid { grid-template-columns: 1fr; }
  .panel { flex-direction: row; flex-wrap: wrap; }
  .panel-block { flex: 1 1 45%; }
  .panel-actions { flex-direction: row; flex: 1 1 100%; }
}

@media (max-width: 640px) {
  .hero h1 { font-size: var(--fs-2xl); }
  .steps { gap: var(--sp-1); }
  .step-pill span.label { display: none; }
  .dropzone { padding: var(--sp-6) var(--sp-4); }
  .panel-block { flex: 1 1 100%; }
  .stage { aspect-ratio: 1/1; }
  .brand-tool { display: none; }
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
