:root {
  color-scheme: dark;
  --bg: #101318;
  --panel: #1d232c;
  --panel2: #252d38;
  --line: #343e4b;
  --muted: #a5afbd;
  --text: #edf1f6;
  --accent: #7896d4;
  --accent2: #91a9dc;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, Segoe UI, Arial, sans-serif;
  background: linear-gradient(145deg, #171c23 0%, var(--bg) 58%);
  color: var(--text);
}

header {
  padding: 20px 24px 14px;
  max-width: 1600px;
  margin: auto;
}

h1 {
  margin: 0 0 5px;
  font-size: clamp(1.45rem, 2.5vw, 2rem);
  letter-spacing: -.035em;
}

header p {
  color: var(--muted);
  margin: 0;
  font-size: .9rem;
}

.app {
  display: grid;
  grid-template-columns: minmax(320px, 390px) minmax(0, 1fr);
  gap: 18px;
  width: 100%;
  max-width: 1600px;
  padding: 0 24px 24px;
  margin: auto;
  align-items: start;
}

.app>* {
  min-width: 0;
}

.panel {
  background: linear-gradient(160deg, rgba(32, 39, 49, .99), rgba(25, 30, 38, .99));
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 16px 36px #0002;
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 1rem;
}

.field {
  margin: 0 0 10px;
}

.panel .field.compact-row {
  display: grid;
  grid-template-columns: minmax(100px, .8fr) minmax(0, 1.2fr);
  align-items: center;
  gap: 10px;
}

.panel .field.compact-row>label {
  margin: 0;
}

.panel .field.compact-row>select,
.panel .field.compact-row>input {
  min-width: 0;
}

label,
.label {
  display: block;
  font-size: .82rem;
  font-weight: 600;
  color: #dce8f7;
  margin-bottom: 6px;
}

input[type="file"] {
  width: 100%;
  color: var(--muted);
  font-size: .8rem;
  background: #151a21;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 9px;
}

input[type="color"] {
  width: 100%;
  height: 36px;
  padding: 3px;
  border: 1px solid var(--line);
  background: #151a21;
  border-radius: 8px;
  cursor: pointer;
}

select,
input[type="number"],
input[type="range"] {
  width: 100%;
}

select,
input[type="number"] {
  padding: 9px 10px;
  color: var(--text);
  background: #151a21;
  border: 1px solid var(--line);
  border-radius: 9px;
  font: inherit;
  font-size: .86rem;
}

select:focus,
input:focus {
  outline: 2px solid #7896d455;
  outline-offset: 1px;
}

input[type="range"] {
  accent-color: var(--accent);
  cursor: pointer;
}

.range-line {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: .72rem;
  margin-top: 4px;
}

.btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 14px;
}

button {
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 700;
  font-size: .84rem;
  transition: transform .12s, filter .12s;
}

button:hover {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

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

.primary {
  background: var(--accent);
  color: #101722;
  grid-column: 1/-1;
}

.secondary {
  background: #303945;
  border-color: #424d5b;
  color: var(--text);
}

#clear {
  grid-column: 1/-1;
}

.hint {
  color: var(--muted);
  font-size: .74rem;
  line-height: 1.4;
  margin: 8px 0 0;
}

.stage {
  min-width: 0;
  position: sticky;
  top: 12px;
}

.stage-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin: 2px 0 10px;
}

.stage-top strong {
  font-size: .95rem;
}

.stage-top span {
  color: var(--muted);
  font-size: .8rem;
}

.canvas-wrap {
  background: #0d1015;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 250px;
  width: 100%;
  min-width: 0;
  box-shadow: 0 14px 32px #0003;
}

.canvas-wrap canvas[hidden],
.canvas-wrap .empty[hidden] {
  display: none !important;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100vh - 285px);
  object-fit: contain;
  border-radius: 5px;
}

.thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 7px;
  margin-top: 10px;
  max-height: min(20vh, 220px);
  overflow-y: auto;
}

.thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  background: #151a21;
  border: 1px solid var(--line);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb button {
  position: absolute;
  top: 3px;
  right: 3px;
  padding: 2px 6px;
  border-radius: 5px;
  background: #07101ddd;
  color: white;
  font-size: .72rem;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 36px 20px;
  line-height: 1.6;
  width: 100%;
  max-width: 520px;
  margin: auto;
  flex: 1 1 100%;
  overflow-wrap: normal;
  word-break: normal;
}

.status {
  min-height: 18px;
  margin-top: 8px;
  color: var(--accent);
  font-size: .78rem;
}

#uploadLimits {
  display: block;
  margin-top: 9px;
  padding: 10px 12px;
  border: 1px solid #3a4655;
  border-left: 3px solid var(--accent);
  border-radius: 9px;
  background: #171c23;
  color: #c4ccd7;
  font-size: .76rem;
  line-height: 1.55;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(3, 8, 18, .78);
  backdrop-filter: blur(5px);
}

.loading-overlay[hidden] {
  display: none !important;
}

.loading-card {
  width: min(420px, 100%);
  padding: 24px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 24px 70px #0008;
}

.loading-card h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.loading-card p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: .85rem;
}

.progress-track {
  height: 9px;
  overflow: hidden;
  border-radius: 99px;
  background: #07101d;
  border: 1px solid var(--line);
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width .12s;
}

.loading-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 9px 0 16px;
  color: var(--muted);
  font-size: .78rem;
}

.cancel-loading {
  width: 100%;
  background: #303945;
  border-color: #424d5b;
  color: var(--text);
}

@media(max-width:1100px) {
  .app {
    grid-template-columns: 1fr;
    padding-bottom: calc(24px + min(320px, max(200px, 42vh)) + env(safe-area-inset-bottom, 0px));
  }

  .stage {
    position: fixed;
    top: auto;
    left: 24px;
    right: 24px;
    bottom: 0;
    z-index: 2;
    display: flex;
    flex-direction: column;
    height: min(320px, max(200px, 42vh));
    padding: 8px 0 max(10px, env(safe-area-inset-bottom));
    background: var(--bg);
    border-top: 1px solid var(--line);
  }

  .stage-top {
    flex: 0 0 auto;
    margin: 0 0 6px;
  }

  .canvas-wrap {
    flex: 1 1 auto;
    min-height: 80px;
  }

  canvas {
    max-height: 24vh;
  }

  .thumbs {
    flex: 0 1 auto;
    max-height: 44px;
    margin-top: 6px;
    overflow-y: auto;
  }

  .panel {
    width: 100%;
  }
}

@media(min-width:1101px) and (max-height:800px) {
  header {
    padding-top: 12px;
    padding-bottom: 8px;
  }

  .app {
    gap: 12px;
  }

  .panel {
    padding: 12px;
  }

  .field {
    margin-bottom: 8px;
  }
}

@media(max-width:560px) {
  header {
    padding: 16px 14px 10px;
  }

  .app {
    padding: 0 12px calc(18px + min(320px, max(200px, 42vh)) + env(safe-area-inset-bottom, 0px));
    gap: 12px;
  }

  .panel {
    padding: 12px;
  }

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

  .primary,
  #clear {
    grid-column: auto;
  }

  .stage-top {
    align-items: flex-start;
  }

  .stage {
    left: 12px;
    right: 12px;
    height: min(320px, max(200px, 42vh));
  }

  .canvas-wrap {
    padding: 6px;
    min-height: 80px;
  }

  .thumbs {
    max-height: 44px;
  }
}