:root {
  --bg: #f7f9fc;
  --panel: #ffffff;
  --text: #111827;
  --muted: #667085;
  --accent: #4f8cff;
  --accent-2: #50e3c2;
  --warning: #ffb020;
  --danger: #ff5a6b;
  --gap: 10px;
  --board-size: min(82vmin, 420px);
  --cell-radius: 8px;
  --cell-border: 2px;
  --shadow-sm: 0 6px 16px rgba(0,0,0,.12);
  --shadow-md: 0 10px 30px rgba(0,0,0,.18);
  --game-gap: 16px;
  --title-center-offset: 0px;
  --controls-center-offset: 0px;
  --tagline-size: 16px;
  --tagline-color: var(--muted);
  --tagline-offset-x: 0px;
  --tagline-offset-y: -4px;
  --main-btn-height: 34px;
  --logo-buttons-gap: 36px;
  --piece-percent-desktop: 0.081;
  --piece-percent-mobile: 0.081;
  /* NEW: frame control for piece containers (pools) */
  --pool-frame-shadow: none;
  --pool-frame-shadow-over: none;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text);
}
#app { min-height: 100%; display: grid; }
.screen { display: none; min-height: 100%; }
.screen.active { display: grid; }
#start-screen .center {
  margin: auto;
  text-align: center;
  padding: 24px;
}
#start-screen h1 { font-size: clamp(28px, 4vw, 48px); margin: 0 0 8px; }
.subtitle { color: var(--muted); margin: 6px 0 0; }
#howto-screen { grid-template-rows: auto 1fr; }
.screen-header { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; padding: 12px 16px; }
.screen-header .left { justify-self: start; }
.screen-header .center { justify-self: center; }
.screen-header .right { justify-self: end; }
.title { margin: 0; font-size: clamp(18px, 2.2vw, 28px); color: var(--muted); margin-inline-end: var(--title-center-offset); display: inline-grid; width: max-content; justify-items: center; }
#howto-screen .content { max-width: 880px; margin: 0 auto; padding: 12px 20px 40px; }
.rules { line-height: 1.5; color: var(--muted); }
.rules li { margin: 10px 0; }
.top-bar { display: grid; justify-content: center; row-gap: 8px; padding: 12px 16px 0; }
.header-top { display: grid; grid-auto-flow: column; align-items: center; gap: var(--logo-buttons-gap, 16px); justify-content: center; }
.top-controls { display: grid; grid-auto-flow: column; gap: 8px; margin-inline-start: var(--controls-center-offset); }
.top-bar .subtitle { text-align: center; }
.top-controls .btn { background: #ffffff; color: var(--text); min-height: var(--main-btn-height, 44px); border: 1px solid rgba(0,0,0,0.08); }
.title .tagline { display: block; font-weight: 700; font-size: var(--tagline-size); color: var(--tagline-color); margin-top: var(--tagline-offset-y); transform: translateX(var(--tagline-offset-x)); }
#game-screen { grid-template-rows: auto 1fr; }
.game-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: var(--game-gap);
  justify-content: center;
  align-items: start;
  padding: 18px 16px 24px;
}
.top-controls details.dropdown { position: relative; }
details.dropdown > summary.btn { list-style: none; cursor: pointer; }
details.dropdown > summary::-webkit-details-marker { display: none; }
details.dropdown .dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  background: #fff;
  border: 1px solid rgba(0,0,0,0.1);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  padding: 6px;
  display: grid;
  gap: 6px;
  z-index: 1000;
  width: max-content;
  white-space: nowrap;
  list-style: none;
  margin: 0;
}
details.dropdown .dropdown-menu > li { list-style: none; margin: 0; padding: 0; }
details.dropdown .dropdown-item {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
details.dropdown .dropdown-item:hover { background: rgba(0,0,0,0.04); }
.board-wrap { position: relative; display: grid; place-items: center; }
.board {
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: var(--gap);
  background: linear-gradient(180deg, #ffffff, #f5f7fb);
  padding: var(--gap);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  position: relative;
  z-index: 1;
}
.cell {
  position: relative;
  border-radius: var(--cell-radius);
  background: rgba(0,0,0,0.03);
  outline: var(--cell-border) solid rgba(0,0,0,0.08);
  overflow: hidden;
  aspect-ratio: 1 / 1;
}
.cell.dropzone { transition: outline-color .2s, background-color .2s; }
.cell.dropzone.over { outline-color: rgba(0,0,0,0.1); background-color: rgba(0,0,0,0.04); }
.pieces-layer {
  position: absolute;
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: var(--gap);
  padding: var(--gap);
  pointer-events: none;
  z-index: 2;
}
.piece {
  pointer-events: auto;
  user-select: none;
  -webkit-user-drag: element;
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), 1fr);
  grid-template-rows: repeat(var(--rows, 1), 1fr);
  border-radius: 0;
  color: #111315;
  font-weight: 900;
  letter-spacing: 1px;
  text-shadow: 0 1px 0 rgba(255,255,255,.4);
  cursor: grab;
  box-shadow: none;
  transition: transform .12s ease-out, box-shadow .12s ease-out, opacity .12s ease-out;
  overflow: visible;
  background: transparent;
}
.piece:active { cursor: grabbing; transform: translateY(1px); }
.piece.preview {
  pointer-events: none;
  opacity: 0.55;
  box-shadow: none;
}
.pool .piece { background: transparent; overflow: visible; }
.piece-cell { display: grid; place-items: center; border-radius: var(--cell-radius); }
.piece-cell .char { font-size: clamp(16px, 3.6vmin, 28px); }
.piece-hole { background: transparent; }
/* Always-visible frame for piece containers (both desktop and mobile) */
.pool {
  border-radius: 12px;
  box-shadow: var(--pool-frame-shadow, inset 0 0 0 2px rgba(0,0,0,0.12));
}
@media (min-width: 961px) {
  #pool-left, #pool {
    --pool-gap: 6px;
    --pool-cell: calc(var(--board-size) * var(--piece-percent-desktop, 0.081));
    --pool-min-cols-desktop: 5;

    height: var(--board-size);
    display: grid;
    grid-auto-flow: column;
    grid-auto-rows: var(--pool-cell);
    grid-auto-columns: var(--pool-cell);
    grid-template-rows: repeat(auto-fill, var(--pool-cell));
    gap: var(--pool-gap);
    width: max-content;
    min-width: calc(
      var(--pool-cell) * var(--pool-min-cols-desktop)
      + var(--pool-gap) * (var(--pool-min-cols-desktop) - 1)
    );
    align-content: start;
    overflow: visible;
  }
  #pool-left { justify-self: end; }
  #pool { justify-self: start; }
}
/* Align left pool content to the right on desktop to be closer to the board */
@media (min-width: 961px) {
  #pool-left { justify-content: end; }
}
/* Highlight frame on drag-over */
.pool.dropzone.over { box-shadow: var(--pool-frame-shadow-over, inset 0 0 0 2px rgba(79,140,255,0.6)); }
.pool .piece {
  width: 100%;
  height: 100%;
  grid-column: span var(--cols);
  grid-row: span var(--rows);
}
.pool .piece-cell .char { font-size: 16px; }
.side { display: grid; align-content: start; gap: 12px; }
.controls { display: grid; gap: 8px; background: var(--panel); padding: 12px; border-radius: 10px; box-shadow: var(--shadow-sm); border: 1px solid rgba(0,0,0,0.06); }
.pool-title { margin: 0 0 6px 0; color: var(--muted); font-weight: 600; }
.btn { appearance: none; border: 1px solid rgba(0,0,0,0.1); border-radius: 10px; padding: 10px 14px; color: var(--text); background: #ffffff; cursor: pointer; font-weight: 700; box-shadow: var(--shadow-sm); }
.btn:hover { filter: brightness(0.98); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, var(--accent), #2d69ff); color: white; border-color: transparent; }
.btn.ghost { background: transparent; border: 1px solid rgba(0,0,0,0.2); color: var(--text); }
.btn-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.modal { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .15s; z-index: 9999; }
.modal.show { opacity: 1; pointer-events: auto; }
.modal-card { background: #ffffff; border: 1px solid rgba(0,0,0,0.12); border-radius: 14px; padding: 20px; min-width: 260px; max-width: 600px; max-height: 80vh; overflow: auto; box-shadow: var(--shadow-md); }
.modal-card h2 { margin: 0 0 6px; }
.modal-card p { color: var(--muted); margin: 0 0 16px; }
.modal-actions { margin-top: 12px; text-align: right; }
.share-panel { display: grid; gap: 14px; }
.share-preview-wrap { display: grid; place-items: center; }
.pattern-preview {
  --cell: 30px;
  --gap: 4px;
  display: grid;
  grid-template-columns: repeat(5, var(--cell));
  grid-template-rows: repeat(5, var(--cell));
  gap: var(--gap);
  padding: var(--gap);
  background: #ffffff;
  border-radius: 12px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}
.pattern-preview .sq { width: var(--cell); height: var(--cell); border-radius: 6px; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.06); }
.actions.share-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.share-row { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 10px; }
.share-label { color: var(--text); font-weight: 700; }
.share-buttons {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 12px;
    }
    .share-buttons a {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 48px;
      height: 48px;
      border-radius: 50%;
      color: #fff;
      text-decoration: none;
      font-size: 22px;
      transition: opacity 0.2s;
    }
    .share-buttons a:hover {
      opacity: 0.8;
    }
    .facebook   { background: #3b5998; }
    .twitter    { background: #000000; }
    .linkedin   { background: #0077b5; }
    .pinterest  { background: #bd081c; }
    .reddit     { background: #ff4500; }
    .whatsapp   { background: #25d366; }
    .telegram   { background: #0088cc; }
    .email      { background: #666666; }
@media (max-width: 960px) {
  .game-wrap { grid-template-columns: 1fr; }
  #pool-left { display: none; }
  .pattern-preview { --cell: 24px; --gap: 5px; }
  #pool.pool {
    --pool-gap: 6px;
    --pool-ref-size: var(--board-mobile-size, var(--board-size));
    --pool-cell: calc(var(--pool-ref-size) * var(--piece-percent-mobile, var(--piece-percent-desktop, 0.081)));

    width: var(--pool-ref-size);
    max-width: var(--pool-ref-size);
    display: grid;
    justify-content: start;
    grid-auto-flow: dense;

    grid-template-columns: repeat(var(--pool-cols-mobile, 10), var(--pool-cell));
    grid-auto-columns: var(--pool-cell);
    grid-auto-rows: var(--pool-cell);
    gap: var(--pool-gap);
  }
}
@media (max-width: 600px) {
  :root { --board-mobile-margin: 12px; }
  .game-wrap { grid-template-columns: 1fr; padding: 12px var(--board-mobile-margin) 16px; gap: 12px; }
  .board-wrap { width: 100%; }
  .board,
  .pieces-layer {
    width: calc(100vw - 2 * var(--board-mobile-margin));
    height: calc(100vw - 2 * var(--board-mobile-margin));
  }
  .pool { justify-content: center; }
}
@media (max-width: 600px) {
  :root {
    --board-mobile-margin: 18px;
    --board-mobile-size: calc(100vw - 2 * var(--board-mobile-margin));
  }

  .game-wrap {
    grid-template-columns: 1fr;
    justify-items: center;
    align-items: start;
    padding: 12px var(--board-mobile-margin) 16px;
    gap: 12px;
  }

  .board-wrap {
    width: var(--board-mobile-size);
    margin-bottom: 12px;
  }

  .board,
  .pieces-layer {
    width: var(--board-mobile-size);
    height: var(--board-mobile-size);
  }

  #pool.pool {
    --pool-cols-mobile: 10;
    --pool-gap: 6px;

    width: var(--board-mobile-size);
    margin: 0;
    display: grid;
    justify-content: start;
    grid-auto-flow: dense;

    --pool-cell: calc(var(--board-mobile-size) * var(--piece-percent-mobile, var(--piece-percent-desktop, 0.081)));

    grid-template-columns: repeat(var(--pool-cols-mobile), var(--pool-cell));
    grid-auto-columns: var(--pool-cell);
    grid-auto-rows: var(--pool-cell);
    gap: var(--pool-gap);

    max-width: var(--board-mobile-size);
    overflow-x: clip;
  }

  .pool .piece-cell .char { font-size: 14px; }
}
@media (max-width: 600px) {
  :root {
    --pool-under-gap: 6px;
  }

  .game-wrap {
    grid-template-columns: 1fr;
    row-gap: var(--pool-under-gap);
    column-gap: var(--game-gap, 16px);
    padding: 12px var(--board-mobile-margin, 12px) 16px;
    justify-items: center;
    align-items: start;
  }

  .board-wrap { margin-bottom: 0; }

  #pool.pool {
    width: var(--board-mobile-size);
    max-width: var(--board-mobile-size);
  }
}
@media (max-width: 600px) {
  :root{
    --pool-under-gap: 0px;
    --pool-under-nudge: 0px;
  }

  .game-wrap{
    grid-template-columns: 1fr !important;
    row-gap: var(--pool-under-gap) !important;
    column-gap: var(--game-gap, 16px);
    padding-bottom: 0 !important;
  }
  .board-wrap{
    margin-bottom: 0 !important;
    padding-bottom: 0 !important;
  }
  #pool.pool{
    margin-top: 0 !important;
    padding-top: 0 !important;
    transform: translateY(var(--pool-under-nudge));
  }

  .board,
  .pieces-layer{
    margin: 0 !important;
  }

  .top-controls details.dropdown { flex: 1 1 0; }
  .top-controls details.dropdown > summary.btn { width: 100%; }
}
html { -webkit-text-size-adjust: 100%; }

button, .btn, a, .top-controls { touch-action: manipulation; }

button, .btn { font-size: 16px; line-height: 1.2; }
html { -webkit-text-size-adjust: 100%; }

button, .btn, a, .top-controls { touch-action: manipulation; }

@media (max-width: 380px) {
  .top-bar { padding-left: 12px; padding-right: 12px; }
  .header-top { 
    grid-auto-flow: row;
    justify-items: center;
    row-gap: 8px;
    max-width: 100%;
  }

  .title {
    width: 100%;
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .top-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    max-width: 100%;
  }
  .top-controls .btn {
    font-size: 16px;
    flex: 1 1 auto;
    min-width: 120px;
  }

  html, body { overflow-x: hidden; }
}
@media (max-width: 600px) {
  .header-top {
    display: grid;
    grid-auto-flow: row;
    justify-items: center;
    row-gap: 10px;
  }

  .title { text-align: center; }

  .top-controls {
    width: var(--board-mobile-size);
    max-width: var(--board-mobile-size);
    margin: 0 auto;

    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: stretch;
    gap: 8px;
  }

  .top-controls .btn {
    flex: 1 1 0;
    min-width: 0;
    height: var(--main-btn-height, 34px);
    line-height: 1.2;
    font-size: 16px;
  }
}
@media (max-width: 600px) {
  .top-controls .btn {
    display: flex;
    align-items: center;
    justify-content: center;

    padding: 0 8px;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;

    line-height: 1.05;
    font-size: clamp(12px, 3.2vw, 16px);
  }
}

@media (max-width: 360px) {
  .top-controls .btn {
    font-size: clamp(11px, 3.6vw, 15px);
  }
}

@media (max-width: 600px) {
  :root { --modal-mobile-width: var(--board-mobile-size); }
  .modal-card {
    min-width: 0;
    width: var(--modal-mobile-width);
    max-width: var(--modal-mobile-width);
    box-sizing: border-box;
  }
}

/* Mobile: distance between board and piece pool as a percent of board size */
:root {
  /* Value is a number in percent (%). Example: 3 means 3% of board height */
  --mobile-pool-distance-percent: 3;
}
@media (max-width: 960px) {
  /* Ensure layout uses margin-top for the spacing control */
  .game-wrap { row-gap: 0 !important; align-content: start !important; }
  .board-wrap { margin-bottom: 0 !important; }
  #pool.pool {
    margin-top: calc(
      var(--board-mobile-size, var(--board-size)) * var(--mobile-pool-distance-percent, 3) / 100
    ) !important;
  }
}
