:root {
  --ink: #20242a;
  --muted: #6b7280;
  --paper: #fffdf8;
  --surface: #ffffff;
  --sky: #dff2ff;
  --mint: #c9f2d7;
  --coral: #ff8d86;
  --sun: #ffd66e;
  --blue: #3788d8;
  --shadow: 0 18px 46px rgba(31, 41, 55, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family:
    ui-rounded,
    "SF Pro Rounded",
    "PingFang SC",
    "Hiragino Sans GB",
    "Microsoft YaHei",
    system-ui,
    sans-serif;
  background:
    linear-gradient(180deg, rgba(223, 242, 255, 0.86), rgba(255, 253, 248, 0.98) 46%),
    var(--paper);
  overscroll-behavior: none;
}

button {
  color: inherit;
  font: inherit;
}

.app {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.app img {
  -webkit-user-drag: none;
}

.topbar {
  width: min(100%, 980px);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(26px, 5vw, 44px);
  line-height: 1;
  letter-spacing: 0;
}

.back-link {
  min-height: 34px;
  margin: -4px 0 2px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  color: #3377b7;
  background: rgba(255, 255, 255, 0.74);
  font-size: 14px;
  font-weight: 900;
  text-decoration: none;
  touch-action: manipulation;
}

.topbar p {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
}

.top-actions {
  display: flex;
  gap: 8px;
}

.icon-button {
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 8px 20px rgba(31, 41, 55, 0.14);
  font-size: 34px;
  font-weight: 900;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#fullscreenButton {
  font-size: 27px;
}

.icon-button:active,
.thumb-button:active,
.fullscreen-exit:active {
  transform: scale(0.96);
}

.play-area {
  min-height: 0;
  display: grid;
  place-items: center;
}

.art-shell {
  width: min(92vw, 62vh, 560px);
  aspect-ratio: 1122 / 1402;
  position: relative;
  overflow: hidden;
  border: 10px solid #4b9fe5;
  border-radius: 8px;
  background: white;
  box-shadow: var(--shadow);
  touch-action: none;
  user-select: none;
}

.art-shell::before {
  content: "";
  position: absolute;
  inset: 8px;
  z-index: 4;
  pointer-events: none;
  border: 4px solid #1f2937;
  border-radius: 6px;
}

.art-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.color-layer {
  z-index: 1;
  background: white;
}

.mask-layer {
  z-index: 2;
  cursor: crosshair;
}

.line-layer {
  z-index: 3;
  pointer-events: none;
}

.complete-badge {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 5;
  opacity: 0;
  transform: translateY(-8px) scale(0.92);
  transition: opacity 160ms ease, transform 160ms ease;
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--sun);
  border: 3px solid #1f2937;
  font-weight: 900;
  box-shadow: 0 8px 18px rgba(31, 41, 55, 0.18);
  pointer-events: none;
}

.complete-badge.is-visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.fullscreen-exit {
  position: absolute;
  top: 14px;
  left: 14px;
  z-index: 6;
  width: 54px;
  height: 54px;
  display: none;
  place-items: center;
  border: 3px solid #1f2937;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 8px 18px rgba(31, 41, 55, 0.18);
  font-size: 38px;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.play-area:fullscreen {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: #f4fbff;
}

body.is-immersive {
  overflow: hidden;
}

body.is-immersive .play-area {
  position: fixed;
  inset: 0;
  z-index: 20;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: #f4fbff;
}

.play-area:fullscreen .art-shell,
body.is-immersive .art-shell {
  width: min(100%, calc(100dvh * 1122 / 1402));
  max-width: 100%;
  max-height: 100%;
  border-width: 6px;
  box-shadow: none;
}

.play-area:fullscreen .fullscreen-exit,
body.is-immersive .fullscreen-exit {
  display: grid;
}

.bottom-panel {
  width: min(100%, 980px);
  margin: 0 auto;
  display: grid;
  gap: 10px;
}

.progress-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 48px;
  align-items: center;
  gap: 10px;
}

.progress-bar {
  height: 14px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: inset 0 0 0 2px rgba(31, 41, 55, 0.08);
}

#progressFill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--coral), var(--sun), var(--mint), var(--blue));
  transition: width 120ms ease;
}

#progressText {
  color: var(--muted);
  font-weight: 900;
  text-align: right;
}

.thumb-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 86px;
  gap: 10px;
  overflow-x: auto;
  padding: 2px 0 8px;
  scrollbar-width: thin;
  overscroll-behavior: contain;
  touch-action: pan-x;
}

.thumb-button {
  width: 86px;
  min-height: 104px;
  border: 3px solid transparent;
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 18px rgba(31, 41, 55, 0.1);
  padding: 6px;
  display: grid;
  grid-template-rows: 68px auto;
  gap: 4px;
  justify-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.thumb-button.is-active {
  border-color: #1f2937;
  background: #fff7c9;
}

.thumb-button img {
  width: 58px;
  height: 68px;
  object-fit: contain;
  display: block;
}

.thumb-button span {
  max-width: 100%;
  color: var(--ink);
  font-size: 13px;
  font-weight: 900;
  line-height: 1.1;
  text-align: center;
  overflow-wrap: anywhere;
}

@media (orientation: landscape) and (max-height: 700px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .app {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    grid-template-columns: minmax(0, 1fr) 112px;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "top side"
      "play side";
    gap: 10px;
  }

  .topbar {
    grid-area: top;
  }

  .play-area {
    grid-area: play;
  }

  .bottom-panel {
    grid-area: side;
    width: 112px;
    height: auto;
    min-height: 0;
    align-content: stretch;
    overflow: hidden;
  }

  .progress-wrap {
    grid-template-columns: 1fr;
  }

  #progressText {
    text-align: center;
  }

  .thumb-strip {
    min-height: 0;
    grid-auto-flow: row;
    grid-auto-rows: 104px;
    grid-auto-columns: unset;
    overflow-x: hidden;
    overflow-y: auto;
    padding-right: 6px;
    touch-action: pan-y;
  }

  .art-shell {
    width: auto;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
  }
}

@media (max-width: 520px) {
  .topbar {
    align-items: flex-start;
  }

  .top-actions {
    gap: 6px;
  }

  .icon-button {
    width: 46px;
    height: 46px;
    font-size: 30px;
  }

  .art-shell {
    width: min(94vw, 68vh);
    border-width: 7px;
  }

  .art-shell::before {
    inset: 6px;
    border-width: 3px;
  }
}
