@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Lora:wght@400;500&display=swap');

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

:root {
  --cell-size: 32px;      
  --tool-size: max(28px, var(--cell-size));
  --bar-h: 22px;        
  --bar-gap: 6px;
  --piece-cell: 18px;    
}

html {
  background-image: url('/background/background.png');
  background-repeat: repeat;
  background-size: 1600px;
  min-height: 100%;
}

body {
  margin: 0;
  padding: 40px 20px;
  min-height: 100vh;
  font-family: 'Lora', Georgia, serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #fff;
}

main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  width: 100%;
}

.puzzle-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
}

.puzzle-nav {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 18px;
  font-size: 14px;
}

.puzzle-nav a:first-child { justify-self: end; }
.puzzle-nav a:last-child  { justify-self: start; }

.puzzle-nav a { color: #fff; opacity: 0.8; text-decoration: none; }
.puzzle-nav a:hover { opacity: 1; text-decoration: underline; }
.puzzle-header h1 { font-family: 'Playfair Display', serif; font-size: 28px; margin: 0; }

.puzzle-meta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  font-size: 14px;
}
#puzzle-difficulty { display: flex; opacity: 0.9; }

#grid {
  display: grid;
  gap: 1px;
  background: rgba(0,0,0,0.08);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: #fff;
  border: 0px solid black;
}

#grid.flooding .cell { transition: background 130ms ease-out; }

#tray {
  display: flex;
  gap: 24px;
  align-items: flex-end;
  justify-content: center;
  flex-wrap: wrap;
  user-select: none;
  -webkit-user-select: none;
}
.piece { display: flex; flex-direction: column; align-items: center; gap: 6px; cursor: pointer; }
.piece.used   { opacity: 0.2; pointer-events: none; }
.piece.active { outline: 2px solid rgba(0,0,0,0.3); outline-offset: 4px; }
.piece-grid { display: grid; gap: 1px; }
.pc { width: var(--piece-cell); height: var(--piece-cell); }

.board-wrapper {
  position: relative;
  display: inline-block;
}

.board-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--bar-h);
  margin-bottom: var(--bar-gap);
}

#area-counter {
  font-family: 'Playfair Display', serif;
  font-size: 17px;
  line-height: 1;
  color: #fff;
  transition: color 260ms ease-out;
}

/* at or past the best known score */
#area-counter.record { color: #77DFA0; }
#area-counter.pop    { animation: score-pop 620ms ease-out; }

@keyframes score-pop {
  0%   { transform: scale(1);    }
  28%  { transform: scale(1.28); }
  60%  { transform: scale(0.98); }
  100% { transform: scale(1);    }
}

.board-tools {
  position: absolute;
  top: calc(var(--bar-h) + var(--bar-gap));   /* level with the top of the grid */
  left: calc(100% + 1px);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.board-tools button {
  height: var(--tool-size);
  width:  var(--tool-size);
  font-size: calc(var(--tool-size) * 0.56);
  line-height: 1;
  color: #6b573d;
  background: #f6eee3;
  border: 0;
  border-radius: 0;
  padding: 0;
  cursor: pointer;
}
.board-tools button:hover  { background: #efe4d4; }
.board-tools button:active { background: #e6d9c6; }
#help-button { font-family: 'Playfair Display', serif; font-weight: 700; }

#share-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: calc(-1 * var(--tool-size) - 1px);
  z-index: 20;
  width: 300px;
  max-width: 90vw;
  padding: 12px;
  background: #f6eee3;
  color: #333;
  outline: 2px solid #8e7456;
  outline-offset: 0.5px;
  font-size: 13px;
}
#share-popover[hidden] { display: none; }

.share-title { font-family: 'Playfair Display', serif; font-size: 15px; margin-bottom: 8px; }

#help-popover {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 20;
  width: 336px;
  max-width: 92vw;
  padding: 12px;
  background: #f6eee3;
  color: #333;
  outline: 2px solid #8e7456;
  outline-offset: 0.5px;
  font-size: 12px;
}
#help-popover[hidden] { display: none; }

.how-lede { margin: 0 0 10px; font-size: 13px; }
.how-foot { margin: 0 0 10px; opacity: 0.75; }
.how-figs { display: flex; gap: 10px; justify-content: space-between; margin-bottom: 12px; }
.how-fig  { text-align: center; }
.how-grid { display: grid; gap: 1px; margin: 0 auto 6px; --how-cell: 12px; }
.how-cell { width: var(--how-cell); height: var(--how-cell); background: #fffaf2; }
.how-cap  { font-size: 11px; line-height: 1.35; white-space: nowrap; }
.how-cap b { font-family: 'Playfair Display', serif; font-size: 15px; }

.how-keys {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 5px 12px;
  margin-bottom: 10px;
}
.how-keys span { display: inline-flex; align-items: center; gap: 4px; }
.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  background: #fdf8f0;
  outline: 1.5px solid #8e7456;
  font-size: 13px;
  line-height: 1;
}

.how-actions { display: flex; gap: 8px; justify-content: flex-end; }
.how-actions button {
  padding: 6px 13px;
  font-family: inherit;
  font-size: 12px;
  color: #f6eee3;
  background: #8e7456;
  border: 0;
  cursor: pointer;
}
.how-actions button:hover { background: #7a6248; }

.share-row { display: flex; gap: 6px; }

#share-link {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 12px;
  color: #333;
  background: #fff;
  border: 1px solid #cbb79b;
}

#share-copy {
  padding: 6px 12px;
  font-family: inherit;
  font-size: 12px;
  color: #f6eee3;
  background: #8e7456;
  border: 0;
  cursor: pointer;
}
#share-copy:hover { background: #7a6248; }

#share-status { min-height: 16px; margin-top: 6px; font-size: 12px; opacity: 0.75; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  z-index: 50;
  max-width: 420px;
  padding: 10px 16px;
  background: #f6eee3;
  color: #333;
  outline: 2px solid #8e7456;
  font-size: 13px;
  text-align: center;
}

#piece-controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: #2b3a33;
  user-select: none;
  -webkit-user-select: none;
}
#piece-controls[hidden] { display: none; }

#piece-controls button {
  flex: 1 1 0;
  height: 50px;
  font-family: 'Lora', Georgia, serif;
  font-size: 15px;
  line-height: 1;
  color: #4a3b28;
  background: #f6eee3;
  border: 0;
  outline: 2px solid #8e7456;
  outline-offset: -2px;
  cursor: pointer;
  touch-action: manipulation;
}
#piece-controls button:active { background: #e6d9c6; }
#piece-controls button.glyph { font-size: 23px; }

#piece-controls #ctl-cancel {
  flex: 0 1 78px;
  color: rgba(246, 238, 227, 0.8);
  background: transparent;
  outline: 0;
}
#piece-controls #ctl-cancel:active { background: rgba(246, 238, 227, 0.12); }

#piece-controls #ctl-place {
  flex: 1.5 1 0;
  color: #f6eee3;
  background: #8e7456;
}
#piece-controls #ctl-place:active { background: #7a6248; }

@media (max-width: 720px) {
  body { padding: 12px 10px; }
  main { gap: 10px; }

  :root { --tool-size: 36px; --piece-cell: 11px; }

  .puzzle-header { gap: 3px; }
  .puzzle-header h1 { font-size: 21px; }
  .puzzle-nav { gap: 12px; font-size: 13px; }

  .board-bar { height: auto; }
  .board-tools { position: static; flex-direction: row; gap: 6px; }

  #tray { gap: 10px; }

  #area-counter { font-size: 15px; }

  #share-popover { left: 0; right: 0; width: auto; max-width: none; }
  #help-popover  { width: calc(100vw - 20px); }
}

.index-title { font-family: 'Playfair Display', serif; font-size: 42px; margin: 0; }
.index-note  { margin: -10px 0 0; font-size: 14px; opacity: 0.75; }

#puzzle-list {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 14px;
  width: 100%;
  max-width: 1120px;
}

@media (max-width: 1060px) {
  #puzzle-list { grid-template-columns: repeat(3, minmax(0, 1fr)); max-width: 680px; }
}
@media (max-width: 700px) {
  #puzzle-list { grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: 460px; }
}
.card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
  height: 126px;
  padding: 14px;
  background: #f6eee3;
  color: #333;
  text-decoration: none;
  transition: background 0.3s ease, color 0.3s ease;
  outline: 2px solid #8e7456;
  outline-offset: 0.5px;
}
.card:hover {
  background: rgba(255,255,255,0.2);
  color: #f6eee3;
}

.card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card h2 {
  font-family: 'Playfair Display', serif;
  font-size: 17px;
  line-height: 1.2;
  margin: 0;
  min-width: 0;
}

.card-badge { flex: none; padding-top: 2px; }

.card-preview {
  display: flex;
  flex-wrap: wrap;
  column-gap: 8px;
  row-gap: 4px;
  align-items: flex-end;
  align-content: flex-end;
  height: 48px;
  overflow: hidden;
}

.mini-grid { display: grid; gap: 1px; }
.mini-cell { width: 8px; height: 8px; }

#puzzle-difficulty .mini-grid { display: inline-grid; }