:root {
  --background-color: #f4f4f4;
  --panel-background: #ffffff;
  --text-color: #171717;
  --muted-color: #666666;
  --border-color: #d7d7d7;
  --accent-color: #1473e6;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--background-color);
}

body {
  min-height: 100dvh;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text-color);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
select {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

#game-area {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--background-color);
  touch-action: none;
}

#target {
  position: absolute;
  z-index: 2;
  display: block;
  padding: 0;
  border: 0;
  background: var(--accent-color);
  cursor: pointer;
  touch-action: none;
  box-shadow: 0 3px 12px rgb(0 0 0 / 14%);
  will-change: left, top;
}

#target:active {
  scale: 0.96;
}

#target:focus-visible {
  outline: 4px solid rgb(20 115 230 / 35%);
  outline-offset: 4px;
}

#target.is-image {
  background: transparent;
  box-shadow: none;
}

#target-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}

#feedback-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
}

.feedback-ring {
  position: absolute;
  border: 3px solid currentColor;
  border-radius: 50%;
  opacity: 0.45;
  transform: translate(-50%, -50%) scale(0.7);
  animation: feedback-ring 280ms ease-out forwards;
}

.feedback-copy {
  position: absolute;
  overflow: hidden;
  pointer-events: none;
  will-change: transform, opacity;
}

.feedback-copy img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.feedback-copy.is-fade {
  animation: feedback-fade 190ms ease-out forwards;
}

.feedback-copy.is-pulse {
  animation: feedback-pulse 220ms ease-out forwards;
}

@keyframes feedback-ring {
  to {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.25);
  }
}

@keyframes feedback-fade {
  to {
    opacity: 0;
    transform: scale(0.88);
  }
}

@keyframes feedback-pulse {
  50% {
    opacity: 0.55;
    transform: scale(1.08);
  }
  100% {
    opacity: 0;
    transform: scale(0.94);
  }
}

.icon-button {
  position: fixed;
  top: calc(var(--safe-top) + 10px);
  right: calc(var(--safe-right) + 10px);
  z-index: 10;
  width: 46px;
  height: 46px;
  padding: 0;
  border: 1px solid rgb(0 0 0 / 10%);
  border-radius: 50%;
  background: rgb(255 255 255 / 78%);
  color: #333333;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 2px 12px rgb(0 0 0 / 12%);
}

.icon-button:active {
  transform: scale(0.94);
}

.modal[hidden],
[hidden] {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding:
    calc(var(--safe-top) + 18px)
    calc(var(--safe-right) + 18px)
    calc(var(--safe-bottom) + 18px)
    calc(var(--safe-left) + 18px);
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgb(0 0 0 / 42%);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.settings-panel {
  position: relative;
  z-index: 1;
  width: min(540px, 100%);
  max-height: 100%;
  overflow: auto;
  border-radius: 20px;
  background: var(--panel-background);
  box-shadow: 0 20px 60px rgb(0 0 0 / 28%);
  -webkit-overflow-scrolling: touch;
  user-select: text;
  -webkit-user-select: text;
}

.settings-header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 18px 12px 22px;
  border-bottom: 1px solid var(--border-color);
  background: rgb(255 255 255 / 94%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.settings-header h1 {
  margin: 0;
  font-size: 1.35rem;
}

.close-button {
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #eeeeee;
  color: #333333;
  font-size: 30px;
  line-height: 36px;
  cursor: pointer;
}

.settings-content {
  display: grid;
  gap: 22px;
  padding: 20px 22px 28px;
}

.panel-section {
  display: grid;
  gap: 12px;
  padding-bottom: 22px;
  border-bottom: 1px solid #e7e7e7;
}

.panel-section:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.panel-section h2 {
  margin: 0;
  font-size: 1.02rem;
}

.section-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.setting-row,
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 42px;
  font-weight: 600;
}

.setting-row output {
  min-width: 88px;
  color: var(--muted-color);
  text-align: right;
  font-weight: 500;
}

.settings-subgroup {
  display: grid;
  gap: 12px;
  padding: 4px 0 4px 14px;
  border-left: 3px solid #e4e4e4;
}

.field-help {
  margin: 0;
  color: var(--muted-color);
  font-size: 0.9rem;
  line-height: 1.4;
  font-weight: 400;
}

select {
  max-width: 230px;
  min-height: 40px;
  padding: 6px 34px 6px 10px;
  border: 1px solid var(--border-color);
  border-radius: 9px;
  background: #ffffff;
  color: var(--text-color);
}

input[type="range"] {
  width: 100%;
  min-height: 34px;
  margin: -6px 0 4px;
  accent-color: var(--accent-color);
}

input[type="color"] {
  width: 56px;
  height: 38px;
  padding: 2px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: #ffffff;
}

input[type="checkbox"] {
  width: 24px;
  height: 24px;
  accent-color: var(--accent-color);
}

.statistics-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.statistic-card {
  min-width: 0;
  padding: 10px;
  border: 1px solid #e1e1e1;
  border-radius: 12px;
  background: #f8f8f8;
}

.statistic-card strong {
  display: block;
  margin-top: 4px;
  overflow: hidden;
  font-size: 1.05rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.statistic-label {
  display: block;
  color: var(--muted-color);
  font-size: 0.76rem;
  line-height: 1.2;
}

.count-badge {
  display: inline-grid;
  min-width: 28px;
  height: 28px;
  place-items: center;
  padding: 0 8px;
  border-radius: 999px;
  background: #eeeeee;
  color: #444444;
  font-size: 0.85rem;
  font-weight: 700;
}

.upload-button,
.primary-button,
.secondary-button,
.text-button,
.small-text-button {
  cursor: pointer;
}

.upload-button {
  display: grid;
  min-height: 48px;
  place-items: center;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  background: #f5f5f5;
  color: var(--text-color);
  font-weight: 700;
}

.image-library {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 10px;
}

.image-item {
  position: relative;
  min-width: 0;
  padding: 7px;
  border: 1px solid #dedede;
  border-radius: 12px;
  background: #fafafa;
}

.image-item img {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
  object-fit: contain;
  background: #ffffff;
}

.image-name {
  display: block;
  margin-top: 6px;
  overflow: hidden;
  color: #555555;
  font-size: 0.72rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-image-button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgb(255 255 255 / 92%);
  color: #842525;
  font-size: 20px;
  line-height: 1;
  box-shadow: 0 1px 5px rgb(0 0 0 / 20%);
}

.empty-library {
  grid-column: 1 / -1;
  margin: 0;
  padding: 14px;
  border: 1px dashed #c9c9c9;
  border-radius: 12px;
  color: var(--muted-color);
  text-align: center;
  font-size: 0.9rem;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 2px;
}

.primary-button,
.secondary-button,
.text-button {
  min-height: 48px;
  border-radius: 12px;
}

.primary-button {
  border: 0;
  background: var(--accent-color);
  color: #ffffff;
  font-weight: 700;
}

.secondary-button {
  border: 1px solid var(--border-color);
  background: #f5f5f5;
  color: var(--text-color);
  font-weight: 650;
}

.text-button {
  border: 0;
  background: transparent;
  color: #a02323;
  font-weight: 600;
}

.small-text-button {
  min-height: 34px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--accent-color);
  font-size: 0.88rem;
  font-weight: 650;
}

.primary-button:disabled,
.secondary-button:disabled,
.text-button:disabled {
  opacity: 0.5;
  cursor: default;
}

.install-help {
  margin: 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: #eef5ff;
  color: #23456e;
  font-size: 0.94rem;
  line-height: 1.4;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@media (max-width: 540px) {
  .statistics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .action-grid {
    grid-template-columns: 1fr;
  }

  select {
    max-width: 190px;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .settings-panel {
    animation: panel-in 160ms ease-out;
  }

  @keyframes panel-in {
    from {
      opacity: 0;
      transform: translateY(10px) scale(0.98);
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .feedback-ring,
  .feedback-copy {
    animation-duration: 1ms !important;
  }
}
