:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #687083;
  --line: #e0e5ef;
  --primary: #4c8eff;
  --primary-pressed: #3c72cc;
  --shadow: 0 18px 48px rgba(30, 42, 76, 0.16);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--tg-theme-bg-color, var(--bg));
  color: var(--tg-theme-text-color, var(--text));
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  padding: max(18px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom));
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 0.92fr) minmax(320px, 1.08fr);
  gap: 18px;
  max-width: 1180px;
  margin: 0 auto;
}

.preview-pane,
.editor-pane {
  min-width: 0;
}

.phone-frame {
  position: sticky;
  top: 18px;
  aspect-ratio: 9 / 16;
  max-height: calc(100vh - 36px);
  min-height: 560px;
  overflow: hidden;
  background: #dce3f2;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.theme-preview {
  display: none;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.theme-preview.visible {
  display: block;
}

.empty-preview {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.78), rgba(255, 255, 255, 0.34)),
    url("data:image/svg+xml,%3Csvg width='48' height='48' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 47h48M47 0v48' stroke='%23c8d0df' stroke-width='1'/%3E%3C/svg%3E");
}

.empty-preview.hidden {
  display: none;
}

.attach-button,
.primary-button,
.secondary-button,
.icon-button {
  border: 0;
  cursor: pointer;
  border-radius: 8px;
  transition: transform 0.16s ease, background 0.16s ease, opacity 0.16s ease;
}

.attach-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: min(100%, 320px);
  min-height: 68px;
  padding: 0 24px;
  background: var(--primary);
  color: #fff;
  font-size: 20px;
  font-weight: 750;
}

.attach-button:active,
.primary-button:active,
.secondary-button:active,
.icon-button:active {
  transform: translateY(1px);
}

.attach-icon {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 2px solid currentColor;
  border-radius: 50%;
  font-size: 22px;
  line-height: 1;
}

.file-input {
  display: none;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.topbar h1 {
  margin: 0;
  color: var(--tg-theme-text-color, var(--text));
  font-size: 30px;
  line-height: 1.1;
}

.topbar p {
  margin: 7px 0 0;
  color: var(--tg-theme-hint-color, var(--muted));
  font-size: 16px;
}

.icon-button {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  background: var(--panel);
  color: var(--primary);
  border: 1px solid var(--line);
  font-size: 24px;
  font-weight: 800;
}

.editor-controls {
  display: grid;
  gap: 14px;
}

.control-section {
  padding: 16px;
  background: var(--tg-theme-secondary-bg-color, var(--panel));
  border: 1px solid var(--line);
  border-radius: 8px;
}

.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title h2 {
  margin: 0;
  color: var(--tg-theme-text-color, var(--text));
  font-size: 18px;
}

.section-title span {
  color: var(--tg-theme-hint-color, var(--muted));
  font-size: 14px;
  font-weight: 700;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.swatch {
  position: relative;
  min-height: 62px;
  border: 2px solid rgba(17, 24, 39, 0.08);
  border-radius: 8px;
  overflow: hidden;
  background: var(--swatch);
}

.swatch.active {
  border-color: #111827;
  box-shadow: 0 0 0 3px rgba(17, 24, 39, 0.16);
}

.swatch span {
  position: absolute;
  left: 8px;
  bottom: 6px;
  padding: 3px 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.78);
  color: #111827;
  font-size: 12px;
  font-weight: 800;
}

.manual-row {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 10px;
  margin-top: 12px;
}

input[type="color"] {
  width: 58px;
  height: 46px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
}

.hex-input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  color: var(--tg-theme-text-color, var(--text));
  background: var(--tg-theme-bg-color, #fff);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-weight: 750;
  text-transform: uppercase;
}

.channel-input {
  width: 100%;
  height: 48px;
  padding: 0 14px;
  color: var(--tg-theme-text-color, var(--text));
  background: var(--tg-theme-bg-color, #fff);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-weight: 750;
}

.opacity-slider {
  width: 100%;
  accent-color: var(--primary);
}

.opacity-scale {
  display: flex;
  justify-content: space-between;
  color: var(--tg-theme-hint-color, var(--muted));
  font-size: 13px;
}

.action-row {
  display: grid;
  grid-template-columns: 0.8fr 1.2fr;
  gap: 12px;
}

.primary-button,
.secondary-button {
  min-height: 56px;
  padding: 0 18px;
  font-size: 17px;
  font-weight: 800;
}

.primary-button {
  background: var(--primary);
  color: #fff;
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: default;
  opacity: 0.55;
}

.secondary-button {
  color: var(--primary);
  background: var(--tg-theme-secondary-bg-color, var(--panel));
  border: 1px solid var(--line);
}

.download-links {
  display: grid;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.download-links a {
  color: var(--primary);
  font-weight: 800;
  text-decoration: none;
}

.hidden {
  display: none !important;
}

@media (max-width: 820px) {
  .app-shell {
    padding: 12px;
  }

  .workspace {
    display: block;
  }

  .phone-frame {
    position: relative;
    top: 0;
    width: min(100%, 430px);
    min-height: 0;
    max-height: none;
    margin: 0 auto 16px;
  }

  .palette-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
