:root {
  --bg: #f6f6f4;
  --card: #ffffff;
  --ink: #1c1c1e;
  --muted: #6b6b70;
  --accent: #2f6f4f;
  --line: #e2e2de;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.5rem 1rem 4rem;
}

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
.sub { color: var(--muted); margin: 0 0 1.25rem; }

.drop {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 120px;
  border: 2px dashed var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--muted);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.drop:active, .drop.hover { border-color: var(--accent); background: #fbfdfb; }
.drop small { color: var(--muted); }

.grid {
  list-style: none;
  padding: 0;
  margin: 1rem 0 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: .6rem;
}
.grid li {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 10px;
  overflow: hidden;
  background: #ddd;
  cursor: grab;
  touch-action: manipulation;
}
.grid img { width: 100%; height: 100%; object-fit: cover; display: block; }
.grid .num {
  position: absolute; top: 4px; left: 4px;
  background: rgba(0,0,0,.65); color: #fff;
  font-size: .75rem; font-weight: 600;
  width: 22px; height: 22px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.grid .rm {
  position: absolute; top: 4px; right: 4px;
  background: rgba(0,0,0,.65); color: #fff; border: none;
  width: 22px; height: 22px; border-radius: 50%;
  font-size: 1rem; line-height: 1; cursor: pointer;
}
.sortable-ghost { opacity: .4; }

.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 1.25rem;
  flex-wrap: wrap;
}
.check { color: var(--muted); display: flex; align-items: center; gap: .5rem; }

button {
  font: inherit;
  border: none;
  border-radius: 10px;
  padding: .6rem 1.1rem;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:disabled { opacity: .45; cursor: default; }
#go { font-weight: 600; }

.status {
  margin-top: 1.25rem;
  padding: .8rem 1rem;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--muted);
}
.status.err { color: #b02a2a; border-color: #e6c9c9; background: #fdf6f6; }

.result { margin-top: 1.5rem; }
.result-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: .5rem; gap: .5rem;
}
#result-label { color: var(--muted); font-size: .9rem; }
.result-actions { display: flex; gap: .5rem; }
.result-actions button { background: #e9e9e6; color: var(--ink); padding: .45rem .8rem; }
#output {
  width: 100%;
  min-height: 320px;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  font: inherit;
  resize: vertical;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e; --card: #2a2a2c; --ink: #f2f2f2;
    --muted: #9a9a9e; --accent: #4a9c72; --line: #3a3a3c;
  }
  .drop:active, .drop.hover { background: #232323; }
  .result-actions button { background: #3a3a3c; color: var(--ink); }
  .status.err { background: #2c1f1f; border-color: #5a3a3a; color: #f0b0b0; }
}
