/* ============ Reset & Base ============ */

* {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  user-select: none;
  font-family: "Mona Sans", sans-serif;
  font-optical-sizing: auto;
  font-weight: 500;
  font-style: normal;
  font-variation-settings: "wdth" 100;
}

html,
body {
  height: 100dvh;
  overscroll-behavior: none;
}

/* ============ Layout ============ */

#puzzle {
  display: flex;
  width: 100vw;
  height: 100%;
  flex-direction: column;
  align-items: flex-start;
}

main {
  display: flex;
  flex: 1 0 0;
  align-self: stretch;
  padding: 0;
  gap: 20px;
  align-items: center;
}

#center {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  align-self: stretch;
  gap: 20px;
}

#app {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1 0 0;
  align-self: stretch;
  flex-direction: column;
  gap: 1rem;
}

#copyright {
  font-size: 0.8rem;
  color: #808080;
  font-weight: 400;
  letter-spacing: 0.01rem;
  width: clamp(320px, calc(100vw - 40px), 500px);
  text-align: center;
}

/* ============ Header ============ */

header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  height: 62px;
  padding: 0 12px;
  align-items: center;
  flex-shrink: 0;
  align-self: stretch;
  border-bottom: 1px solid #e3e3e3;
}

.header-left {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}

.mobile-header {
  display: none;
}

#logo {
  width: 24px;
  height: 24px;
}

#settings,
#info,
#install-app {
  width: 40px;
  height: 40px;
  border-radius: 100%;
  padding: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

#settings:hover,
#info:hover,
#install-app:hover,
#move-counter:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

#move-counter {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 40px;
  border-radius: 100px;
  padding: 10px 14px 10px 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 0.5rem;
}

#move-counter span {
  font-size: 0.85rem;
  font-weight: 600;
}

#move-counter.solved {
  background-color: #e8f5e9 !important;
  color: #2e7d32 !important;
}

#move-counter.solved:hover {
  background-color: #c8e6c9 !important;
}

/* ============ Puzzle Board ============ */

#board {
  width: clamp(320px, calc(100vw - 40px), 500px);
  aspect-ratio: 1 / 1;
  position: relative;
  perspective: 1000px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: clamp(0px, 1.5vw, 12px);
}

#board img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tile {
  position: absolute;
  width: 25%;
  height: 25%;
  border: 1px solid rgba(255, 255, 255, 0);
  border-radius: clamp(0px, 1.5vw, 10px);
}

.front {
  position: absolute;
  width: 100%;
  height: 100%;
  background-size: 400% 400%;
  background-repeat: no-repeat;
  background-color: #e1e1e1;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 0.9rem;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.9);
  border-radius: clamp(0px, 1.5vw, 10px);
  overflow: hidden;
}

.tile .front {
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.image-piece {
  position: absolute;
  background-image: url("https://picsum.photos/500");
  background-size: 400% 400%;
  border: 1px solid #e1e1e1;
  border-radius: 6px;
}

.draggable::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  transition: background-color 0.3s ease;
  z-index: 1;
  border-radius: clamp(0px, 1.5vw, 10px);
}

.draggable:hover::after,
.draggable.touch-hover::after {
  background-color: rgba(0, 0, 0, 0.4);
}

/* Tile grid positions */
#tile1, #tile2, #tile3, #tile4 { top: 0%; }
#tile5, #tile6, #tile7, #tile8 { top: 25%; }
#tile9, #tile10, #tile11, #tile12 { top: 50%; }
#tile13, #tile14, #tile15, #tile16 { top: 75%; }

#tile1, #tile5, #tile9, #tile13 { left: 0%; }
#tile2, #tile6, #tile10, #tile14 { left: 25%; }
#tile3, #tile7, #tile11, #tile15 { left: 50%; }
#tile4, #tile8, #tile12, #tile16 { left: 75%; }

/* Tile background positions */
#tile1 > div, #tile2 > div, #tile3 > div, #tile4 > div { background-position-y: 0%; }
#tile5 > div, #tile6 > div, #tile7 > div, #tile8 > div { background-position-y: 33.333%; }
#tile9 > div, #tile10 > div, #tile11 > div, #tile12 > div { background-position-y: 66.667%; }
#tile13 > div, #tile14 > div, #tile15 > div, #tile16 > div { background-position-y: 100%; }

#tile1 > div, #tile5 > div, #tile9 > div, #tile13 > div { background-position-x: 0%; }
#tile2 > div, #tile6 > div, #tile10 > div, #tile14 > div { background-position-x: 33.333%; }
#tile3 > div, #tile7 > div, #tile11 > div, #tile15 > div { background-position-x: 66.667%; }
#tile4 > div, #tile8 > div, #tile12 > div, #tile16 > div { background-position-x: 100%; }

#tile16 { display: none; }

/* ============ Modals ============ */

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: clamp(320px, calc(100vw - 40px), 460px);
  max-width: 500px;
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
  z-index: 1000;
  display: none;
  text-align: center;
}

.modal h1 {
  font-size: 1.5rem;
  font-weight: 600;
}

.modal p {
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

.modal ul {
  list-style: none;
  padding: 0;
  border-radius: clamp(0px, 1.25vw, 10px);
  background-color: #f1f1f1;
  margin-top: 1.5rem;
  overflow: hidden;
}

.modal li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  font-size: 0.9rem;
  border-bottom: 1px solid #e3e3e3;
}

.modal li:last-child {
  border-bottom: none;
}

#info-modal li {
  text-align: left;
}

#settings-modal li {
  cursor: pointer;
  transition: background-color 0.2s ease;
}

#settings-modal li:hover {
  background-color: #e8e8e8;
  transition: all 0.3s ease;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  backdrop-filter: blur(2px);
  z-index: 999;
  display: none;
}

/* ============ Toggle ============ */

.toggle {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
  cursor: pointer;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: 0.3s;
  border-radius: 20px;
}

.toggle-slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
}

.toggle input:checked + .toggle-slider {
  background-color: #2196F3;
}

.toggle input:checked + .toggle-slider:before {
  transform: translateX(20px);
}

/* ============ Ads ============ */

.ad {
  background: #f1f1f1;
}

#leftAd,
#rightAd {
  display: none;
  flex-direction: column;
  gap: 20px;
  align-self: stretch;
  flex: 0 0 auto;
}

#leftAd {
  justify-content: center;
  align-items: flex-start;
}

#rightAd {
  justify-content: center;
  align-items: flex-end;
}

#footer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  align-self: stretch;
  margin-top: auto;
}

#footer .ad {
  width: 320px;
  height: 60px;
}

/* ============ Breakpoints ============ */

@media (min-width: 768px) {
  main {
    padding: 20px;
  }

  #footer {
    background: none;
    border: none;
  }

  #footer .ad {
    width: 468px;
    height: 60px;
  }
}

@media (min-width: 1024px) and (max-height: 800px) {
  .mobile-header header {
    display: flex;
    width: clamp(320px, calc(100vw - 40px), 500px);
    border-radius: 0 0 8px 8px;
  }

  #leftAd,
  #rightAd {
    display: flex;
  }

  #leftAd .ad,
  #rightAd .ad {
    width: 160px;
    height: 600px;
  }

  #footer .ad {
    width: 728px;
    height: 90px;
  }
}

@media (min-width: 1128px) {
  #leftAd,
  #rightAd {
    display: flex;
  }

  #leftAd .ad,
  #rightAd .ad {
    width: 160px;
    height: 600px;
  }

  #footer .ad {
    width: 728px;
    height: 90px;
  }
}

@media (min-width: 1650px) {
  #leftAd .ad,
  #rightAd .ad {
    width: 300px;
    height: 600px;
  }

  #footer .ad {
    width: 970px;
    height: 250px;
  }
}

/* ============ Dark Mode ============ */

@media (prefers-color-scheme: dark) {
  body {
    background: #1e1f24;
  }

  #logo {
    color: #ffffff !important;
  }

  header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
    color: #ffffff;
  }

  #settings:hover,
  #install-app:hover,
  #move-counter:hover {
    background-color: rgba(255, 255, 255, 0.1);
  }

  .toggle-slider {
    background-color: #ccc;
  }

  #settings-modal {
    background-color: #fff;
  }

  #settings-modal ul {
    background-color: #f1f1f1;
  }

  #settings-modal li {
    border-bottom: 1px solid #e3e3e3;
  }

  #settings-modal li:hover {
    background-color: #e8e8e8;
    transition: all 0.3s ease;
  }

  #board {
    background-color: rgba(255, 255, 255, 0.95);
    border: 1px solid #ffffff;
  }
}
