/* ==========================================
   手書き風ホワイトボード - Style Sheet
   ========================================== */

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

:root {
  /* Warm craft-paper palette */
  --bg-board: #f5f0e8;
  --bg-board-dots: #e0d8cc;
  --toolbar-bg: #3e2723;
  --toolbar-bg-glass: rgba(62, 39, 35, 0.92);
  --toolbar-border: rgba(255, 255, 255, 0.08);
  --btn-bg: rgba(255, 255, 255, 0.06);
  --btn-hover: rgba(255, 255, 255, 0.14);
  --btn-active: rgba(255, 193, 7, 0.25);
  --btn-active-border: #ffc107;
  --text-light: #fafafa;
  --text-muted: #bcaaa4;
  --accent: #ffc107;
  --danger: #ef5350;
  --shadow-soft: 0 2px 12px rgba(0, 0, 0, 0.12);
  --shadow-heavy: 0 4px 24px rgba(0, 0, 0, 0.18);
  --radius: 10px;
  --radius-sm: 6px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);

  /* Handwriting fonts */
  --font-hand: 'Caveat', 'Zen Kurenaido', cursive;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-hand);
  background: var(--bg-board);
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ==========================================
   Toolbar
   ========================================== */

#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--toolbar-bg-glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--toolbar-border);
  z-index: 1000;
  flex-shrink: 0;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.2);
}

.toolbar-section {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* CSP対策: インラインstyle="display:none" の代替 */
.sticky-color-section {
  display: none;
}

.toolbar-divider {
  width: 1px;
  height: 32px;
  background: rgba(255, 255, 255, 0.12);
  margin: 0 6px;
  flex-shrink: 0;
}

.toolbar-spacer {
  flex: 1;
}

/* Tool buttons */
.tool-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  background: var(--btn-bg);
  border: 1.5px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  font-family: var(--font-hand);
  font-size: 19px;
  /* Significantly increased for better readability */
  transition: all var(--transition);
  user-select: none;
  touch-action: manipulation;
}

.tool-btn svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.tool-btn:hover {
  background: var(--btn-hover);
  color: var(--text-light);
  transform: translateY(-1px);
}

.tool-btn.active {
  background: var(--btn-active);
  border-color: var(--btn-active-border);
  color: var(--accent);
}

.tool-btn.danger:hover {
  background: rgba(239, 83, 80, 0.2);
  border-color: var(--danger);
  color: var(--danger);
}

/* Section label */
.section-label {
  color: var(--text-muted);
  font-size: 15px;
  /* Increased from 12px */
  margin-right: 8px;
  white-space: nowrap;
}

/* Size slider */
.size-section label,
.pen-size-section label {
  color: var(--text-muted);
  display: flex;
  align-items: center;
}

#size-slider,
#pen-size-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100px;
  height: 5px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

#size-slider::-webkit-slider-thumb,
#pen-size-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  border: 2px solid #fff;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  transition: transform var(--transition);
}

#size-slider::-webkit-slider-thumb:hover,
#pen-size-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

#size-value,
#pen-size-value {
  color: var(--text-muted);
  font-size: 12px;
  min-width: 36px;
  text-align: center;
  font-family: system-ui, sans-serif;
}

/* Color palette */
.color-palette,
.sticky-palette {
  display: flex;
  gap: 4px;
  align-items: center;
}

.color-swatch,
.sticky-color-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}

.color-swatch:hover,
.sticky-color-swatch:hover {
  transform: scale(1.2);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.4);
}

.color-swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent), inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  transform: scale(1.15);
}

.sticky-color-swatch.active {
  border-color: var(--toolbar-bg);
  box-shadow: 0 0 0 2px var(--accent);
  transform: scale(1.15);
}

/* CSP対策: インラインstyle属性の代替 — data-color属性セレクタで背景色を定義 */
.color-swatch[data-color="#2c2c2c"] { background: #2c2c2c; }
.color-swatch[data-color="#d84315"] { background: #d84315; }
.color-swatch[data-color="#1565c0"] { background: #1565c0; }
.color-swatch[data-color="#2e7d32"] { background: #2e7d32; }
.color-swatch[data-color="#f9a825"] { background: #f9a825; }
.color-swatch[data-color="#6a1b9a"] { background: #6a1b9a; }
.color-swatch[data-color="#e65100"] { background: #e65100; }
.color-swatch[data-color="#ffffff"] { background: #ffffff; border: 1px solid #aaa; }

.sticky-color-swatch[data-color="#fff9c4"] { background: #fff9c4; }
.sticky-color-swatch[data-color="#f8bbd0"] { background: #f8bbd0; }
.sticky-color-swatch[data-color="#b3e5fc"] { background: #b3e5fc; }
.sticky-color-swatch[data-color="#c8e6c9"] { background: #c8e6c9; }
.sticky-color-swatch[data-color="#ffe0b2"] { background: #ffe0b2; }
.sticky-color-swatch[data-color="#e1bee7"] { background: #e1bee7; }

#color-picker {
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  background: none;
  padding: 0;
  margin-left: 4px;
}

#color-picker::-webkit-color-swatch-wrapper {
  padding: 0;
}

#color-picker::-webkit-color-swatch {
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
}

/* ==========================================
   Whiteboard
   ========================================== */

#whiteboard {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: crosshair;
  /* Dot grid pattern for craft-paper feel */
  background-color: var(--bg-board);
  background-image:
    radial-gradient(circle, var(--bg-board-dots) 1px, transparent 1px);
  background-size: 24px 24px;
  touch-action: none;
}

#whiteboard-viewport {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
  will-change: transform;
  touch-action: none;
}

#whiteboard.panning {
  cursor: grabbing !important;
}

#draw-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#elements-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
  touch-action: none;
}

/* Zoom indicator */
.zoom-indicator {
  color: var(--text-muted);
  font-size: 12px;
  font-family: system-ui, sans-serif;
  padding: 3px 8px;
  background: var(--btn-bg);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  transition: all var(--transition);
  white-space: nowrap;
}

.zoom-indicator:hover {
  background: var(--btn-hover);
  color: var(--text-light);
}

/* ==========================================
   Text Elements
   ========================================== */

.text-element {
  position: absolute;
  pointer-events: auto;
  min-width: 40px;
  min-height: 1.2em;
  padding: 4px 8px;
  font-family: var(--font-hand);
  outline: none;
  border: 2px dashed transparent;
  border-radius: 4px;
  cursor: default;
  /* Changed from move to default */
  word-break: break-word;
  white-space: pre-wrap;
  transition: border-color 0.15s, box-shadow 0.15s;
  line-height: 1.3;
  background: transparent;
  touch-action: none;
}

.text-element:hover {
  border-color: rgba(255, 193, 7, 0.4);
  background: rgba(255, 193, 7, 0.03);
  /* Subtle backdrop to show hitbox */
}

.text-element:focus,
.text-element.editing {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 193, 7, 0.15);
  cursor: text;
  background: transparent;
}

.text-element.dragging {
  opacity: 0.8;
  cursor: grabbing !important;
  border-color: var(--accent);
  border-style: solid;
}

/* ==========================================
   Sticky Notes
   ========================================== */

.sticky-note {
  position: absolute;
  pointer-events: auto;
  width: 200px;
  height: 160px;
  /* Use height by default */
  min-height: 100px;
  padding: 0;
  border-radius: 4px;
  cursor: move;
  box-shadow: 2px 4px 12px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  transform: rotate(var(--rotate, 0deg));
  transition: box-shadow var(--transition), transform 0.15s;
  display: flex;
  flex-direction: column;
  overflow: visible;
  /* Allow handles to hang outside */
  z-index: 10;
  touch-action: none;
}

.sticky-note.dragging,
.sticky-note.resizing,
.sticky-note.rotating {
  transition: none !important;
  z-index: 1000 !important;
}

.sticky-note::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 28px;
  background: rgba(0, 0, 0, 0.04);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  pointer-events: none;
}

.sticky-note:hover {
  box-shadow: 4px 8px 24px rgba(0, 0, 0, 0.18), 0 2px 6px rgba(0, 0, 0, 0.1);
  transform: rotate(var(--rotate, 0deg)) translateY(-2px);
}

.sticky-note.dragging {
  box-shadow: 6px 12px 32px rgba(0, 0, 0, 0.24);
  transform: rotate(var(--rotate, 0deg)) scale(1.03);
  opacity: 0.92;
  z-index: 100;
}

.sticky-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 28px;
  padding: 0 6px;
  flex-shrink: 0;
  touch-action: none;
}

.sticky-delete {
  width: 18px;
  height: 18px;
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all var(--transition);
  font-size: 14px;
  line-height: 1;
}

.sticky-delete:hover {
  background: rgba(0, 0, 0, 0.1);
  color: rgba(0, 0, 0, 0.6);
}

.sticky-content {
  flex: 1;
  padding: 8px 14px 14px;
  font-family: var(--font-hand);
  font-size: 20px;
  line-height: 1.4;
  outline: none;
  border: none;
  background: transparent;
  color: #3e2723;
  word-break: break-word;
  white-space: pre-wrap;
  cursor: text;
  min-height: 100px;
  position: relative;
  z-index: 2;
}

.sticky-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

/* Hide placeholder if there is text OR if there is ink */
.sticky-note.has-ink .sticky-content::before,
.sticky-content:not(:empty)::before {
  content: none !important;
  display: none !important;
}

.sticky-content:empty::before {
  content: 'Type something...';
  color: rgba(0, 0, 0, 0.25);
  font-style: italic;
}

/* Resize handle for sticky notes */
.sticky-resize {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 18px;
  height: 18px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity var(--transition);
  z-index: 15;
  touch-action: none;
}

.sticky-note:hover .sticky-resize,
.sticky-note.resizing .sticky-resize {
  opacity: 1;
}

.sticky-resize::before {
  content: '';
  position: absolute;
  bottom: 3px;
  right: 3px;
  width: 8px;
  height: 8px;
  border-right: 2px solid rgba(0, 0, 0, 0.2);
  border-bottom: 2px solid rgba(0, 0, 0, 0.2);
}

/* Rotation handle for sticky notes */
.sticky-rotate {
  position: absolute;
  top: -24px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 20px;
  background: white;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity var(--transition), transform 0.2s;
  z-index: 10;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  touch-action: none;
}

.sticky-rotate::after {
  content: '';
  position: absolute;
  top: 20px;
  left: 50%;
  width: 1px;
  height: 10px;
  background: rgba(0, 0, 0, 0.1);
}

.sticky-rotate svg {
  width: 12px;
  height: 12px;
  color: rgba(0, 0, 0, 0.4);
}

.sticky-note:hover .sticky-rotate {
  opacity: 1;
}

.sticky-rotate:active {
  cursor: grabbing;
  transform: translateX(-50%) scale(1.1);
}


/* ==========================================
   Canvas cursor styles
   ========================================== */

#whiteboard.mode-pen {
  cursor: crosshair;
}

#whiteboard.mode-eraser {
  cursor: url('eraser-cursor.svg') 12 12, auto;
}

#whiteboard.mode-text {
  cursor: text;
}

#whiteboard.mode-sticky {
  cursor: copy;
}

/* ==========================================
   Animations
   ========================================== */

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.92);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes stickyDrop {
  0% {
    opacity: 0;
    transform: rotate(var(--rotate, 0deg)) scale(0.6) translateY(-20px);
  }

  60% {
    transform: rotate(var(--rotate, 0deg)) scale(1.05) translateY(2px);
  }

  100% {
    opacity: 1;
    transform: rotate(var(--rotate, 0deg)) scale(1) translateY(0);
  }
}

.text-element {
  animation: fadeIn 0.2s ease-out;
}

.sticky-note {
  animation: stickyDrop 0.35s ease-out;
}

/* ==========================================
   Responsive
   ========================================== */

@media (max-width: 900px) {
  #toolbar {
    flex-wrap: wrap;
    gap: 4px;
    padding: 6px 10px;
  }

  .toolbar-divider {
    display: none;
  }

  .tool-btn span {
    display: none;
  }

  .tool-btn {
    padding: 8px;
  }

  #size-slider,
  #pen-size-slider {
    width: 100px;
  }
}

/* ==========================================
   Contact Link
   ========================================== */

.contact-link {
  position: absolute;
  bottom: 24px;
  right: 24px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--toolbar-bg-glass);
  color: var(--text-muted);
  border-radius: 50%;
  text-decoration: none;
  box-shadow: var(--shadow-soft);
  transition: all var(--transition);
  z-index: 1000;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--toolbar-border);
}

.contact-link:hover {
  transform: translateY(-2px);
  background: var(--toolbar-bg);
  box-shadow: var(--shadow-heavy);
  color: var(--text-light);
}

/* ==========================================
   Download Modal
   ========================================== */

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: none; /* デフォルト非表示 — JSのCSOM操作(style.display='flex')で表示 */
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: fadeIn 0.2s ease-out;
}

.modal-content {
  background: var(--toolbar-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-lg);
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 22px;
  /* Increased from 18px */
  font-weight: 500;
  color: var(--text-light);
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 28px;
  cursor: pointer;
  line-height: 1;
}

.close-btn:hover {
  color: var(--text-light);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  color: var(--text-muted);
}

.modal-body p {
  margin: 0 0 16px 0;
  font-size: 17px;
  /* Increased from 14px */
  line-height: 1.5;
}

.preview-container {
  background: #f5f0e8;
  /* Same as whiteboard bg */
  border-radius: var(--radius-md);
  padding: 10px;
  display: flex;
  justify-content: center;
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1);
}

#preview-img {
  max-width: 100%;
  height: auto;
  border-radius: 2px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.download-link-btn {
  background: var(--accent);
  color: #000;
  padding: 12px 28px;
  border-radius: var(--radius-md);
  text-decoration: none;
  font-weight: 600;
  font-size: 16px;
  /* Increased from 14px */
  transition: all var(--transition);
}

.download-link-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 193, 7, 0.3);
}

.secondary-btn {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-light);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 12px 24px;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 16px;
  /* Increased from 14px */
  transition: all var(--transition);
}

.secondary-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}