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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  background: #f0f2f5;
}

/* ===== Header ===== */
header {
  background: #1a1d2e;
  color: white;
  padding: 1.25rem 1rem;
  text-align: center;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.4;
}

.subtitle {
  font-size: 0.85rem;
  opacity: 0.75;
  margin-top: 0.4rem;
}

/* ===== Main layout ===== */
main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}

/* ===== Ad unit ===== */
.ad-unit {
  margin: 1.25rem 0;
}

/* ===== Tool Section ===== */
.tool-section {
  background: white;
  border-radius: 10px;
  padding: 1rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.07);
}

.tool-layout {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ===== Controls Panel ===== */
.panel-controls {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.control-group {
  border-bottom: 1px solid #f0f0f0;
  padding: 1rem 0;
}

.control-group:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.panel-title {
  font-size: 0.78rem;
  font-weight: 700;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.75rem;
}

/* Device buttons */
.device-buttons {
  display: flex;
  gap: 0.5rem;
}

.device-btn {
  flex: 1;
  padding: 0.5rem 0.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.3;
  background: #f5f5f5;
  color: #555;
  border: 2px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  min-height: 44px;
  transition: all 0.15s;
  text-align: center;
}

.device-btn small {
  display: block;
  font-weight: 400;
  font-size: 0.72rem;
  color: #999;
}

.device-btn.active,
.device-btn:hover {
  background: #e8eeff;
  border-color: #5b8dee;
  color: #2a5bd7;
}

.device-btn.active small {
  color: #5b8dee;
}

/* Preset buttons */
.preset-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.preset-btn {
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  background: #f5f5f5;
  color: #555;
  border: 1px solid #ddd;
  border-radius: 20px;
  cursor: pointer;
  min-height: 44px;
  transition: all 0.15s;
}

.preset-btn:hover,
.preset-btn.active {
  background: #5b8dee;
  color: white;
  border-color: #5b8dee;
}

/* Slider rows */
.slider-row {
  margin-top: 0.75rem;
}

.slider-row label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: #444;
  margin-bottom: 0.3rem;
}

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

.slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: #ddd;
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #5b8dee;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(91, 141, 238, 0.4);
}

.slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: #5b8dee;
  cursor: pointer;
}

output {
  font-size: 0.9rem;
  font-weight: 700;
  color: #5b8dee;
  min-width: 2.5rem;
  text-align: right;
}

/* Toggle switch */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
}

.toggle-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: #444;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider-switch {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #ccc;
  border-radius: 24px;
  transition: background 0.2s;
}

.slider-switch::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
}

.switch input:checked + .slider-switch {
  background: #5b8dee;
}

.switch input:checked + .slider-switch::before {
  transform: translateX(20px);
}

/* Color inputs */
.color-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
}

.color-row label {
  font-size: 0.85rem;
  font-weight: 600;
  color: #444;
}

.color-input {
  width: 44px;
  height: 32px;
  padding: 2px;
  border: 1px solid #ddd;
  border-radius: 6px;
  cursor: pointer;
  background: none;
}

/* Primary button */
.btn-primary {
  display: block;
  width: 100%;
  padding: 0.75rem;
  font-size: 0.95rem;
  font-weight: 700;
  background: #1a1d2e;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  min-height: 44px;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: #2d3250;
}

/* ===== Preview Panel ===== */
.panel-preview {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.preview-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.size-label {
  font-size: 0.8rem;
  color: #999;
  font-variant-numeric: tabular-nums;
}

.canvas-scroll {
  overflow-x: auto;
  background: #e8eaed;
  border-radius: 8px;
  padding: 0.75rem;
  min-height: 200px;
  display: flex;
  justify-content: center;
}

#grid-canvas {
  display: block;
  max-width: 100%;
  border-radius: 4px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  background: white;
}

/* ===== Output Section ===== */
.output-section {
  background: white;
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.07);
}

.output-section h2 {
  font-size: 1rem;
  font-weight: 700;
  color: #1a1d2e;
  margin-bottom: 1rem;
}

.output-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.tab-btn {
  padding: 0.45rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  background: #f0f2f5;
  color: #666;
  border: 1px solid #ddd;
  border-radius: 6px;
  cursor: pointer;
  min-height: 44px;
  transition: all 0.15s;
}

.tab-btn.active,
.tab-btn:hover {
  background: #5b8dee;
  color: white;
  border-color: #5b8dee;
}

.code-block-wrap {
  position: relative;
}

.code-block {
  background: #1a1d2e;
  color: #abb2bf;
  padding: 1rem;
  border-radius: 8px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre;
  min-height: 140px;
}

.code-block.hidden {
  display: none;
}

.btn-copy {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  background: rgba(255,255,255,0.1);
  color: #ccc;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 5px;
  cursor: pointer;
  min-height: 44px;
  transition: all 0.15s;
}

.btn-copy:hover {
  background: rgba(255,255,255,0.2);
  color: white;
}

.btn-copy.copied {
  background: #49cc90;
  color: white;
  border-color: #49cc90;
}

/* ===== How-to Section ===== */
.howto-section {
  background: white;
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.07);
  margin-top: 0;
}

.howto-section h2 {
  font-size: 1rem;
  font-weight: 700;
  color: #1a1d2e;
  margin-bottom: 0.75rem;
  margin-top: 1.5rem;
}

.howto-section h2:first-child {
  margin-top: 0;
}

.howto-section ol {
  padding-left: 1.4rem;
}

.howto-section li {
  font-size: 0.9rem;
  color: #555;
  margin-bottom: 0.6rem;
}

.howto-section p {
  font-size: 0.9rem;
  color: #555;
  margin-bottom: 0.75rem;
  line-height: 1.7;
}

/* ===== Footer ===== */
footer {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.85rem;
  color: #888;
}

footer a {
  color: #666;
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* ===== Responsive: Tablet+ ===== */
@media (min-width: 640px) {
  header h1 {
    font-size: 1.35rem;
  }

  main {
    padding: 1.5rem;
  }
}

/* ===== Responsive: Desktop ===== */
@media (min-width: 900px) {
  header h1 {
    font-size: 1.5rem;
  }

  .tool-layout {
    flex-direction: row;
    align-items: flex-start;
    gap: 1.5rem;
  }

  .panel-controls {
    width: 280px;
    flex-shrink: 0;
  }

  .panel-preview {
    flex: 1;
    min-width: 0;
  }
}
