/* Classroomscreen-Style Notepad Tool */
#notepad-tool {
    /* Design tokens */
    --np-accent: #0b57d0;
    --np-border: #e5e7eb;
    --np-toolbar-bg: #f7f7f9;
    --np-text-muted: #6b7280;
    --np-hover: #f1f3f5;
    --np-active-bg: #e8f0fe;
    --np-active-brd: #b6d0ff;
    width: 520px;
    height: 360px;
    min-width: 520px;
    min-height: 360px;
    position: relative; /* anchor for absolute-positioned resize handle */
    background-color: #fff;
    border: 1px solid var(--np-border);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    display: grid;
    grid-template-rows: auto 1fr;
    overflow: hidden;
    /* Allow notepad to grow larger than standard tools */
    max-width: 98vw;
    max-height: 96vh;
}

/* Resizer handle appearance for Notepad */
#notepad-tool .resize-handle.bottom-right {
    position: absolute;
    right: 6px;
    bottom: 6px;
    width: 16px;
    height: 16px;
    border-radius: 3px;
    cursor: nwse-resize;
    z-index: 20; /* Above corner-scale-handle (z-index:10) */
    background:
      repeating-linear-gradient(135deg,
        rgba(0,0,0,0.35) 0 2px,
        transparent 2px 6px);
    opacity: .6;
}
#notepad-tool .resize-handle.bottom-right:hover { opacity: .9; }

/* Hide corner scaling for Notepad to prevent overlap with resize handle */
#notepad-tool .corner-scale-handle { display: none !important; }

/* Allow selecting and editing text inside the notepad despite global draggable rules */
#notepad-tool, #notepad-tool * {
    user-select: text;
    -webkit-user-select: text;
}

/* But prevent text selection in the toolbar (avoid highlighting B/I/U, A+/A- on rapid clicks) */
#notepad-toolbar, #notepad-toolbar * {
    user-select: none !important;
    -webkit-user-select: none !important;
    -ms-user-select: none !important;
    -moz-user-select: none !important;
}

/* Keep selects/inputs inside toolbar usable */
#notepad-toolbar select, #notepad-toolbar input, #notepad-toolbar textarea {
    user-select: text !important;
    -webkit-user-select: text !important;
    -ms-user-select: text !important;
    -moz-user-select: text !important;
    cursor: auto;
}

/* Toolbar - shows when tool is selected/active */
/* Top menu strip */
#notepad-toolbar { position: relative; display: block; z-index: 1; flex: 0 0 auto; }
.menu-strip { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background:var(--np-toolbar-bg); border-bottom:1px solid var(--np-border); }
.menu-btn { background:#fff; color:#111; border:1px solid var(--np-border); border-radius: 8px; padding:6px 10px; min-height: 32px; font-size:.9rem; line-height: 1; }
.menu-btn:hover { background:var(--np-hover); }
.menu-btn.active { background:var(--np-active-bg); color:var(--np-accent); border-color:var(--np-active-brd); }
.menu-btn, .menu-btn * { user-select: none; -webkit-user-select:none; }
.menu-sep { flex: 0 0 1px; height: 18px; border-left:1px solid #cfcfcf; margin: 0 6px; }
.wrap-toggle { display:flex; align-items:center; gap:6px; font-size:.9rem; color:#333; }

/* Toolbar grouping */
.toolbar-group { display:flex; align-items:center; gap:8px; background:#fff; padding:4px; border:1px solid var(--np-border); border-radius:10px; }
.toolbar-group .menu-btn { border:1px solid transparent; background: transparent; }
.toolbar-group .menu-btn:hover { background: var(--np-hover); border-color: var(--np-border); }
.toolbar-group .menu-btn.active { background: var(--np-active-bg); border-color: var(--np-active-brd); }

/* Visible focus style for accessibility */
.menu-btn:focus-visible, #np-font:focus-visible, #notepad-toolbar .color-option:focus-visible, #np-color-toggle:focus-visible {
    outline: 2px solid var(--np-accent);
    outline-offset: 2px;
}

/* Toolbar sections */
.toolbar-section {
    display: flex;
    align-items: center;
    gap: 4px;
    padding-right: 8px;
    border-right: 1px solid #eee;
}

.toolbar-section:last-child {
    border-right: none;
    padding-right: 0;
}

/* Font family dropdown */
#notepad-font-family {
    font-size: 12px;
    padding: 4px 6px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    min-width: 90px;
    font-family: inherit;
}

/* Font size input */
#notepad-font-size {
    width: 40px;
    font-size: 12px;
    padding: 4px 6px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    text-align: center;
}

/* Style buttons */
.style-btn {
    width: 28px;
    height: 28px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    transition: all 0.2s;
}

.style-btn:hover {
    background: #f0f0f0;
}

.style-btn.active {
    background: #007bff;
    color: #fff;
    border-color: #007bff;
}

/* Alignment buttons */
.align-btn {
    width: 28px;
    height: 28px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    transition: all 0.2s;
}

.align-btn:hover {
    background: #f0f0f0;
}

.align-btn.active {
    background: #007bff;
    color: #fff;
    border-color: #007bff;
}

/* Line spacing dropdown */
#notepad-line-spacing {
    font-size: 12px;
    padding: 4px 6px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    min-width: 50px;
}

/* Color picker */
#notepad-text-color {
    width: 28px;
    height: 28px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    padding: 0;
    overflow: hidden;
}

/* Background toggle */
#notepad-bg-toggle {
    width: 28px;
    height: 28px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    transition: all 0.2s;
}

#notepad-bg-toggle:hover {
    background: #f0f0f0;
}

#notepad-bg-toggle.active {
    background: #28a745;
    color: #fff;
    border-color: #28a745;
}

/* Delete button */
#notepad-delete {
    width: 28px;
    height: 28px;
    border: 1px solid #dc3545;
    border-radius: 3px;
    background: #fff;
    color: #dc3545;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    transition: all 0.2s;
    margin-left: 4px;
}

#notepad-delete:hover {
    background: #dc3545;
    color: #fff;
}

/* Content area */
#notepad-content { flex: 1 1 auto; display: flex; flex-direction: column; overflow: hidden; min-height: 0; position: relative; z-index: 0; background: #fff; padding: 8px 8px 16px; box-sizing: border-box; }
#notepad-toolbar .color-picker { position: relative; display:inline-block; }
#notepad-toolbar .color-swatch { display:inline-flex; align-items:center; gap:6px; }
#notepad-toolbar .color-swatch .swatch { width:16px; height:16px; border:1px solid rgba(0,0,0,.2); border-radius:4px; background:#000; display:inline-block; }
#notepad-toolbar .color-picker { position: relative; }
#notepad-toolbar .color-menu { position:absolute; top:100%; left:50%; transform: translateX(-50%); margin-top:6px; background:#fff; border:1px solid var(--np-border); border-radius:8px; box-shadow:0 8px 20px rgba(0,0,0,.12); padding:8px; display:grid; grid-template-columns: repeat(3, 20px); grid-auto-rows: 20px; gap:8px; z-index:10; justify-items: start; align-items: start; }
#notepad-toolbar .color-menu.hidden { display:none; }
#notepad-toolbar .color-option { width:20px; height:20px; box-sizing: border-box; display:block; line-height:0; border-radius:4px; border:1px solid rgba(0,0,0,.25); padding:0; margin:0; min-width:0; min-height:0; overflow:hidden; cursor:pointer; appearance:none; -webkit-appearance: none; justify-self: start; align-self: start; }
#notepad-toolbar .color-option:hover { outline:2px solid var(--np-accent); }
#notepad-toolbar .color-option.selected { outline:3px solid var(--np-accent); outline-offset: 0; }

/* Editable contenteditable area */
#notepad-tool #notepad-editor {
    /* Flex child that fills the content area */
    position: relative;
    width: 100%;
    height: 100%;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    max-height: 100%;
    border: 1px solid var(--np-border) !important;
    border-radius: 8px;
    outline: none;
    resize: none; /* we control size via the tool */
    background: #fff;
    font-family: "Comic Sans MS", "Comic Sans", cursive !important;
    font-size: 16px;
    line-height: 1.5;
    color: #000;
    text-align: left;
    caret-color: #000;
    overflow: auto; /* allow scrollbars as needed */
    overflow-x: hidden; /* with word wrap on */
    white-space: pre-wrap; /* default wrap on */
    /* Force wrapping for very long unbroken strings */
    overflow-wrap: anywhere; /* modern: allows breaking anywhere if needed */
    word-wrap: break-word;   /* legacy alias for older engines */
    word-break: break-word;  /* WebKit/Chromium compatibility */
    hyphens: auto;           /* optional hyphenation where dictionaries exist */
    box-sizing: border-box;
    display: block;
    margin: 0;
    padding: 12px;
    /* Ensure editing and text selection work despite global draggable styles */
    user-select: text !important;
    -webkit-user-select: text !important;
    -ms-user-select: text !important;
    pointer-events: auto;
    cursor: text;
    z-index: 2;
}

/* Basic rich text normalization inside editor */
#notepad-editor, #notepad-editor * {
    line-height: inherit;
    font-family: inherit;
    /* Propagate safe breaking behavior into inline children */
    overflow-wrap: anywhere;
    word-wrap: break-word;
    word-break: break-word;
}

/* Placeholder for empty editor */
#notepad-editor.is-empty::before {
    content: attr(data-placeholder);
    position: absolute;
    left: 12px;
    top: 10px;
    color: #9ca3af;
    pointer-events: none;
}

/* Status bar */
#notepad-statusbar { margin-top: 6px; font-size: 12px; color: var(--np-text-muted); display: flex; justify-content: space-between; padding: 2px 2px; }
#notepad-statusbar .sb-item { opacity: .9; }



/* Hide scrollbar but keep functionality */
#notepad-text::-webkit-scrollbar {
    width: 6px;
}

#notepad-text::-webkit-scrollbar-track {
    background: transparent;
}

#notepad-text::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

#notepad-text::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.4);
}

/* Responsive adjustments */
@media (max-width: 768px) { #notepad-editor { font-size: 15px; } }

/* Print styles */
@media print {
    #notepad-toolbar { display: none !important; }
    #notepad-tool { box-shadow: none; border: 1px solid #000; }
}
