:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1d2129;
  --muted: #6b7280;
  --border: #d9dce1;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --stage: #1b1d22;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1c1f25;
    --text: #e6e8eb;
    --muted: #9aa1ac;
    --border: #30343c;
    --accent: #4f8cff;
    --stage: #0b0c0f;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  padding: 16px 24px 4px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.topbar h1 { margin: 0; font-size: 22px; }
.tagline { margin: 2px 0 0; color: var(--muted); }

.repo-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  margin-top: 4px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.repo-link:hover { border-color: var(--accent); }

@media (max-width: 560px) {
  .topbar { flex-direction: column; gap: 8px; }
  .repo-link { margin-top: 0; }
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  padding: 16px 24px 24px;
  align-items: start;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
}

.workspace {
  background: var(--stage);
  border-radius: 10px;
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: hidden;
}

.dropzone {
  width: 100%;
  min-height: 50vh;
  border: 2px dashed #555b66;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #c9ced6;
  text-align: center;
}
.dropzone.over { border-color: var(--accent); background: rgba(79, 140, 255, 0.08); }
.dropzone p { margin: 0; }
.dropzone[hidden] { display: none; }

.stage {
  position: relative;
  line-height: 0;
  user-select: none;
  touch-action: none;
  overflow: hidden;
}
.stage[hidden] { display: none; }

#photo {
  display: block;
  max-width: 100%;
  max-height: 75vh;
}

.crop {
  position: absolute;
  border: 1px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.55);
  cursor: move;
  outline: none;
}
.crop:focus-visible { border-color: var(--accent); }

.grid {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(to right, transparent calc(33.333% - 0.5px), rgba(255,255,255,0.35) calc(33.333% - 0.5px), rgba(255,255,255,0.35) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px),
      transparent calc(66.666% - 0.5px), rgba(255,255,255,0.35) calc(66.666% - 0.5px), rgba(255,255,255,0.35) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px)),
    linear-gradient(to bottom, transparent calc(33.333% - 0.5px), rgba(255,255,255,0.35) calc(33.333% - 0.5px), rgba(255,255,255,0.35) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px),
      transparent calc(66.666% - 0.5px), rgba(255,255,255,0.35) calc(66.666% - 0.5px), rgba(255,255,255,0.35) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px));
}

.handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.5);
  border-radius: 2px;
}
@media (pointer: coarse) {
  .handle { width: 22px; height: 22px; }
}
.handle[data-handle="nw"] { left: 0;    top: 0;    cursor: nwse-resize; transform: translate(0, 0); }
.handle[data-handle="n"]  { left: 50%;  top: 0;    cursor: ns-resize; transform: translate(-50%, 0); }
.handle[data-handle="ne"] { left: 100%; top: 0;    cursor: nesw-resize; transform: translate(-100%, 0); }
.handle[data-handle="e"]  { left: 100%; top: 50%;  cursor: ew-resize; transform: translate(-100%, -50%); }
.handle[data-handle="se"] { left: 100%; top: 100%; cursor: nwse-resize; transform: translate(-100%, -100%); }
.handle[data-handle="s"]  { left: 50%;  top: 100%; cursor: ns-resize; transform: translate(-50%, -100%); }
.handle[data-handle="sw"] { left: 0;    top: 100%; cursor: nesw-resize; transform: translate(0, -100%); }
.handle[data-handle="w"]  { left: 0;    top: 50%;  cursor: ew-resize; transform: translate(0, -50%); }

.sidebar { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.panel h2 { margin: 0 0 10px; font-size: 15px; }

.ratios {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.ratios button,
.button {
  font: inherit;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.ratios button:hover, .button:hover:not(:disabled) { border-color: var(--accent); }
.ratios button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.button:disabled { opacity: 0.5; cursor: not-allowed; }
.button.wide { width: 100%; margin-top: 8px; }
label.button { display: inline-block; line-height: 1.45; }

.row { display: flex; gap: 6px; margin-top: 10px; }
.row .button { flex: 1; }

.hint, .muted { color: var(--muted); font-size: 13px; }
.hint { margin: 10px 0 0; }

.preview-box {
  background: repeating-conic-gradient(#8882 0% 25%, transparent 0% 50%) 0 0 / 16px 16px;
  border-radius: 6px;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#preview { display: block; max-width: 100%; }
#preview[hidden] { display: none; }
#preview-empty { margin: 0; padding: 16px; text-align: center; }
#dimensions { margin: 8px 0 0; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; }
.field span { color: var(--muted); }
.field select, .field input[type="number"] {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}
.field[hidden] { display: none; }
