:root {
  --tile-size: 2.5rem;
  --gap: 0.25rem;
  --page-pad: 1rem;
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, sans-serif;
  margin: 0;
  padding: var(--page-pad);
  /* The viewport meta uses viewport-fit=cover; keep content out of notches. */
  padding-left: max(var(--page-pad), env(safe-area-inset-left));
  padding-right: max(var(--page-pad), env(safe-area-inset-right));
  padding-bottom: max(var(--page-pad), env(safe-area-inset-bottom));
  background: #fafafa;
  color: #222;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.25rem 1rem;
  margin-bottom: 1rem;
}

header h1 { margin: 0; font-size: 1.5rem; }

#puzzle-picker {
  font: inherit;
  font-variant-numeric: tabular-nums;
  padding: 0.25rem 0.5rem;
  border: 1px solid #ccc;
  border-radius: 0.25rem;
  background: #fff;
  color: #333;
  cursor: pointer;
}

#row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
  padding: 0.5rem;
  background: #fff;
  border-radius: 0.5rem;
  margin-bottom: 1rem;
  min-height: calc(var(--tile-size) + 1rem);
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 0.25rem;
  font-weight: 700;
  font-size: 1rem;
  color: #fff;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.tile.placed { opacity: 0.25; cursor: default; pointer-events: none; }
#row .tile.placed { /* dimmed in row when in a slot */ }
.slot .tile.placed { opacity: 1; pointer-events: auto; cursor: grab; }

.tile.dragging { cursor: grabbing; opacity: 0.7; }

.tile.floating {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%);
  opacity: 0.85;
}

#clues { display: flex; flex-direction: column; gap: 0.5rem; }

.clue-row {
  display: grid;
  grid-template-columns: minmax(40%, 1fr) 1fr auto;
  gap: 0.75rem;
  align-items: center;
  background: #fff;
  border-radius: 0.5rem;
  padding: 0.5rem;
  min-height: calc(var(--tile-size) + 1rem);
}

.slot { position: relative; cursor: text; }
.slot:focus-within {
  outline: 2px solid #0058b8;
  outline-offset: 1px;
}
.slot .tile { position: relative; z-index: 1; }

.slot-input {
  position: absolute;
  inset: 0;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  outline: none;
  font: inherit;
  cursor: text;
}

.slot {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
  min-height: var(--tile-size);
  border-radius: 0.25rem;
  padding: 0.25rem;
  background: #f0f0f0;
}

.slot.dragover { background: #e0e8ff; }
#row.dragover { background: #e0e8ff; }

.clue-text { font-style: italic; color: #333; }

#modal[hidden], #rules-modal[hidden], #settings-modal[hidden] { display: none; }
#modal, #rules-modal, #settings-modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: grid; place-items: center;
  z-index: 100;
}
#modal .card, #rules-modal .card, #settings-modal .card {
  background: #fff;
  padding: 1.5rem 2rem;
  border-radius: 0.75rem;
  max-width: min(90vw, 30rem);
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  text-align: center;
}
#modal h2, #rules-modal h2, #settings-modal h2 { margin-top: 0; }
#modal button, #rules-modal button, #settings-modal button { margin: 0.25rem; padding: 0.5rem 1rem; font-size: 1rem; cursor: pointer; }

/* Appearance settings */
.settings-form { display: flex; flex-direction: column; gap: 0.5rem; text-align: left; margin: 0.5rem 0 1rem; }
.settings-form label { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; }
.settings-preview { display: flex; flex-wrap: wrap; gap: var(--gap); justify-content: center; margin-bottom: 0.5rem; }
.settings-preview .tile { cursor: default; }

.pos-dot {
  position: absolute;
  width: 3px; height: 3px;
  border-radius: 50%;
  background: #fff;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.pos-dot.big { width: 6px; height: 6px; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35); }
.tile.dots-mode { position: relative; }

.rules-btn {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: #fff;
  color: #0058b8;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.rules-btn:hover { background: #f0f0f0; }

footer {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid #e0e0e0;
  color: #666;
  font-size: 0.9rem;
  text-align: center;
}
footer a { color: #0058b8; }

.header-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  align-items: center;
}

.nav-link {
  color: #0058b8;
  text-decoration: none;
  font-size: 0.95rem;
}
.nav-link:hover { text-decoration: underline; }

/* Editor */
.editor-body main { max-width: 48rem; margin: 0 auto; }

#editor section { margin-bottom: 1.25rem; }
#editor h2 { font-size: 1.1rem; margin: 0 0 0.5rem; }

#editor button,
#editor input,
#editor select {
  font: inherit;
  padding: 0.35rem 0.6rem;
  border: 1px solid #ccc;
  border-radius: 0.25rem;
  background: #fff;
}
#editor button { cursor: pointer; }
#editor button:hover { background: #f0f0f0; }

.row-block { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }

.edit-row {
  display: grid;
  grid-template-columns: 12rem auto minmax(0, 1fr) auto;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.word-input { min-width: 0; }

.word-input { text-transform: uppercase; font-weight: 700; }
.clue-wrap { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.clue-input { font-style: italic; width: 100%; }
.clue-warn {
  color: #a00;
  font-size: 0.8rem;
  font-style: italic;
  display: none;
}
.clue-warn.show { display: block; }
.conflict-warn {
  color: #a00;
  font-size: 0.8rem;
  font-style: italic;
  display: none;
}
.conflict-warn.show { display: block; }
.word-input.conflict, .clue-input.conflict {
  border-color: #a00;
  background: #fff0f0;
}
.remove-btn { padding: 0.2rem 0.5rem; color: #a00; }
.rand-btn { padding: 0.2rem 0.6rem; font-size: 0.85rem; }
.rand-btn:disabled { opacity: 0.4; cursor: wait; }

.mash-display {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  padding: 0.5rem;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 0.5rem;
  margin-bottom: 0.5rem;
  min-height: calc(var(--tile-size) + 1rem);
  color: #999;
  font-style: italic;
}
.mash-tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tile-size);
  height: var(--tile-size);
  border: none;
  padding: 0;
  border-radius: 0.25rem;
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-style: normal;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.mash-tile.dragging { opacity: 0.3; cursor: grabbing; }
.mash-tile.floating {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%);
}

.copy-feedback { color: #060; font-size: 0.9rem; }

.note { color: #666; font-size: 0.9rem; }
.note code { background: #f0f0f0; padding: 0 0.25rem; border-radius: 0.2rem; }

@media (max-width: 500px) {
  :root { --tile-size: 2.25rem; --page-pad: 0.75rem; }
  header h1 { font-size: 1.35rem; }
  /* Slot on its own row; clue text and hint button share the second row. */
  .clue-row { grid-template-columns: 1fr auto; gap: 0.5rem; }
  .clue-row .slot { grid-column: 1 / -1; }
  #modal .card, #rules-modal .card, #settings-modal .card {
    padding: 1rem 1.25rem;
    max-width: calc(100vw - 1.5rem);
  }
  .edit-row { grid-template-columns: 1fr; }
  .edit-row .remove-btn { justify-self: end; }
}

@media (max-width: 380px) {
  :root { --tile-size: 2rem; --gap: 0.2rem; }
  .tile, .mash-tile { font-size: 0.9rem; }
}

.hint-btn {
  border: 1px solid #ccc;
  border-radius: 0.4rem;
  background: #fff;
  padding: 0.25rem 0.5rem;
  font-size: 0.85rem;
  color: #555;
  cursor: pointer;
}
.hint-btn:hover { background: #f0f0f0; }

.notice {
  background: #fff7d6;
  border: 1px solid #e0c94a;
  border-radius: 0.5rem;
  padding: 0.5rem 0.75rem;
  margin: 0 0 1rem;
}

.login-main { max-width: 22rem; margin: 2rem auto; }
.login-form { display: flex; flex-direction: column; gap: 0.75rem; }
.login-form label { display: flex; flex-direction: column; gap: 0.25rem; }
.login-form input { padding: 0.4rem; font-size: 1rem; }
.login-form button { padding: 0.5rem; font-size: 1rem; cursor: pointer; }
.login-error { color: #a00; }

.puzzle-list { border-collapse: collapse; }
.puzzle-list th, .puzzle-list td { text-align: left; padding: 0.25rem 1rem 0.25rem 0; }
