:root {
  color-scheme: light;
}

body {
  font-family: "Quicksand", sans-serif;
  background: linear-gradient(135deg, #74ebd5 0%, #acb6e5 100%);
  color: #2c3e50;
  margin: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding: clamp(12px, 5vw, 28px);
  box-sizing: border-box;
  overflow-x: hidden;
}

@supports (padding: env(safe-area-inset-top)) {
  body {
    padding-top: calc(env(safe-area-inset-top) + clamp(12px, 5vw, 28px));
    padding-bottom: calc(env(safe-area-inset-bottom) + clamp(12px, 5vw, 28px));
    padding-left: calc(env(safe-area-inset-left) + clamp(12px, 5vw, 28px));
    padding-right: calc(env(safe-area-inset-right) + clamp(12px, 5vw, 28px));
  }
}

.start-screen {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 24px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  text-align: center;
}

.start-screen h1 {
  color: #e74c3c;
  font-size: clamp(1.9rem, 8vw, 2.4rem);
  margin: 0;
}

.game-container {
  background: #ffffff;
  border-radius: 18px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.15);
  padding: clamp(16px, 5vw, 24px);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 3vw, 16px);
  width: min(100%, 540px);
  min-height: clamp(360px, 96vh, 620px);
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

@media (min-width: 720px) {
  .game-container {
    width: min(92vw, 720px);
    min-height: clamp(420px, 90vh, 620px);
  }
}

@media (min-width: 800px) {
  body { padding: 0; align-items: center; }
  .game-container {
    width: 800px;
    height: 500px;
    min-height: 500px;
    max-height: 500px;
    padding: 12px 16px; /* tighter padding to ensure no overflow */
    gap: 8px;
    overflow: hidden;
  }
  h1 { font-size: 1.3rem; margin: 0; }
  .level-banner { margin: 0; }
  .level-display { padding: 4px 14px; font-size: 0.75rem; }
  .status-row { gap: 6px; margin: 0; }
  .timer-widget { padding: 4px 12px; font-size: 0.85rem; }
  .timer-label { font-size: 0.65rem; }
  .timer-display { font-size: 0.95rem; }
  .goal-chip { padding: 4px 12px; font-size: 0.8rem; }
  .info-cta { padding: 6px 14px; font-size: 0.8rem; }
  .jug-section { gap: 24px; margin: 0; flex: 0 0 auto; }
  .jug-card { gap: 6px; }
  .jug { padding: 8px; gap: 6px; }
  .jug-fill-area { height: 130px; width: 80px; }
  .jug-label { font-size: 0.8rem; }
  .gesture-hint { font-size: 0.75rem; margin: 0; }
  .game-message { max-width: 760px; font-size: 0.85rem; min-height: 2em; margin: 0; }
  .message-panel { margin-top: 0; gap: 6px; }
  .post-level-actions { gap: 8px; margin: 0; }
  .primary-cta, .secondary-cta, .danger-cta { padding: 8px 20px; font-size: 0.85rem; }
}

.game-container.level-complete-flash {
  animation: levelCompleteFlash 0.7s ease-in-out 2;
}

@keyframes levelCompleteFlash {
  0%, 100% {
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.15);
    background: #ffffff;
  }
  50% {
    box-shadow: 0 20px 40px rgba(39, 174, 96, 0.4);
    background: #eafaf1;
  }
}

h1 {
  color: #e74c3c;
  font-size: clamp(1.4rem, 6vw, 1.7rem);
  margin: 0;
}

.level-banner {
  display: flex;
  justify-content: center;
}

.level-display {
  background: rgba(106, 17, 203, 0.12);
  color: #6a11cb;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 6px 18px;
  border-radius: 999px;
  text-transform: uppercase;
  font-size: clamp(0.75rem, 2.4vw, 0.9rem);
}

.status-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  align-items: center;
  gap: 12px;
}
.jug-hero {
  margin: clamp(8px, 3vw, 16px) auto 0;
  display: flex;
  justify-content: center;
  width: min(100%, 360px);
}

.jug-hero__image {
  width: 100%;
  height: auto;
  max-height: clamp(140px, 32vh, 220px);
  object-fit: contain;
}

.timer-widget {
  display: inline-flex;
  justify-self: center;
  align-items: center;
  gap: 8px;
  background: rgba(52, 152, 219, 0.14);
  color: #2980b9;
  padding: 6px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timer-label {
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 0.7rem;
}

.timer-display {
  font-size: clamp(1rem, 4vw, 1.1rem);
}

.goal-chip {
  background: rgba(39, 174, 96, 0.15);
  color: #1e8449;
  padding: 6px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: clamp(0.75rem, 2.6vw, 0.9rem);
  justify-self: center;
}

.info-cta {
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  font-size: clamp(0.8rem, 2.7vw, 0.9rem);
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
  color: #7f2e0a;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  justify-self: center;
}

.info-cta:hover,
.info-cta:focus-visible {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
  outline: none;
}

.jug-section {
  display: flex;

  justify-content: center;
  align-items: flex-end;
  gap: clamp(12px, 4vw, 20px);
  flex: 1;
  min-height: 0;
}

@media (min-width: 640px) and (max-width: 799px) {
  .jug-section {
    flex-direction: row;
    align-items: flex-end;
    gap: clamp(32px, 6vw, 60px);
    min-height: 0;
  }
  .game-container {
    gap: clamp(10px, 3vw, 16px);
  }
}

.jug-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex: 0 1 auto;
  width: min(200px, 60vw);
}

@media (min-width: 640px) {
  .jug-card {
    width: auto;
  }
}

.jug {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: clamp(8px, 3vw, 12px);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.85), rgba(245, 245, 245, 0.9));
  box-shadow: inset 0 0 0 2px rgba(52, 73, 94, 0.18);
  cursor: pointer;
  transition: box-shadow 0.2s ease, transform 0.2s ease;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  overflow: visible;
}

.jug:focus-visible {
  outline: 3px solid #f39c12;
  outline-offset: 4px;
}

.jug.selected {
  box-shadow: inset 0 0 0 3px #f39c12, 0 8px 16px rgba(243, 156, 18, 0.25);
  transform: translateY(-3px);
}

.jug-fill-area {
  width: clamp(70px, 24vw, 95px);
  height: clamp(80px, 20vh, 110px);
  border-radius: 12px 12px 8px 8px;
  background: #ecf0f1;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  transition: all 0.4s ease;
  box-shadow: inset 0 -6px 10px rgba(0, 0, 0, 0.08);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  overscroll-behavior: contain;
}

.water {
  width: 100%;
  background: linear-gradient(180deg, #3498db, #2980b9);
  transition: height 0.4s ease;
  border-radius: 8px 8px 0 0;
  height: 0;
  will-change: height;
}

/* Different heights for jugs based on capacity - dynamically set via JavaScript */
#jug-A-container .jug-fill-area {
  height: var(--jug-a-height, 80px);
}

#jug-B-container .jug-fill-area {
  height: var(--jug-b-height, 100px);
}

.jug-label {
  font-size: clamp(0.78rem, 2.5vw, 0.9rem);
  color: #34495e;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.gesture-hint {
  margin: 0;
  font-size: clamp(0.72rem, 2.4vw, 0.85rem);
  color: #616e7c;
}

.message-panel {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.game-message {
  font-size: clamp(0.85rem, 2.6vw, 1rem);
  font-weight: 700;
  min-height: 2.4em;
  line-height: 1.4;
  max-width: min(90vw, 720px);
}

.post-level-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  width: 100%;
}

.primary-cta,
.secondary-cta,
.danger-cta {
  border: none;
  border-radius: 999px;
  padding: 12px clamp(22px, 8vw, 30px);
  font-size: clamp(0.85rem, 2.7vw, 0.95rem);
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  touch-action: manipulation;
}

.primary-cta {
  background: linear-gradient(135deg, #00b4db, #0083b0);
  color: #ffffff;
}

.secondary-cta {
  background: linear-gradient(135deg, #8e54e9, #4776e6);
  color: #ffffff;
}

.danger-cta {
  background: linear-gradient(135deg, #ff5f6d, #ffc371);
  color: #7f1d1d;
}

.primary-cta:hover,
.secondary-cta:hover,
.danger-cta:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
}

.level-progress-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(46, 134, 222, 0.18);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  padding: clamp(12px, 6vw, 24px);
}

.level-progress-overlay.visible {
  opacity: 1;
}

.overlay-content {
  background: rgba(255, 255, 255, 0.96);
  border-radius: 20px;
  padding: clamp(18px, 6vw, 26px) clamp(24px, 8vw, 38px);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.2);
  text-align: center;
  transform: scale(0.8);
  opacity: 0;
}

.level-progress-overlay.visible .overlay-content {
  animation: overlayPop 0.8s ease forwards;
}

.overlay-label {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  font-size: clamp(0.68rem, 2.2vw, 0.8rem);
  color: #2980b9;
}

.overlay-title {
  margin: 6px 0 0;
  font-size: clamp(1.15rem, 4.5vw, 1.6rem);
  color: #1e8449;
}

@keyframes overlayPop {
  0% {
    opacity: 0;
    transform: scale(0.6);
  }
  60% {
    opacity: 1;
    transform: scale(1.08);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.how-to-play-modal {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(44, 62, 80, 0.25);
  padding: clamp(12px, 5vw, 24px);
  box-sizing: border-box;
  z-index: 20;
  overflow: hidden;
}

.modal-content {
  background: #ffffff;
  border-radius: 16px;
  padding: clamp(18px, 5vw, 24px);
  width: min(90%, 400px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
  text-align: left;
  max-height: 100%;
  overflow: hidden;
}

@media (min-width: 800px) {
  .how-to-play-modal { padding: 12px; }
  .modal-content { width: 360px; padding: 16px; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.modal-header h2 {
  font-size: clamp(1rem, 3.4vw, 1.2rem);
  margin: 0;
  color: #34495e;
}

.modal-close {
  border: none;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: clamp(0.8rem, 2.5vw, 0.9rem);
  font-weight: 700;
  cursor: pointer;
  background: linear-gradient(135deg, #bdc3c7, #95a5a6);
  color: #2c3e50;
}

.modal-close:hover,
.modal-close:focus-visible {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.18);
  outline: none;
}

.instruction-list {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: clamp(0.82rem, 2.7vw, 0.96rem);
  color: #2c3e50;
}

.highlight {
  color: #f39c12;
  font-weight: 700;
}

.hidden {
  display: none !important;
}

@media (max-width: 420px) {
  .post-level-actions button {
    width: 100%;
  }
}

/* Mobile layout tweaks: keep jugs side-by-side and scale sizes */
@media (max-width: 639px) {
  /* Use explicit margins on mobile to avoid flex-gap support issues */
  .game-container { gap: 0; }
  .level-banner { margin-bottom: 8px; }
  .jug-section {
    flex-direction: row;
    align-items: flex-end;
    justify-content: center;
    flex-wrap: nowrap;
    gap: 8px;
    flex: 0 0 auto;
    margin: 0 auto;
    margin-bottom: 12px; /* space below jugs before How To */
    min-height: 0;
  }

  /* Space between status and jugs */
  .status-row { margin-bottom: 10px; }

  /* Pull message panel up under How To and tighten spacing */
  .message-panel {
    margin-top: 10px; /* space after How To before message */
    gap: 6px;
  }

  .game-message {
    min-height: 1.6em;
  }

  .jug-card {
    flex: 0 0 calc(50% - 4px);
    width: calc(50% - 4px);
    max-width: none;
  }

  .jug {
    padding: 8px;
  }

  .jug-fill-area {
    width: clamp(60px, 30vw, 86px);
  }

  #jug-A-container .jug-fill-area {
    height: var(--jug-a-height, 80px);
  }

  #jug-B-container .jug-fill-area {
    height: var(--jug-b-height, 100px);
  }

  .jug-label {
    font-size: clamp(0.72rem, 3.2vw, 0.82rem);
  }
}

/* Faucet icon hotspot for tap-to-fill */
.faucet-tap {
  position: relative;
  width: 30px;
  height: 30px;
  margin: 0 auto 4px auto;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #ffffff 0%, #e0f7ff 45%, #bfe9ff 100%);
  box-shadow: 0 3px 8px rgba(0,0,0,0.2), inset 0 0 0 2px rgba(0,0,0,0.1);
  cursor: pointer;
  touch-action: manipulation;
  display: grid;
  place-items: center;
  z-index: 10;
  flex-shrink: 0;
}

.faucet-tap::before {
  content: "🚰";
  font-size: 16px;
  line-height: 1;
}

@media (max-width: 640px) {
  .faucet-tap { width: 34px; height: 34px; margin-bottom: 6px; }
  .faucet-tap::before { font-size: 18px; }
}

/* Spout icon hotspot for tap-to-empty */
.spout-tap {
  position: relative;
  width: 30px;
  height: 30px;
  margin: 4px auto 0 auto;
  border-radius: 50%;
  background: radial-gradient(circle at 70% 70%, #ffffff 0%, #ffe7e7 45%, #ffd1d1 100%);
  box-shadow: 0 3px 8px rgba(0,0,0,0.2), inset 0 0 0 2px rgba(0,0,0,0.1);
  cursor: pointer;
  touch-action: manipulation;
  display: grid;
  place-items: center;
  z-index: 10;
  flex-shrink: 0;
}

.spout-tap::before {
  content: "🕳";
  font-size: 16px;
  line-height: 1;
}

@media (max-width: 640px) {
  .spout-tap { width: 34px; height: 34px; margin-top: 6px; }
  .spout-tap::before { font-size: 18px; }
}

/* Mobile-only How To button near jugs */
.mobile-howto { display: none; }

/* Replace gesture hint with button on small screens */
@media (max-width: 640px) {
  .gesture-hint { display: none; }
  .mobile-howto { display: inline-block; margin: 0 0 10px 0; z-index: 10; position: relative; }
  /* Optional: hide header How To on mobile to avoid duplication */
  #how-to-play-btn { display: none; }
}


/* Consolidated mobile spacing pass to ensure visible gaps across devices */
@media (max-width: 700px) {
  /* Consistent vertical rhythm on mobile */
  .game-container { gap: 12px !important; min-height: auto; padding-bottom: 12px; }
  .game-container > h1 { margin: 0; }
  .level-banner { margin: 0; }
  .status-row { margin: 0 !important; }
  .jug-section { margin: 0 !important; }
  .mobile-howto { margin: 0 !important; }
  .message-panel { margin: 0 !important; gap: 8px; }
}

/* Final mobile height override: shrink container height on phones */
@media (max-width: 700px) {
  .game-container { min-height: 0 !important; height: auto !important; }
}
