/* Page: green table on every width; the clip grows with the game instead of keeping 4:3,
   and "Spiel vergrößern" enlarges board and card instead of zooming the whole game. */

/* Registered, so that the width of the card can change smoothly: card and board are measured from it and grow
   together, frame by frame. */
@property --kak-card {
  syntax: '<length>';
  inherits: true;
  initial-value: 500px;
}

.page-kakuro .game-section {
  background: #005000;
}

.page-kakuro .game-clip {
  --kak-card: 500px;
  aspect-ratio: auto;
  transition: width 0.3s ease, --kak-card 0.3s ease;
}

.page-kakuro .game-clip--large {
  --kak-card: 640px;
}

.page-kakuro .game-clip--large .game-inner {
  zoom: 1;
}

@media (max-width: 850px) {
  .page-kakuro .game-section {
    padding: 20px 5px;
  }
}

/* Game */

#kak-app {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

/* The board is a square as wide as the card allows and as high as the window allows (--kak-chrome is
   everything else on the screen), never under 240px. It is the same for every level, so that switching the
   level moves nothing; --kak-n (cells per side, set by the script) divides it. */
#kak-game {
  --kak-pad: 16px;
  --kak-chrome: 270px;
  --kak-w: min(var(--kak-card), calc(100vw - 20px));
  --kak-board: max(240px, min(calc(var(--kak-w) - 2 * var(--kak-pad)), calc(100vh - var(--kak-chrome))));
  --kak-cell: calc((var(--kak-board) - (var(--kak-n, 9) + 1) * 1px) / var(--kak-n, 9));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "levels new" "board board" "combo combo" "pad pad" "tools tools" "status status";
  justify-items: center;
  gap: 10px 8px;
  box-sizing: border-box;
  width: var(--kak-w);
  padding: var(--kak-pad);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  color: #1f2d1f;
  font-family: 'Inter', system-ui, sans-serif;
  text-transform: uppercase;
  user-select: none;
}

/* Buttons do not inherit the capitals on their own (the browser sets text-transform: none on them). */
#kak-game button {
  text-transform: inherit;
}

/* The levels as one row of buttons; the one in play is lit (as the levels of Tic Tac Toe). */
#kak-levels {
  grid-area: levels;
  justify-self: stretch;
  display: flex;
  gap: 2px;
  padding: 3px;
  background: #edf1ed;
  border: 1px solid #d6ded6;
  border-radius: 8px;
}

#kak-levels button {
  flex: 1 1 0;
  min-width: 0;
  padding: 5px 6px;
  font: 600 13px 'Inter', system-ui, sans-serif;
  color: #556355;
  background: transparent;
  border: 0;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#kak-levels button.kak-on {
  color: #005000;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  cursor: default;
}

/* The buttons of the other games (Games/shared/ui.css) as on the Sokoban page: light green with a green glow; under
   the mouse white with green letters and a thin green ring, no glow, because the card is white. */
#kak-new,
#kak-check,
#kak-tip {
  padding: 10px 14px;
  font: 600 14px 'Inter', system-ui, sans-serif;
  color: #fff;
  background: #00b050;
  border: 0;
  border-radius: 8px;
  box-shadow: 0 0 8px rgba(0, 176, 80, 0.8);
  cursor: pointer;
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}

#kak-new {
  grid-area: new;
  align-self: stretch;
}

/* the question: red with a red glow */
#kak-new.kak-ask {
  background: #e5534b;
  box-shadow: 0 0 8px rgba(229, 83, 75, 0.8);
}

/* The grid: black cells with their sums, white cells for the digits. The 1px gaps are the lines. */
#kak-board {
  grid-area: board;
  display: grid;
  grid-template-columns: repeat(var(--kak-n, 9), var(--kak-cell));
  grid-template-rows: repeat(var(--kak-n, 9), var(--kak-cell));
  gap: 1px;
  box-sizing: border-box;
  width: var(--kak-board);
  height: var(--kak-board);
  padding: 1px;
  align-content: center;
  justify-content: center;
  background: #6f806f;
  border-radius: 4px;
  outline: none;
}

.kak-block {
  position: relative;
  background: #2b392b;
}

/* A sum cell: a diagonal from the top left to the bottom right, the across sum above it, the down sum below. */
.kak-clue {
  background: linear-gradient(to top right, #2b392b calc(50% - 0.7px), #8fa08f 50%, #2b392b calc(50% + 0.7px));
}

.kak-ca,
.kak-cd {
  position: absolute;
  font-size: calc(var(--kak-cell) * 0.31);
  font-weight: 600;
  line-height: 1;
  color: #fff;
  transition: opacity 0.2s, color 0.2s;
}

.kak-ca {
  top: 9%;
  right: 9%;
}

.kak-cd {
  bottom: 9%;
  left: 9%;
}

/* A run filled in: faded when it adds up with no digit twice, red when not. */
.kak-ca.kak-ok,
.kak-cd.kak-ok {
  opacity: 0.4;
}

.kak-ca.kak-bad,
.kak-cd.kak-bad {
  color: #ff8a80;
}

.kak-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  cursor: pointer;
  transition: background 0.12s;
}

.kak-v {
  font-size: calc(var(--kak-cell) * 0.6);
  font-weight: 600;
  line-height: 1;
}

/* Pencil marks: nine small places, 1 to 9 as on a phone keypad. */
.kak-notes {
  position: absolute;
  inset: 6%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  align-items: center;
  justify-items: center;
  font-size: calc(var(--kak-cell) * 0.25);
  font-weight: 600;
  line-height: 1;
  color: #6d7c6d;
}

.kak-cell.kak-peer {
  background: #e9f3e9;
}

.kak-cell.kak-sel {
  background: #fff1a8;
  box-shadow: inset 0 0 0 2px #f2b600;
}

.kak-cell.kak-dup .kak-v {
  color: #d9443d;
}

.kak-cell.kak-wrong {
  background: #ffdcd8;
}

.kak-cell.kak-wrong .kak-v {
  color: #c62828;
}

.kak-cell.kak-given .kak-v {
  color: #1e6fd9;
}

/* Solved: the whole grid turns green and takes no more input. */
#kak-board.kak-done .kak-cell {
  background: #dff3e1;
  cursor: default;
}

#kak-board.kak-done .kak-v {
  color: #1d7f42;
}

/* The combinations of the chosen cell's two runs, one line each. */
#kak-combo {
  grid-area: combo;
  justify-self: stretch;
  min-height: 34px;
  font-size: 12px;
  line-height: 17px;
  color: #556355;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
}

#kak-combo div {
  overflow: hidden;
  text-overflow: ellipsis;
}

#kak-combo b {
  color: #1f2d1f;
  font-weight: 600;
}

/* The digits and the eraser in one row. */
#kak-pad {
  grid-area: pad;
  justify-self: stretch;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 4px;
}

#kak-pad button {
  min-width: 0;
  height: 40px;
  padding: 0;
  font: 600 18px 'Inter', system-ui, sans-serif;
  color: #1f2d1f;
  background: #e4ebe4;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}

#kak-pad svg {
  display: block;
  width: 22px;
  height: 22px;
  margin: 0 auto;
}

/* In note mode the digits are small, as they will be in the cell. */
#kak-game.kak-noting #kak-pad button[data-digit] {
  font-size: 13px;
  color: #556355;
}

#kak-tools {
  grid-area: tools;
  justify-self: stretch;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

/* A switch, not an action: white with a green frame, as high as the buttons beside it. */
#kak-notes {
  padding: 8px 10px;
  font: 600 14px 'Inter', system-ui, sans-serif;
  color: #005000;
  background: #fff;
  border: 2px solid #00b050;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

#kak-notes[aria-pressed="true"] {
  color: #fff;
  background: #005000;
  border-color: #005000;
}

#kak-game button:disabled {
  opacity: 0.4;
  box-shadow: none;
  cursor: default;
}

#kak-status {
  grid-area: status;
  min-height: 18px;
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  line-height: 18px;
  text-align: center;
}

#kak-new:focus-visible,
#kak-check:focus-visible,
#kak-tip:focus-visible,
#kak-notes:focus-visible,
#kak-levels button:focus-visible,
#kak-pad button:focus-visible,
#kak-board:focus-visible {
  outline: 3px solid #00b050;
  outline-offset: 2px;
}

/* Hover only where there is a mouse: after a tap on a phone it would stick to the button. */
@media (hover: hover) {
  #kak-new:not(.kak-ask):hover,
  #kak-check:not(:disabled):hover,
  #kak-tip:not(:disabled):hover {
    color: #00b050;
    background: #fff;
    box-shadow: inset 0 0 0 1px #00b050;
  }

  #kak-new.kak-ask:hover {
    background: #ef6a62;
  }

  /* as in the other games: while one button is under the mouse, the others lose their glow */
  #kak-game:has(#kak-new:hover, #kak-check:not(:disabled):hover, #kak-tip:not(:disabled):hover) :is(#kak-new, #kak-check, #kak-tip):not(:hover) {
    box-shadow: none;
  }

  #kak-levels button:not(.kak-on):hover {
    color: #1f2d1f;
    background: rgba(0, 0, 0, 0.05);
  }

  #kak-pad button:not(:disabled):hover {
    background: #d3dfd3;
  }

  #kak-notes[aria-pressed="false"]:not(:disabled):hover {
    background: #eef7ee;
  }

  #kak-board:not(.kak-done) .kak-cell:not(.kak-sel):hover {
    background: #f4f8f4;
  }
}

@media (prefers-reduced-motion: reduce) {
  .page-kakuro .game-clip,
  .kak-cell,
  .kak-ca,
  .kak-cd {
    transition: none;
  }
}

/* Tablets: the page header stands above the game. */
@media (min-width: 851px) and (max-width: 1024px) {
  #kak-game {
    --kak-chrome: 310px;
  }
}

/* The narrowest phones: a little less padding and smaller keys. */
@media (max-width: 360px) {
  #kak-game {
    --kak-pad: 12px;
  }

  #kak-new {
    padding: 10px;
  }

  #kak-levels button {
    font-size: 12px;
  }

  #kak-pad {
    gap: 3px;
  }

  #kak-pad button {
    font-size: 16px;
  }
}

/* Under 350px the three levels need the whole row: "Neues Rätsel" goes below the tools. */
@media (max-width: 350px) {
  #kak-game {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "levels" "board" "combo" "pad" "tools" "new" "status";
  }

  #kak-new {
    justify-self: stretch;
  }
}

/* Phones held sideways: board on the left, everything else beside it. */
@media (orientation: landscape) and (max-height: 500px) {
  #kak-game {
    --kak-w: min(760px, calc(100vw - 20px));
    --kak-board: max(200px, calc(100vh - 70px));
    grid-template-areas: "board levels" "board new" "board combo" "board pad" "board tools" "board status";
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto auto auto auto 1fr;
    align-items: start;
    column-gap: 16px;
    row-gap: 8px;
  }

  #kak-new {
    align-self: start;
    justify-self: stretch;
  }

  #kak-pad button {
    height: 34px;
  }
}
