:root {
  --bg: #ffffff;
  --text: #162018;
  --muted: #55665b;
  --card: rgba(255, 255, 255, 0.9);
  --card-border: #d6e0d2;
  --shadow: 0 20px 55px rgba(25, 56, 31, 0.16);
  --accent: #2f9f57;
  --accent-strong: #1f7b41;
  --accent-soft: #d8f1de;
  --cell-bg: #edf5ea;
  --cell-border: #cfdbcf;
  --warn: #c74444;
}

:root[data-theme="dark"] {
  --bg: #101812;
  --text: #e8f2e7;
  --muted: #afc0b0;
  --card: rgba(25, 37, 29, 0.86);
  --card-border: #2f4436;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  --accent: #5fd88a;
  --accent-strong: #45bd73;
  --accent-soft: #1c3527;
  --cell-bg: #17251d;
  --cell-border: #36513f;
  --warn: #f07070;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: "Avenir Next", "Trebuchet MS", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  overflow-x: hidden;
}

.page-bg {
  display: none;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 1240px;
  margin: 0 auto;
  padding: 24px clamp(16px, 2.4vw, 32px) 36px;
  display: grid;
  gap: 18px;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 22px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 18px 22px;
}

.brand-kicker {
  margin: 0;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.brand h1 {
  margin: 6px 0 0;
  font-size: clamp(1.7rem, 2.7vw, 2.5rem);
  line-height: 1.05;
  font-family: "Futura", "Avenir Next", "Trebuchet MS", sans-serif;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.icon-btn {
  min-height: 42px;
  border: 1px solid var(--card-border);
  background: rgba(255, 255, 255, 0.4);
  color: var(--text);
  border-radius: 12px;
  padding: 0 14px;
  font-size: 0.93rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.2s ease, border-color 0.2s ease;
}

.icon-btn:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.settings {
  padding: 20px 22px;
  display: grid;
  gap: 18px;
}

.settings-row {
  display: grid;
  gap: 12px;
}

.settings-row h2 {
  margin: 0;
  font-size: 1.08rem;
  letter-spacing: 0.01em;
}

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

.seg-btn {
  border: 1px solid var(--card-border);
  border-radius: 12px;
  min-height: 52px;
  background: transparent;
  color: var(--text);
  font-size: 1.03rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
}

.seg-btn.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.settings-inline {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
}

.check-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 1.02rem;
  color: var(--text);
}

.check-row input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.pieces-info {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.ops-group {
  display: flex;
  gap: 12px;
}

.ops-line {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.op-btn {
  width: 62px;
  height: 62px;
  border-radius: 999px;
  border: 2px solid var(--card-border);
  background: transparent;
  color: var(--text);
  font-size: 2rem;
  font-weight: 800;
  cursor: pointer;
  transition: all 0.2s ease;
}

.op-btn.is-active {
  background: var(--accent);
  border-color: var(--accent-strong);
  color: #fff;
  box-shadow: 0 8px 20px rgba(34, 122, 65, 0.26);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(280px, 1fr);
  gap: 18px;
  align-items: start;
}

.board {
  padding: 16px;
}

.board-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.board-head h2,
.task h2 {
  margin: 0;
  font-size: 1.2rem;
}

#progressText {
  margin: 0;
  font-weight: 700;
  color: var(--muted);
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
}

.board-cell {
  position: relative;
  border: 1px solid var(--cell-border);
  background: var(--cell-bg);
  border-radius: 0;
  aspect-ratio: 3 / 2;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.5rem, 4.2vw, 3rem);
  font-weight: 800;
  color: var(--text);
  overflow: hidden;
  transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.board-cell.solved {
  color: transparent;
  border-color: transparent;
  background-repeat: no-repeat;
}

.board-cell.solved::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.15), rgba(0, 0, 0, 0.03));
  pointer-events: none;
}

.board-cell.drop-match {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 159, 87, 0.26);
  transform: translateY(-2px);
}

.board-cell.drop-miss {
  border-color: var(--warn);
  box-shadow: 0 0 0 3px rgba(199, 68, 68, 0.24);
}

.board-cell.shake {
  animation: shake 0.25s linear;
}

.task {
  padding: 18px;
  display: grid;
  gap: 14px;
  align-content: start;
}

.status {
  margin: 0;
  color: var(--muted);
  min-height: 1.2em;
}

.equation-shell {
  border-radius: 16px;
  border: 1px solid var(--card-border);
  background: linear-gradient(140deg, rgba(255, 255, 255, 0.58), rgba(47, 159, 87, 0.1));
  padding: 18px;
}

.equation {
  margin: 0;
  text-align: center;
  font-size: clamp(2rem, 6vw, 4rem);
  font-weight: 900;
  letter-spacing: 0.02em;
}

.piece-shell {
  border: 1px solid var(--card-border);
  border-radius: 16px;
  background: linear-gradient(170deg, rgba(47, 159, 87, 0.12), rgba(255, 255, 255, 0.4));
  padding: 12px;
}

.piece-preview {
  width: 100%;
  max-width: 320px;
  margin: 0 auto;
  aspect-ratio: 3 / 2;
  border-radius: 12px;
  border: 1px solid var(--cell-border);
  cursor: grab;
  background-repeat: no-repeat;
  background-color: var(--accent-soft);
  background-size: 300% 300%;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.piece-preview:active {
  cursor: grabbing;
}

.piece-preview.is-disabled {
  cursor: not-allowed;
  background-image: repeating-linear-gradient(
    -45deg,
    rgba(120, 133, 120, 0.3),
    rgba(120, 133, 120, 0.3) 12px,
    transparent 12px,
    transparent 24px
  );
}

.hint {
  margin: 0;
  font-size: 0.92rem;
  color: var(--muted);
}

.actions-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.btn {
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
}

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

.btn.secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--card-border);
}

.btn.start-inline {
  min-height: 62px;
  min-width: 160px;
}

.btn:hover,
.seg-btn:hover,
.op-btn:hover {
  transform: translateY(-1px);
}

.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  border-radius: 12px;
  border: 1px solid var(--accent);
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.28);
  background-repeat: no-repeat;
  background-size: 300% 300%;
  transform: translate(-50%, -50%);
}

body.dragging .piece-preview {
  opacity: 0.45;
}

@keyframes shake {
  0% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
  100% { transform: translateX(0); }
}

@media (max-width: 1024px) {
  .game-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .app {
    padding: 14px;
  }

  .app-header {
    padding: 14px;
  }

  .header-actions {
    width: 100%;
  }

  .icon-btn {
    flex: 1;
  }

  .settings,
  .task,
  .board {
    padding: 14px;
  }

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

  .actions-row {
    grid-template-columns: 1fr;
  }

  .btn.start-inline {
    width: 100%;
  }
}
